Skip to content
Advertisement

Java streams collect excel CSV to a list filtering based on the sum of a column

Suppose we have an excel spreadsheet that looks like:

JavaScript

How can I be able to gather to a list of only the IDs of the people whose status count is a sum greater than 0, and if it is 0 or less then discard it. So in the excel spreadsheet above, the list in java should look like:

List<Integer> = [122145, 149333, 884214, 993213]

Update (adding in what I tried so far):

JavaScript

I collected them just by status counts of 1 but that isn’t the right process, it should be to sum up the status count for each person or ID (I guess it is good to find any dupes) and if its > 0 then collect to the list, if not then discard.

Update 2: I forgot to mention that the csv file is brought into java as a List<String[]> where the List contains the rows of the csv and the String[] is the contents of the rows, so it would be like:

[[1, Tod, Mahones, 122145],[0, Tod, Mahones, 122145], [1, Tod, Mahones, 122145], ...]

Advertisement

Answer

The following should work:

  1. Create a Map<Integer, Integer> to summarize statuses per ID using Collectors.groupingBy + Collectors.summingInt
  2. Filter entries of the intermediate map and collect keys (IDs) to the list.

If the order of IDs should be maintained as in the input file, a LinkedHashMap::new can be provided as an argument when building the map.

JavaScript

Test

JavaScript

Output

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement