Skip to content
Advertisement

How to count number of custom objects in list which have same value for one of its attribute

I am programming in java. Say I have an custom object Item

JavaScript

I have list of Items.

JavaScript

Now, What is best way in java to know, list of Items contain some Items with same value for itemNumber.

Advertisement

Answer

To search for a specific item with some item number:

JavaScript

To get lists of items for all item numbers:

You can use a HashMap for this case:

JavaScript

Later on, you can iterate over map’s keyset and get all items for each key:

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