Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用java在数组列表中查找相同值的数目_Java - Fatal编程技术网

如何使用java在数组列表中查找相同值的数目

如何使用java在数组列表中查找相同值的数目,java,Java,我有一个数组列表,其值如下 x-tag 125 154 166 125 125 222 125 在上面的列表中,它的值为125,持续4次 我有另一个值125,我可以检查列表是否有125。 但无法找到它在列表中出现或出现的次数使用方法: 可以使用Guava库,多集合中彼此相等的元素称为同一单个元素的引用 Multiset<Integer> myMultiset = HashMultiset.create(); myMultiset.addAll(list); System.out.p

我有一个数组列表,其值如下

x-tag
125
154
166
125
125
222
125
在上面的列表中,它的值为125,持续4次 我有另一个值125,我可以检查列表是否有125。 但无法找到它在列表中出现或出现的次数使用方法:


可以使用Guava库,多集合中彼此相等的元素称为同一单个元素的引用

Multiset<Integer> myMultiset = HashMultiset.create();
myMultiset.addAll(list);
System.out.println(myMultiset.count(125)); // 4

也可以将其映射到地图中。不过,还有一些代码:

package com.stackoverflow.counter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Count {
    public Map<Integer,Integer> convertToMap(List<Integer> integerList) {
        Map<Integer,Integer> integerMap = new HashMap<>();
        for (Integer i: integerList) {
            Integer numberOfOccurrences = integerMap.get(i);
            if (numberOfOccurrences == null)
                numberOfOccurrences = 0;
            integerMap.put(i,++numberOfOccurrences);
        }
        return integerMap;
    }
    public static void main(String[] args) {
        List<Integer> integerList = new ArrayList<>();
        integerList.add(1);
        integerList.add(3);
        integerList.add(2);
        integerList.add(1);
        integerList.add(3);
        integerList.add(2);
        integerList.add(4);
        integerList.add(5);
        integerList.add(5);
        Count count = new Count();
        Map<Integer, Integer> integerMap = count.convertToMap(integerList);
        for (Integer i: integerMap.keySet()) {
            System.out.println("Number: " + i + ", found " + integerMap.get(i) + " times.");
        }
    }
}

你们试过什么?柜台是你们的朋友。顺便说一句,你的代码在哪里?添加代码怎么样?你必须检查号码是否存在?提示可以从这里流出这里是+1不要重新发明轮子!
List<Integer> list = Arrays.asList(125,154,166,125,125,222,125);

int count = Collections.frequency(list, 125);

System.out.println("total count: " + tcount);
total count: 4   
package com.stackoverflow.counter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Count {
    public Map<Integer,Integer> convertToMap(List<Integer> integerList) {
        Map<Integer,Integer> integerMap = new HashMap<>();
        for (Integer i: integerList) {
            Integer numberOfOccurrences = integerMap.get(i);
            if (numberOfOccurrences == null)
                numberOfOccurrences = 0;
            integerMap.put(i,++numberOfOccurrences);
        }
        return integerMap;
    }
    public static void main(String[] args) {
        List<Integer> integerList = new ArrayList<>();
        integerList.add(1);
        integerList.add(3);
        integerList.add(2);
        integerList.add(1);
        integerList.add(3);
        integerList.add(2);
        integerList.add(4);
        integerList.add(5);
        integerList.add(5);
        Count count = new Count();
        Map<Integer, Integer> integerMap = count.convertToMap(integerList);
        for (Integer i: integerMap.keySet()) {
            System.out.println("Number: " + i + ", found " + integerMap.get(i) + " times.");
        }
    }
}