1. Map 1.1 特点:元素以键值对的形式无序添加,键不能重复,值可以重复 它没有继承Collection接口 1.2 遍历 1.2.1 先取出保存的所有键Set,再遍历Set即可(2种) 1.2.2 先取出保存的一切Entry的Set,再遍历此Set即可 (重要点)
2.HashMap与HashTable之间的区别 同步既排队 线程安全的 hashtable 键不可以为null,值也不能为null 异步 非安全的 hashmap 键可以为null,值也可以为null 3. 其它 3.1 Collections:提供一组静态操作工具Collection集合 3.2 Arrays:工具类,提供了一组静态方法操作数组
一.定义一个Map集合,key和value不规定类型,任意放入数据,使用keySet()和 entrySet()两种方式遍历出来Map集合的数据
Map m = new HashMap(); m.put("1", 1); m.put("1", 2); m.put("4", 3); m.put("r", 9); m.put("a", 0); Set s = m.keySet() for (Object o : s) { Object o1 = m.get(o); System.out.println(o "=" o1); //o 键 o1 值 } System.out.println("————————————————"); Set s2 = m.entrySet(); for (Object object : s2) { System.out.println(object); //System.out.println(object.); } System.out.println("------____________");
二.定义一个Map集合,key和value不规定类型,随意放入数据,单独获得所有数据 value的数据
Map<Object, Object> m3 = new HashMap<Object, Object>(); m3.put("1", 1); m3.put("1", 2); m3.put("4", 3); m3.put("r", 9); m3.put("a", 0); Set s3 = m.keySet() for (Object o : s3) { Object o1 = m.get(o); System.out.println(o "=" o1); //o 键 o1 值 } System.out.println("————————————————"); Set s31 = m.entrySet(); for (Object object : s2) { System.out.println(object); System.out.println(); } System.out.println("zy2--_____________"); Map m2 = new HashMap(); m2.put("12", 1); m2.put("1e", 2); m2.put("44", 3); m2.put("rf", 9); m2.put("ab", 0); Collection values = m2.values();//获得一切value for (Object object : values) { System.out.println("values=" object ); }
三.找出Collections和Arrays五种常用方法的作用 1.常用方法 public static boolean equals(int[] a, int[] a2)? 判断两个数组是否相等 public static String toString(int[] a)? ? ? ? ? ? ? ? ?输出数组信息 public static void fill(int[] a, int val)? ? ? ? ? ? ? ? ?将指定值填充到数组中 public static void sort(int[] a)? ? ? ? ? ? ? ? ? ? ? ? ? 排序数组 public static int binarySearch(int[] a, int key)? 二分法检索排序后数组指定的值 2.测试 public void test1() { int[] arr1 = new int[]{1, 3, 2, 4}; int[] arr2 = new int[]{1, 2, 3, 4}; boolean isEquals = Arrays.equals(arr1, arr2); System.out.println(isEquals); } public void test2() { int[] arr1 = new int[]{1, 3, 2, 4}; System.out.println(Arrays.toString(arr1)); } @Test public void test3() { int[] arr1 = new int[]{1, 3, 2, 4}; Arrays.fill(arr1, 10);; System.out.println(Arrays.toString(arr1)); } public void test4() { int[] arr1 = new int[]{1, 3, 2, 4}; Arrays.sort(arr1); System.out.println(Arrays.toString(arr1)); } public void test5() { int[] arr1 = new int[Collections 1.常用方法 排序操作
public static void reverse(List<?> list) 反转list中元素的顺序 public static void shuffle(List<?> list) 对list集合元素随机排序 public static <T extends Comparable<? super T>> void sort(List<T> list)? ?指定根据元素的自然排序list收集元素按升序排序 public static <T> void sort(List<T> list, Comparator<? super T> c) 根据指定的comparator顺序正确list收集元素进行排序 public static void swap(List<?> list, int i, int j) 将指定list集中的i处元素与j处元素交互 查找,替换
public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)? 根据元素的自然排序,返回给定集中的最大元素 public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp)? ?根据comparator指定的顺序返回给定集合中的最大元素 public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll) public static <T> T min(Collection<? extends T> coll, Comparator<? super T> comp) public static int frequency(Collection<?> c, O 四.Map集合3层嵌套使用(2Map 1List) List<Student> Map<String,List> Map<String,Map>
List<Student> ls = new ArrayList<Student>(); ls.add(new Student("1", );//名 分数 ls.add(new Student("12", 12)); List<Student> ls01 = new ArrayList<Student>(); ls01.add(new Student("01", 12)); ls01.add(new Student("012", 122)); ist<Student> ls2 = new ArrayList<Student>();
ls2.add(new Student("2", 12));
ls2.add(new Student("22", 12));
List<Student> ls201 = new ArrayList<Student>();
ls201.add(new Student("02", 120));
ls201.add(new Student("022", 172));
List<Student> ls3 = new ArrayList<Student>();
ls3.add(new Student("3", 12));
ls3.add(new Student("32", 12));
List<Student> ls301 = new ArrayList<Student>();
ls301.add(new Student("03", 90));
ls301.add(new Student("032", 93));
Map<String, List<Student>> map = new HashMap<String, List<Student>>();
Map<String, List<Student>> map01 = new HashMap<String, List<Student>>();
Map<String, List<Student>> map02 = new HashMap<String, List<Student>>();
//装箱 一一对应
map.put("T110", ls);
map.put("T111", ls01);
map01.put("T206", ls2);
map01.put("T222", ls201);
map02.put("T230", ls3);
map02.put("T231", ls301);
Map<String, Map<String, List<Student>>> map2 = new HashMap<>();
map2.put("卓京初中部", map);//将对应的集合加到里面
map2.put("卓京高中部", map01);//
map2.put("卓京大学部", map02);//
Set<Entry<String, Map<String, List<Student>>>> entrySet = map2.entrySet();
for (Entry<String, Map<String, List<Student>>> entry : entrySet) {
System.out.println(entry);
}
运行效果
卓京高中部={T206=[Student [sname=2, sscore=12], Student [sname=22, sscore=12]], T222=[Student [sname=02, sscore=120], Student [sname=022, sscore=172]]} 卓京大学部={T231=[Student [sname=03, sscore=90], Student [sname=032, sscore=93]], T230=[Student [sname=3, sscore=12], Student [sname=32, sscore=12]]} 卓京初中部={T110=[Student [sname=1, sscore=12], Student [sname=12, sscore=12]], T111=[Student [sname=01, sscore=12], Student [sname=012, sscore=122]]}