java集合类专题
package com.alibaba.caseutils.test; import java.util.HashMap; import java.util.Map; import java.util.Set; public class testmap { public static void main(String[] args) { Map<String,String> heads = new HashMap<String, String> (); heads.put ( "a","第一个" ); heads.put ( "a","第一个" ); heads.put ( "b","第二个" ); heads.put ( "c","第三个" ); if(heads!=null){ System.out.println (heads.entrySet ());
#[a=第一个, b=第二个, c=第三个] System.out.println (); for(Map.Entry<String,String> head: heads.entrySet ()){ System.out.println (head); System.out.println (head.getKey ()); System.out.println (head.getValue ()); } } } }
第二种方式,思路把map集合利用map.entryset 转化成set集合,在利用set集合的 迭代器进行遍历
package com.alibaba.autotest_v2; import javax.swing.text.InternationalFormatter; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class Cmap { public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); map.put("aa", 123); map.put("aa", 123); map.put("bb", 456); //遍历 Set<Map.Entry<String,Integer>> set = map.entrySet(); Iterator<Map.Entry<String,Integer>> iterator = set.iterator(); while (iterator.hasNext()){ Map.Entry<String, Integer> keys =iterator.next(); System.out.println("key+"+keys.getKey()+";value="+keys.getValue()); } } }
key+aa;value=123
key+bb;value=456
Process finished with exit code 0
package com.alibaba.autotest_v2; import javax.swing.text.InternationalFormatter; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class Cmap { public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); map.put("aa", 123); map.put("aa", 123); map.put("bb", 456); Set<Map.Entry<String,Integer>> set=map.entrySet(); for(Map.Entry<String, Integer> ME:set){ System.out.println(ME.getKey()); System.out.println(ME.getValue()); } } }
集合类图常用总览
1.list与set 本质的区别是list存储元素list可以重复,set则不行
package com.alibaba.caseutils.test; import java.sql.Array; import java.util.*; public class testmap { public static void main(String[] args) { Set<String> set= new HashSet<String> (); set.add ( "褚志强" ); set.add ( "阿强" ); set.add ( "褚志强" ); set.add ( "安命" ); Iterator<String> iterator=set.iterator (); while (iterator.hasNext ()){ String ele=iterator.next (); System.out.println (ele ); } } }
褚志强
阿强
安命
jiapengchu
posted on 2022-03-30 19:08 jiapengchu 阅读(24) 评论(0) 编辑 收藏 举报