HashMap obtains all keys and values respectively
•
Java
I can't remember. I have to record it myself this time. I'll check it if I encounter it. It's a waste of time Although it's really a basic problem
1. Code implementation:
public class Main {
public static void main(String[] args){
// Scanner scanner=new Scanner(system.in);//在线笔试
HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();
map.put(1,2);
map.put(2,3);
map.put(3,4);
map.put(4,4);//<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>所有key Set<Integer> keys=map.keySet(); Iterator<Integer> iterator1=keys.iterator(); while (iterator1.hasNext()){ Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.print(iterator1.next() +","); } Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println(); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("------------------------"); //<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>所有value Collection<Integer> values=map.values(); Iterator<Integer> iterator2=values.iterator(); while (iterator2.hasNext()){ Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.print(iterator2.next()+","); } Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println(); Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println("------------------------"); //<a href="https://www.jb51.cc/tag/quchu/" target="_blank" class="keywords">去除</a>value中重复值,相同值仅仅保留<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a> Set<Integer> valuesSimple=new HashSet<Integer>(); for(int i:values){ valuesSimple.add(i); } Iterator<Integer> iterator3=valuesSimple.iterator(); while (iterator3.hasNext()){ Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.print(iterator3.next()+","); } }
}
2. Output results:
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码