I know that there are different ways to solve this task, but I need a particular way using replaceAll() method. I just stuck with right condition in the expression. So I have a method like this: The case is next: I pass to the method some comments and max length of comment. The method should take list of comments and
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
Print the contents of a HashMap in sorted order according to the Size of each HashMap nested inside the given HashMap
I have a HashMap inside a HashMap. I want to print the elements in the HashMap according to the size of elements in the inner HashMap. So the element with the highest number of elements should print first. I’m new to HashMaps and got stuck. This is the HashMap and how I’m printing it: The output that I’m currently getting:
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
How to add using element to a Collection Set using Streams?
Currently I am adding an element to a Collection Set like this. Is there a way to do this with Streams that does not use Stream.concat()? Answer I would suggest you to remodel the objects, you’re updating User’s favorites outside this should happen within the User class Read this OO Principle, TellDon’tAsk