Count number of items with property

后端 未结 10 2746
不思量自难忘°
不思量自难忘° 2021-02-13 12:18

I have list List where Custom is like

class Custom{
    public int id;
    public String name;
}

How to

10条回答
  •  天命终不由人
    2021-02-13 12:56

    Either you keep track as you add or remove items from the list. This could take the place of a hashmap Name->Count . Where when you add an item you increment the count for that name, and when you remove it you decrement the count.

    Or you iterate over the collection with a loop checking for the name in question.

    Depending on the behavior of your application one of these methods will be faster but without more information it is hard to tell which.

提交回复
热议问题