# How to perform arithmetic on the result of Collectors.Counting()?

Given: List<Integer> myIntegers = Arrays.asList(1, 1, 2, 3, 4, 2); Return: an Integer = Sum(((Frequency of Integer) / 2))) I’m able to get the frequency of each integer using Collectors.groupingBy(), but want to then divide each frequency value by 2 and then sum all the values in the map, returning just an Integer: Map<Integer, Long>… Read More How to perform arithmetic on the result of Collectors.Counting()?

# Problem with .collect() method in list .stream() in JAVA

public class PositiveNumbers { public static List<Integer> positive(List<Integer> numbers){ return numbers.stream() .mapToInt(Integer::valueOf) .filter(s -> s >= 0) .collect(Collectors.toCollection(ArrayList<Integer>::new)); } } Image of the code and description of problem given by IntelliJ Tried all that program gives as fix, asked chatGPT, but no results. I cannot see the problem. I tried also .collect(Collectors.toList()); but same problem…… Read More Problem with .collect() method in list .stream() in JAVA

# Java collector to Optional<T>

Using Java streams and generics I’m trying to create a Collector, that would return me Optional.empty, if stream is empty, Optional<T> if stream has one item and throw exception if stream has more than one item. public static <T, R extends Optional<T>> Collector<T, ?, R> toOptional() { return Collectors.collectingAndThen( Collectors.toList(), list -> { if (list.size()… Read More Java collector to Optional<T>

# Collectors group by characteristic and minimum value of a field of said characteristic

Title is sort of confusing but I’m not sure how to explain my problem in a simple sentence. I have a homework task to group an arraylist of rectangles by length (same as perimeter in this case) using streams and collectors and to calculate minimum width for each group. I have tried the following: public… Read More Collectors group by characteristic and minimum value of a field of said characteristic

# Function::identity doesn't work in Collectors.toMap

I am trying to turn a List<String> into a Map<T, String>, with the value of the map being an element contained within the previous List<String>, and the key being some attribute of that String (for example, the length of the String, in which case, T would actually be an Integer). I first attempted to do… Read More Function::identity doesn't work in Collectors.toMap

# Arrays.asList() how to append String value to each item

I’m reading getting the names of all the images inside a subfolder of my assets folder. //Returns name of all the images inside folder_previews private static List<String> getPreviews(Context context) throws IOException { AssetManager assetManager = context.getAssets(); String[] files = assetManager.list("folder_previews"); return Arrays.asList(files); } I then want to concat a String before each one. try {… Read More Arrays.asList() how to append String value to each item

# Hashmap – cannot infer arguments

Hey ðŸ™‚ This is my first post and my first approach at streams! I am trying to learn it by myself, but since its a new style of programming Im having a tough time ðŸ˜€ So here’s my problem: In the following Method I have a Map of <Long, Ingredient> as parameter. (Ingredient has the… Read More Hashmap – cannot infer arguments

# How to use stream in Java

I have a list of users that need to be sorted by the first name. How do I use stream correctly in this situation? public static List<User> users= List.of( new User("Andrea", "Winter", 45), new User("Lucy", "Diamond", 24)); static List<String> firstName() { return users.stream() .sorted(Comparator.comparing(User::FirstName)); } >Solution : You were close. You just didn’t return a… Read More How to use stream in Java

# How to remove Sonar issue on Java stream "Refactor the code so this stream pipeline is used"

I’m working on a project (Java 17) where I have a list of object with two properties, actionId (String) and right (boolean). I’m trying to get the actionId for object with right = true and store the result as List of String. This is my code: List<String> usserValidActionsArray = userActionGatewayDTO.stream().filter(UserActionGatewayDTO::getRight) .map(UserActionGatewayDTO::getActionId).toList(); My code works fine,… Read More How to remove Sonar issue on Java stream "Refactor the code so this stream pipeline is used"

# Deleting word inside text file

I amdeveloping a command line application to filter a text file by deleting the words specified by the user via the command line. when the code finds the filter word inside the line its deleting the whole line not just the word. Is there a way to split the line into separate words? public class… Read More Deleting word inside text file