Java 计算我的数组列表中的项目数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3704194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 03:41:06  来源:igfitidea点击:

Count the number of items in my array list

java

提问by user442471

I want to count the number of itemids in my array, can i get an example of how i would go about adding this to my code. code below;

我想计算我的数组中的 itemid 的数量,我能得到一个例子来说明我将如何将它添加到我的代码中。代码如下;

if (value != null && !value.isEmpty()) {
    Set set = value.keySet();
    Object[] key = set.toArray();
    Arrays.sort(key);

    for (int i = 0; i < key.length; i++) {
        ArrayList list = (ArrayList) value.get((String) key[i]);

        if (list != null && !list.isEmpty()) {
            Iterator iter = list.iterator();
            double itemValue = 0;
            String itemId = "";

            while (iter.hasNext()) {
                Propertyunbuf p = (Propertyunbuf) iter.next();
                if (p != null) {
                    itemValue = itemValue + p.getItemValue().doubleValue();
                    itemId = p.getItemId();
                }

                buf2.append(NL);
                buf2.append("                  " + itemId);

            }

            double amount = itemValue;
            totalAmount += amount;
        }
    }
}

采纳答案by Mark Peters

The number of itemIds in your list will be the same as the number of elements in your list:

itemId列表中s的数量将与列表中元素的数量相同:

int itemCount = list.size();

However, if you're looking to count the number of unique itemIds (per @pst) then you should use a set to keep track of them.

但是,如果您要计算唯一 itemId 的数量(每个 @pst),那么您应该使用一组来跟踪它们。

Set<String> itemIds = new HashSet<String>();

//...
itemId = p.getItemId();
itemIds.add(itemId);

//... later ...
int uniqueItemIdCount = itemIds.size();

回答by Freiheit

Outside of your loop create an int:

在循环之外创建一个 int:

int numberOfItemIds = 0;
for (int i = 0; i < key.length; i++) {

Then in the loop, increment it:

然后在循环中,增加它:

itemId = p.getItemId();
numberOfItemIds++;

回答by BigMac66

The only thing I would add to Mark Peters solution is that you don't need to iterate over the ArrayList - you should be able to use the addAll(Collection) method on the Set. You only need to iterate over the entire list to do the summations.

我要添加到 Mark Peters 解决方案的唯一一件事是您不需要迭代 ArrayList - 您应该能够在 Set 上使用 addAll(Collection) 方法。您只需要遍历整个列表即可进行求和。

回答by anand.Citizen

You want to count the number of itemids in your array. Simply use:

您想计算数组中 itemid 的数量。只需使用:

int counter=list.size();

Less code increases efficiency. Do not re-invent the wheel...

更少的代码提高了效率。不要重新发明轮子...

回答by Alex

You can get the number of elements in the list by calling list.size(), however some of the elements may be duplicates or null(if your list implementation allows null).

您可以通过调用 获取列表中的元素数量list.size(),但是某些元素可能是重复的或null(如果您的列表实现允许null)。

If you want the number of unique items and your items implement equalsand hashCodecorrectly you can put them all in a set and call sizeon that, like this:

如果您想要唯一项目的数量并且您的项目正确执行equalshashCode您可以将它们全部放在一个集合中并调用size它,如下所示:

new HashSet<>(list).size()

If you want the number of items with a distinct itemIdyou can do this:

如果您想要具有不同的项目数,itemId您可以这样做:

list.stream().map(i -> i.itemId).distinct().count()

Assuming that the type of itemIdcorrectly implements equalsand hashCode(which Stringin the question does, unless you want to do something like ignore case, in which case you could do map(i -> i.itemId.toLowerCase())).

假设类型itemId正确实现equalshashCodeString在问题中确实如此,除非您想执行诸如忽略大小写之类的操作,在这种情况下您可以执行map(i -> i.itemId.toLowerCase()))。

You may need to handle nullelements by either filtering them before the call to map: filter(Objects::nonNull)or by providing a default itemId for them in the mapcall: map(i -> i == null ? null : i.itemId).

您可能需要null通过在调用map:之前过滤它们来处理元素,或者通过在调用中filter(Objects::nonNull)为它们提供默认 itemId来处理元素mapmap(i -> i == null ? null : i.itemId)