集合会增加出现次数而不是添加重复次数

时间:2017-01-21 11:50:12

标签: java collections

是否存在任何仅存储唯一字符串的集合,但是计算此字符串的添加次数? 所以每当我尝试再次添加相同的字符串/项目时,项目数量保持不变,但给定项目的出现次数会增加?

4 个答案:

答案 0 :(得分:1)

据我所知,集合中没有这样的构建,但你可以通过使用Map集合来实现这一点:

Map<String, Integer> map = new HashMap<>();
String sample = "foo";
if (map.containsKey(sample))
    map.put(sample, map.get(sample) + 1);

您也可以使用外部库中的解决方案,例如来自Multiset的{​​{1}}:

Google Guava

带输出:

Multiset<String> multiset = HashMultiset.create();
String test = "foo";
multiset.add(test);
multiset.add(test);
multiset.add(test);
System.out.println(multiset.count(test));

希望它有所帮助。

答案 1 :(得分:1)

您可以使用<div class="d1">1 <!-- the topmost --> <div class="d2">2 <div class="d3">3 <!-- the innermost --> </div> </div> </div>并围绕它包装一些代码:

HashMap

然后你可以像这样使用它:

public class CounterMap<K> {

    private final Map<K, Integer> internalMap = new HashMap<K, Integer>();

    public void increment(K key) {
        initKeyIfNew(key);
        Integer oldValue = internalMap.get(key);
        Integer newValue = oldValue + 1;

        internalMap.put(key, newValue);
    }

    public int getCount(K key) {
        initKeyIfNew(key);
        return internalMap.get(key);
    }

    private void initKeyIfNew(K key) {
        if (internalMap.get(key) == null) {
            internalMap.put(key, 0);
        }
    }

}

答案 2 :(得分:1)

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class UniqueStringCount {

    public static void main(String[] args) {

        boolean takeUserInput = true;
        HashMap<String, Integer> uniqueStringMap = new HashMap<>();
        int counter = 0;

        System.out.println("Welcome. To close the program type exit.");
        System.out.println();

        do {
            Scanner scan = new Scanner(System.in);

            System.out.println("Enter the unique string");

            String userInput = scan.next();

            if(userInput.equalsIgnoreCase("exit")) {
                takeUserInput = false;
                scan.close();
            }

            System.out.println();

            if(!userInput.equalsIgnoreCase("exit")) {

                if(uniqueStringMap.containsKey(userInput)) {
                    counter = uniqueStringMap.get(userInput);
                    uniqueStringMap.put(userInput, ++counter);
                    continue;
                }

                counter = 0;
                uniqueStringMap.put(userInput, ++counter);
            }

        } while(takeUserInput);

        if(!uniqueStringMap.isEmpty()) {
            for(Map.Entry<String, Integer> entry : uniqueStringMap.entrySet()) {
                System.out.println("String " + entry.getKey() + " was added " + entry.getValue() + " times.");
                System.out.println();
            }
            System.out.println("Bye bye.");
        }
    }
}

答案 3 :(得分:0)

使用Java 8:

 Map<String, Integer> map = new HashMap<>();

添加字符串时,请执行:

 map.merge(s, 1, Integer::sum);

这样做是添加字符串s并将值设置为1(如果它还没有)。如果它已经存在,那么它将获取当前值和你要添加的新值(再次为1)并将它们相加,然后将其放回到地图中。