2

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

class Item
{
     Integer id;
     BigDecimal itemNumber;
}

I have list of Items.

List<Item> items = new ArrayList<>();

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

Ankush soni
  • 1,439
  • 1
  • 15
  • 30
Prashant Shilimkar
  • 8,402
  • 13
  • 54
  • 89

5 Answers5

5

To search for a specific item with some item number:

//result list
List<Item> itemsWithSameNumber = new ArrayList<>();

for (Item item : items) {
    if (item.getItemNumber().equals(yourKey)) {
        itemsWithSameNumber.add(item);
    }
}

To get lists of items for all item numbers:

You can use a HashMap for this case:

//result map
HashMap<BigDecimal, List<Item>> map = new HashMap<>();

for (Item item : items) {
    List<Item> itemsWithSameNumber = map.get(item.getItemNumber());
    if (itemsWithSameNumber == null) { //does not exist in map yet
        itemsWithSameNumber = new ArrayList<Item>();
        map.put(item.getItemNumber(), itemsWithSameNumber);
    }
    itemsWithSameNumber.add(item); //now add the item to the list for this key
}

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

for (BigDecimal key : map.keySet()) {
    List<Item> listOfElementsWithSameKey = map.get(key);
}
darijan
  • 9,725
  • 25
  • 38
1

You don't really specify what you mean with contain some Item with same value for itemNumber

  • Sort all by itemNumber?
  • Get all elements that match itemNumber?

Anyway, you have several ways to achieve this:

Or, if you have a reference value, use plain Java, like real men do:

BigInteger yourValue = // your desired value
List<Item> result = new ArrayList<Item>();
for (Item item : items) {
    if (item.itemNumber.equals(yourValue)) {
        item.add(item);
    }
}
Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
0

Override equals method in your Item class and in the item list call contains method which will call Item's equal method.

 class Item
    {
         Integer id;
         BigDecimal itemNumber;

          public boolean equals(Item item) {
          if (this.itemNumber.equals(item.itemNumber)) {
            return true;
        }
    // getter and setter

}

In the List check

items.contains(item)

Ankush soni
  • 1,439
  • 1
  • 15
  • 30
0
public static void main(String[] args) {
    ...
    // Group by Item by ItemNumber in a map Map<ItemNumber, List<Item>>
    list.stream().collect(groupingBy(Item::getItemNumber));
    // Count Items having sameItemNumber map Map<ItemNumber, Long>
    list.stream().collect(groupingBy(Item::getItemNumber, counting()));
    // Return the number of distinct ItemNumber
    list.stream().filter(filterByAttribute(Item::getItemNumber).count());
}

public static <T> Predicate<T> filterByAttribute(Function<? super T, ?> keyExtractor) {
    Map<Object, Boolean> seen = new ConcurrentHashMap<>();
    return (t) -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
LoVas2
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 09 '22 at 11:44
-1

to count them:

List<Item> items = new ArrayList<>();
int count=0;
int idToFind=88;
for(Item i:items){
  if(i.getId()==idToFind)
    count++;
}

to get another list:

Item itemToFind;
List newList=list.contains(itemToFind);

and override equals in your class to compare objects after big decimal

dskfdskjgds
  • 341
  • 3
  • 14