diff --git a/src/main/java/com/pancm/basics/MapTest.java b/src/main/java/com/pancm/basics/MapTest.java index 1587d23..df4b5bc 100644 --- a/src/main/java/com/pancm/basics/MapTest.java +++ b/src/main/java/com/pancm/basics/MapTest.java @@ -1,123 +1,124 @@ -package com.pancm.basics; - -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import java.util.Map.Entry; -import java.util.TreeMap; - -/** - * -* Title: MapTest -* Description:集合map测试 -* Version:1.0.0 -* @author pancm -* @date 2017年11月16日 - */ -public class MapTest { - private static Map map = new HashMap(); - - public static Map getHashMap(){ - if(map == null || map.isEmpty()){ - map = new HashMap(); - } - return map; - } - - public static void main(String[] args) { - //普通的HashMap -// Map map=mapTest.getHashMap(); -// map.put("a", "1"); -// map.put("b", "1"); -// map.put("c", "e"); -// System.out.println(map.toString()); -// Map map1=mapTest.getHashMap(); -// System.out.println("-----"+map1.get("a")); - - //上锁的HashMap -// Map sMap=Collections.synchronizedMap(new HashMap()); - -// getMap1(); - - test1(); - test2(); - } - - - - private static void test1() { - Map map = new HashMap(); - map.put("1", "value1"); - map.put("2", "value2"); - map.put("3", "value3"); - //第一种:普遍使用,二次取值 - System.out.println("通过Map.keySet遍历key和value:"); - for (String key : map.keySet()) { - System.out.println("key= "+ key + " and value= " + map.get(key)); - } - - //第二种 使用迭代器Iterator进行遍历 - System.out.println("通过Map.entrySet使用iterator遍历key和value:"); - Iterator> it = map.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry entry = it.next(); - System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue()); - } - - //第三种:使用entrySet遍历 推荐,尤其是容量大时 - System.out.println("通过Map.entrySet遍历key和value"); - for (Map.Entry entry : map.entrySet()) { - System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue()); - } - } - - private static void test2() { - Map hashMap=new HashMap(); - hashMap.put("a", 1); - hashMap.put("c", 3); - hashMap.put("b", 2); - System.out.println("HashMap:"+hashMap); - - Map treeMap=new TreeMap(); - treeMap.put("a", 1); - treeMap.put("c", 3); - treeMap.put("b", 2); - System.out.println("TreeMap:"+treeMap); - - } - - - - - - private static void getMap1(){ - Map map1 =new HashMap(); - Map map2 =new HashMap(); - map1.put(1, 1); - map1.put(2, 2); - map1.put(3, 3); - map2.put(11, 11); - map2.put(22, 22); - map2.put(33, 33); - Map> map3 =new HashMap>(); - map3.put(1, map1); - map3.put(2, map2); - - System.out.println("map3:"+map3); - StringBuffer sb=new StringBuffer(); - for(Entry> entry:map3.entrySet()){ - sb.append(entry.getKey()); - sb.append(":"); - Map map4=entry.getValue(); - for(Entry entry1:map4.entrySet()){ - sb.append(entry1.getKey()); - sb.append("_"); - sb.append(entry1.getValue()); - sb.append(","); - } - - } - sb.deleteCharAt(sb.lastIndexOf(",")); - System.out.println("sb:"+sb); - } -} +package com.pancm.basics; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; +import java.util.TreeMap; + +/** + * +* Title: MapTest +* Description:集合map测试 +* Version:1.0.0 +* @author pancm +* @date 2017年11月16日 + */ +public class MapTest { + private static Map map = new HashMap(); + + public static Map getHashMap(){ + if(map == null || map.isEmpty()){ + map = new HashMap(); + } + return map; + } + + public static void main(String[] args) { + //普通的HashMap +// Map map=mapTest.getHashMap(); +// map.put("a", "1"); +// map.put("b", "1"); +// map.put("c", "e"); +// System.out.println(map.toString()); +// Map map1=mapTest.getHashMap(); +// System.out.println("-----"+map1.get("a")); + + //上锁的HashMap +// Map sMap=Collections.synchronizedMap(new HashMap()); + +// getMap1(); + + test1(); + test2(); + } + + + + private static void test1() { + Map map = new HashMap(); + map.put("1", "value1"); + map.put("2", "value2"); + map.put("3", "value3"); + //第一种:普遍使用,二次取值 + System.out.println("通过Map.keySet遍历key和value:"); + for (String key : map.keySet()) { + System.out.println("key= "+ key + " and value= " + map.get(key)); + } + + //第二种 使用迭代器Iterator进行遍历 + System.out.println("通过Map.entrySet使用iterator遍历key和value:"); + Iterator> it = map.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry entry = it.next(); + System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue()); + } + + //第三种:使用entrySet遍历 推荐,尤其是容量大时 + System.out.println("通过Map.entrySet遍历key和value"); + for (Map.Entry entry : map.entrySet()) { + System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue()); + } + } + + private static void test2() { + Map hashMap=new HashMap(); + hashMap.put("a", 1); + hashMap.put("c", 3); + hashMap.put("b", 2); + System.out.println("HashMap:"+hashMap); + + Map treeMap=new TreeMap(); + treeMap.put("a", 1); + treeMap.put("c", 3); + treeMap.put("b", 2); + System.out.println("TreeMap:"+treeMap); + + } + + + + + + @SuppressWarnings("unused") + private static void getMap1(){ + Map map1 =new HashMap(); + Map map2 =new HashMap(); + map1.put(1, 1); + map1.put(2, 2); + map1.put(3, 3); + map2.put(11, 11); + map2.put(22, 22); + map2.put(33, 33); + Map> map3 =new HashMap>(); + map3.put(1, map1); + map3.put(2, map2); + + System.out.println("map3:"+map3); + StringBuffer sb=new StringBuffer(); + for(Entry> entry:map3.entrySet()){ + sb.append(entry.getKey()); + sb.append(":"); + Map map4=entry.getValue(); + for(Entry entry1:map4.entrySet()){ + sb.append(entry1.getKey()); + sb.append("_"); + sb.append(entry1.getValue()); + sb.append(","); + } + + } + sb.deleteCharAt(sb.lastIndexOf(",")); + System.out.println("sb:"+sb); + } +} diff --git a/src/main/java/com/pancm/code/MapCodeTest.java b/src/main/java/com/pancm/code/MapCodeTest.java new file mode 100644 index 0000000..fa41dbc --- /dev/null +++ b/src/main/java/com/pancm/code/MapCodeTest.java @@ -0,0 +1,33 @@ +package com.pancm.code; + +import java.util.HashMap; +import java.util.Map; + +/** +* @Title: MapCodeTest +* @Description: Map源码学习相关类 +* @Version:1.0.0 +* @author pancm +* @date 2018年11月29日 +*/ +public class MapCodeTest { + + /** + * @param args + */ + public static void main(String[] args) { + test1(); + } + + + /** + * + */ + private static void test1() { + Map map =new HashMap<>(); + map.put("1", 1); + map.put("2", 2); + } +} + + diff --git a/src/main/java/com/pancm/code/package-info.java b/src/main/java/com/pancm/code/package-info.java new file mode 100644 index 0000000..07de10e --- /dev/null +++ b/src/main/java/com/pancm/code/package-info.java @@ -0,0 +1,8 @@ +/** +* @Title: package-info +* @Description: 源码相关类 +* @Version:1.0.0 +* @author pancm +* @date 2018年11月29日 +*/ +package com.pancm.code; \ No newline at end of file