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.