一直记不住,这次必须得自己记录一下了,遇到就查,太浪费时间了....虽然真的很基础的问题....
1、code实现:
public class Main {
public static void main(String[] args){
// Scanner scanner=new Scanner(System.in);//在线笔试
HashMap map=new HashMap();
map.put(1,2);
map.put(2,3);
map.put(3,4);
map.put(4,4);
//获取所有key
Set keys=map.keySet();
Iterator iterator1=keys.iterator();
while (iterator1.hasNext()){
System.out.print(iterator1.next() +",");
}
System.out.println();
System.out.println("------------------------");
//获取所有value
Collection values=map.values();
Iterator iterator2=values.iterator();
while (iterator2.hasNext()){
System.out.print(iterator2.next()+",");
}
System.out.println();
System.out.println("------------------------");
//去除value中重复值,相同值仅仅保留一个
Set valuesSimple=new HashSet();
for(int i:values){
valuesSimple.add(i);
}
Iterator iterator3=valuesSimple.iterator();
while (iterator3.hasNext()){
System.out.print(iterator3.next()+",");
}
}
}
2、输出结果:
相关文章
总结
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250