如何在HashMap中保留重复项

huangapple go评论76阅读模式
英文:

How to keep duplicates in HashMap

问题

我不需要移除重复项..
HashMap<Integer, String> hm = new HashMap();
hm.put(3, "aba");
hm.put(4, "abab");
hm.put(3, "aba");
hm.put(3, "aba");
System.out.println(hm);

是否有办法在 HashMap 中保留重复项?如果没有,如果我想要保留重复项,我应该做什么(应该使用哪个接口或类)?

英文:

I need not to remove duplicates ..

HashMap&lt;Integer,String&gt; hm=new HashMap();
hm.put(3,&quot;aba&quot;);
hm.put(4,&quot;abab&quot;);
hm.put(3,&quot;aba&quot;);
hm.put(3,&quot;aba&quot;);
System.out.println(hm);

Is there any way to keep duplicates in HashMap ? If not what should I do(what interface or class should i use ) if I want to do so?

答案1

得分: 1

一个 HashMap 不能拥有多个具有相同 key 的元素。尝试使用一个 ArrayList 代替:

public class Item implements Comparable<Item> {
    private int num;
    private String name;
    
    public Item(int num, String name) {
        this.setNum(num);
        this.setName(name);
    }
    
    public int getNum() {
        return num;
    }
    
    public void setNum(int num) {
        this.num = num;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    @Override
    public String toString() {
        return "Item [num=" + num + ", name=" + name + "]";
    }
    
    @Override
    public int compareTo(Item other) {
        return this.num - other.num;
    }
}
List<Item> list = new ArrayList<>();
list.add(new Item(3, "aba"));
list.add(new Item(4, "abab"));
list.add(new Item(3, "aba"));
list.add(new Item(3, "aba"));
Collections.sort(list);
System.out.println(list.toString());

输出:

[Item [num=3, name=aba], Item [num=3, name=aba], Item [num=3, name=aba], Item [num=4, name=abab]]
英文:

A HashMap cannot have multiple elements with the same key. Try using an ArrayList instead:

public class Item implements Comparable&lt;Item&gt;{
	private int num;
	private String name;
	public Item(int num, String name) {
		this.setNum(num);
		this.setName(name);
	}
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return &quot;Item [num=&quot; + num + &quot;, name=&quot; + name + &quot;]&quot;;
	}
	@Override
	public int compareTo(Item other) {
		return this.num-other.num;
	}
}
List&lt;Item&gt; list=new ArrayList&lt;&gt;();
list.add(new Item(3,&quot;aba&quot;));
list.add(new Item(4,&quot;abab&quot;));
list.add(new Item(3,&quot;aba&quot;));
list.add(new Item(3,&quot;aba&quot;));
Collections.sort(list);
System.out.println(list.toString());

Output:

[Item [num=3, name=aba], Item [num=3, name=aba], Item [num=3, name=aba], Item [num=4, name=abab]]

huangapple
  • 本文由 发表于 2020年9月4日 19:27:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63740278.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定