[英]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
值的Item
的itemNumber
itemNumber
? itemNumber
全部排序? itemNumber
? itemNumber
匹配的itemNumber
? Anyway, you have several ways to achieve this: 无论如何,您有几种方法可以实现此目的:
Predicates
to filter Predicates
进行过滤 Comparators
to sort . Comparators
进行排序 。 Java8
). Java8
)。 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.