Skip to content
Advertisement

Tag: collections

Java aggregate same objects into one

I’m quite new into programming and got a tricky question. I got an object which has multiple parameters: Every object always has non-null number attribute as well as one of three values-field. So for example, if valueOne is not null, the other two value fields valueTwo and valueThree would be null. So here’s my problem: The SampleObject is referenced in

Add unique elements in HashSet based on attributes of framework provided non-editable object classes

I am trying to generate a HashSet containing unique Employee instances. Uniqueness should be established based on the object properties. The problem is that I end up with having duplicates. Note that Employee class is provided by a framework, and it’s not possible to provide custom implementations for equals() and hashCode(). Employee class: This would result in a Set mapped

Retrieve word(s) from characters using map

I am trying to understand Collections and Stream. I have split the sentence below and kept the position of each letter (I ignored space/blank): “Hello Word!” Results: [H, e, l, l, o, , W, o, r, d, !] (charsList) {!=[10], r=[8], d=[9], e=[1], W=[6], H=[0], l=[2, 3], o=[4, 7]} (charsIndex) How can I sort the characters and rebuild my word

Java Stream Collect() classifier can’t detect type

I have the following code reading lines from a text file: The text file has data like And I’m trying to group by String1 and String2 and get their counts. Then end result should be a Map<String, Map<String, Long>>. However, with the code above, the compiler is saying that the collect() returns a ConcurrentMap <Object, ConcurrentMap<Object, Long>>. Why are the

ImmutableMap.of vs Java Map.of

I have seen that ImmutableMap.of is used to create immutable map object. At the same time Java offers Map.of which creates immutable map as well. Is there any advantage of using ImmutableMap.of instead of Map.of from Java ? Edit: I am not asking differences between map and immutable map. I am asking why some people prefer ImmutableMap.of instead of Map.of

Java List Stream

I am attempting to simplify my code but list stream is not working. I am creating an instance of a class (that contains an inner class) in a static method and trying to take items from the first list and instantiate them in the inner class and add them to a new list. My code works with fruit loops if

How to write a method that takes in a List of Integer, Float, Double and calculate the average?

I am trying to write a method that takes in a list of numeric values – eg List<Integer>, List<Float>, List<Double> etc – and give me the average. These are the errors I get: Operator ‘+’ cannot be applied to ‘capture<? extends java.lang.Number>’, ‘capture<? extends java.lang.Number>’ Answer OptionalDouble average() Where, OptionalDouble is a container object which may or may not contain

Advertisement