-1

I have a collection of a custom class which looks like this:

CustomClass.java

public class CustomClass{
    public String id;
    public String name;
}

I have a list of ids and names which populates an ObservableList of my custom class from a file:

ObservableList<CustomClass> list = FXCollections.observableArrayList();
for(String s : line){
    CustomClass cc = new CustomClass();
    cc.id = s.split("/")[0];
    cc.name = s.split("/")[1];
    list.add(cc)
}

this will create a list which will look like this

id       name
1      data1
1      data2
1      data3
2      data1
2      data2

I want to get an array containing the information of id and how many instances are present in the ObservableList, like

id=1, count=3
id=2, count=2

How could be done?

Edit:

Based on @mipa 's answer and comments, I've managed to do it by iterate thru list and add in the Map the key if not exist, and increment the counter for those who exists. But I saw on this site some answers on simple string lists (List<String>) which counts by using stream and Collectors. I want to know if it is such a solution for this problem and to understand how this solution works on collections with custom classes, like this one from my example.

VMaties
  • 1
  • 1
  • 1
    this is basically unrelated to javafx (ObservableList is-a List), simply work through a tutorial on java language basics to learn and understand how to use it :) – kleopatra Nov 29 '22 at 11:06
  • 1
    Does this answer your question? [Count int occurrences with Java8](https://stackoverflow.com/questions/23925315/count-int-occurrences-with-java8) – Marv Nov 29 '22 at 11:16

1 Answers1

1

You could iterate over your list and add the id and the current count to a map. While iterating, if the map already contains the next id then increment the count, if not add a new id/count pair to the map with the count set to 1.

Remark: This question has nothing to do with JavaFX because you are only dealing with ordinary List/Map features here.

Edit: Link with stream solution. How to count the number of occurrences of an element in a List

mipa
  • 10,369
  • 2
  • 16
  • 35
  • Thanks @mipa . I'm started programming in JavaFX, so I thought I've must tag it here as well. After this answer I've did it this way, by iterating thru the list, but is there another way of doing it, by grouping, using Collectors? I've saw some answers over stackoverflow using stream and Collectors, but I don't know how to do it that way, seems to be more "codeless" :) Thanks again! – VMaties Nov 29 '22 at 10:35
  • Yes, this could be done via streams as well and this may be even more "codeless" but obviously also less "understandable". Otherwise you wouldn't ask :-) – mipa Nov 29 '22 at 10:40
  • Definitely, if I see such an answer, with explanations I will surely understand. I will be grateful if you could provide such an answer. (edited my question for this :) ) – VMaties Nov 29 '22 at 10:59
  • The solution is just one Google search away "java list count occurrences". See edit above. – mipa Nov 29 '22 at 11:13