简体   繁体   English

如何计算列表中其属性之一具有相同值的自定义对象的数量

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

I am programming in java.我正在 java 中编程。 Say I have an custom object Item假设我有一个自定义 object 项目

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 .现在,了解 java 的最佳方法是什么, Items列表包含一些与Items具有相同值的itemNumber

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: 您可以在这种情况下使用HashMap

//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);
}

You don't really specify what you mean with contain some Item with same value for itemNumber 您实际上并没有明确指定包含一些具有相同itemNumber值的ItemitemNumber

  • Sort all by itemNumber ? itemNumber全部排序?
  • Get all elements that match itemNumber ? 获取所有与itemNumber匹配的itemNumber

Anyway, you have several ways to achieve this: 无论如何,您有几种方法可以实现此目的:

Or, if you have a reference value, use plain Java , like real men do: 或者,如果您有参考价值,请像普通人一样使用纯Java

BigInteger yourValue = // your desired value
List<Item> result = new ArrayList<Item>();
for (Item item : items) {
    if (item.itemNumber.equals(yourValue)) {
        item.add(item);
    }
}

Override equals method in your Item class and in the item list call contains method which will call Item's equal method. 在您的Item类中覆盖equals方法,并且在项目列表中,调用包含将调用Item的equal方法的方法。

 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) items.contains(item)

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;
}

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 并覆盖类中的equals以比较大十进制后的对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查自定义对象列表对于 Java 8 中的属性是否具有相同的值 - Check whether list of custom objects have same value for a property in Java 8 如何从自定义对象列表中获取一个属性 - How to get one attribute from list of custom objects 如何使用AssertJ检查一个属性值计数的对象列表? - How to check list of objects one property value count using AssertJ? 具有最大值的对象列表 - List of objects which have a max value 如何创建一个包含许多相同对象的构造函数,但如果一个对象为空则忽略它? - How can I make a constructor that contains a number of the same objects but if one its empty to ignore it? 如果对象具有相同的 id,则遍历列表并添加属性 - Going through a list and add an attribute if objects have same id 检查两个对象列表是否具有相同的属性值 - check if two lists of objects have the same value of an attribute Java Arraylist同时更改所有对象中的一个属性值 - Java Arraylist change one attribute value in all objects at the same time Java - 映射包含对象列表的两个对象(具有不同命名但功能相同的对象) - Java - Map two objects containing List of objects(objects which have different naming but same function) 在java中计算具有相同哈希值的列表的值的数量? - Count number of values of a list having same hash value in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM