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

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

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

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"