使用HashMap或其他方法在ArrayList中求和

时间:2017-03-29 21:31:01

标签: java arraylist

我无法理解。

List<String[]> dogList = new ArrayList<String[]>();

dogList.add(new String[] { "id_tag", "breed", "rank", "nickname"});
dogList.add(new String[] { "t4639", "Akita", "First", "Marshal"});
dogList.add(new String[] { "t4638", "Akita", "First", "Tom"});
dogList.add(new String[] { "t4637", "Beagle", "First", "Eddy"});
dogList.add(new String[] { "t4636", "Beagle", "Second", "Franky"});

我想按品种和等级

输入其他ArrayList总和
List<String[]> dogTotal = new ArrayList<String[]>();

如果我迭代dogTotal

,要获得这样的输出
"Akita", "First", "2"
"Beagle", "First", "1"
"Beagle", "Second", "1"

我试图使用HashMap但没有成功。

4 个答案:

答案 0 :(得分:1)

你需要创建一个Class Dog。

一旦你有狗课,你可以这样做:

Dog d2 =  new Dog("t4639", "Akita", "First", "Marshal");
Dog d3 = new Dog( "t4638", "Akita", "First", "Tom");
Dog d4 =  new Dog("t4637", "Beagle", "First", "Eddy");
Dog d5 =new Dog("t4636", "Beagle", "Second", "Franky");

List<Dog> dogs = Arrays.asList(d2, d3, d4, d5);

然后你可以在流中使用一些漂亮的东西。下面将创建一个hashmap,您可以将其分组。见这里:Group by in Java 8 on multiple fields with aggregations

dogs.stream()
    .collect(Collectors.groupingBy(Dog::getBreed,
                 Collectors.groupingBy(Dog::getRank, Collectors.counting())));

看看这段代码有多清洁。这是使用声明式而不是命令式。

返回:

{Akita={First=2}, Beagle={Second=1, First=1}}

答案 1 :(得分:0)

实施Comparator<String[]>以检查两个元素是否相等。然后,如果还有其他具有相同品种和等级的狗(一个简单的算法将是O(n ^ 2)),你可以有两个for循环检查特定的狗,或者你可以找到一种方法来使用这个比较器系统实现的方法。

按品种和等级尝试sorting狗,然后执行for循环将其添加到dogsTotal中。这需要O(nlogn)。您还可以尝试根据数据的对称性创建自己的算法。

答案 2 :(得分:0)

嘿,您可以使用&lt; breed&gt; _&lt; rank&gt; 作为维护HashMap计数的关键。完成计数后,只需迭代哈希映射,然后使用_作为分隔符拆分密钥,以便从值中获取品种和排名以及计数值。

    List<String[]> dogList = new ArrayList<String[]>();
    dogList.add(new String[] { "id_tag", "breed", "rank", "nickname"});
    dogList.add(new String[] { "t4639", "Akita", "First", "Marshal"});
    dogList.add(new String[] { "t4638", "Akita", "First", "Tom"});
    dogList.add(new String[] { "t4637", "Beagle", "First", "Eddy"});
    dogList.add(new String[] { "t4636", "Beagle", "Second", "Franky"});
    List<String[]> dogTotal = new ArrayList<String[]>();
    HashMap<String,Integer> map = new HashMap<>();
    for(int i=1;i<dogList.size();i++){
        String key = dogList.get(i)[1]+"_"+dogList.get(i)[2];

        map.put(key,map.get(key)==null?1:map.get(key)+1);
    }
    for(Map.Entry<String,Integer> entry : map.entrySet()){
        String key = entry.getKey();
        String breedRank[] = new String[3];
        String getBackValues[] = key.split("_");
        breedRank[0] = getBackValues[0];
        breedRank[1] = getBackValues[1];
        breedRank[2] = String.valueOf(entry.getValue());
        System.out.println(breedRank[0]+ ", " + breedRank[1]+","+ breedRank[2]);
        dogTotal.add(breedRank);
    }
    System.out.println(dogTotal);

答案 3 :(得分:0)

List<String[]> dogList = new ArrayList<String[]>();

        dogList.add(new String[] { "t4639", "Akita", "First", "Marshal"});
        dogList.add(new String[] { "t4638", "Akita", "First", "Tom"});
        dogList.add(new String[] { "t4637", "Beagle", "First", "Eddy"});
        dogList.add(new String[] { "t4636", "Beagle", "Second", "Franky"});

        HashMap<String, Integer> hmap = new HashMap();

        for(String[] dog : dogList)
        {
            String type = dog[1] + ":" + dog[2];
            if(hmap.containsKey(type))
            {
                hmap.put(type, hmap.get(type) + 1);
            }
            else
            {
                hmap.put(type, 1);
            }            
        }

        for (String entry: hmap.keySet()){

            String key =entry.toString();
            String value = hmap.get(entry).toString();  
            System.out.println(key + " " + value); 
        } 
    }

enter image description here