java 使用 Map<String, Integer> 和输入顺序在数组列表中查找重复项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14376243/
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-10-31 16:03:52  来源:igfitidea点击:

Find duplicates in array list using Map<String, Integer> with input order

java

提问by Casper

Hi everyone I am trying to print all the duplicated elements, this works fine but the outputs are not in order (either from user input or from the text file). I want to print all elements with order (duplicates are not printed). How do I do that? The codes are from this Find the duplicate elements in arraylist and displayThanks @Cory Kendall for the codes.

大家好,我正在尝试打印所有重复的元素,这工作正常,但输出不按顺序(来自用户输入或文本文件)。我想按顺序打印所有元素(不打印重复项)。我怎么做?代码来自this在arraylist 中查找重复元素并显示感谢@Cory Kendall 的代码。

**********updated question: the code now works perfect with LinkedHashMap. Now I want the outputs to be printed with number bullets (ie, 1. name1 = 2 ) incrementally. Thanks

********** 更新问题:代码现在可以完美地与 LinkedHashMap 配合使用。现在我希望输出以数字项目符号(即 1. name1 = 2 )递增打印。谢谢

List<String> strings = new ArrayList<String>();
// suppose datas are entered by user incrementally or from a text files.

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

for (String str : strings) {
    if (counts.containsKey(str)) {
        counts.put(str, counts.get(str) + 1);
    } else {
        counts.put(str, 1);
    }
}

for (Map.Entry<String, Integer> entry : counts.entrySet()) {
    System.out.println(entry.getKey() + " = " + entry.getValue());
}

回答by jlordo

If you want to remember insertion order in your Map, you need to use LinkedHashMap. In your case you have to replace

如果您想记住 Map 中的插入顺序,则需要使用LinkedHashMap. 在你的情况下,你必须更换

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

with

Map<String, Integer> counts = new LinkedHashMap<String, Integer>();

回答by PermGenError

HashMapis not ordered or sorted, use LinkedHashMapif you care about insertion order, or use TreeMapif you care about natural order.

HashMap未排序或排序,LinkedHashMap如果您关心就insertion order使用,或者TreeMap如果您关心就使用natural order

回答by Yogendra Singh

public class FindDup {
    public static void main(String[] args) {
        String str[] = { "yogi", "ram", "ram", "yogi", "yogi", "yogi", "raju", "raju", "ram", "yogi", };
        Map<String, Integer> map = new HashMap<String, Integer>();
        for (String s : str) {
            if (map.containsKey(s)) {
                map.put(s, map.get(s) + 1);
            } else {
                map.put(s, 1);
            }
        }
        for (Entry<String, Integer> e : map.entrySet()) {
            System.out.println(e.getKey() + "---" + e.getValue());

        }
    }
}

回答by Kevin Bowersox

A LinkedHashMapwill retain order.

的LinkedHashMap将保留顺序。

Map<String, Integer> counts = new LinkedHashMap<String, Integer>();

About LinkedHashMap:

关于LinkedHashMap

Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).

Map接口的哈希表和链表实现,迭代顺序可预测。此实现与 HashMap 的不同之处在于它维护一个双向链表,贯穿其所有条目。这个链表定义了迭代顺序,这通常是键被插入到映射中的顺序(插入顺序)。