Collections集合工具类的方法:
/* * java.util.Collections是集合工具类,用来对集合进行操作。部分方法如下: * public static <T> boolean addAll(Collection<T> c,T...elements):往集合中添加一些元素。 * public static void shuffle(List<?> list):打乱顺序:打乱集合顺序*/ public class Demo01Collections { public static void main(String[] args) { ArrayList<String> list=new ArrayList<>(); //往集合中添加多个元素 /*list.add("a"); list.add("b"); list.add("c"); list.add("d"); list.add("e"); list.add("f");*/ Collections.addAll(list,"a","b","c","d","e","f");//往集合中添加一些元素 Collections.shuffle(list);//[a, e, c, f, b, d]顺序已经打乱 System.out.println(list);//[a, b, c, d, e, f] } }
//重写排序规则 @Override public int compareTo(Person o) { //return 0;//认为元素都是相同的 //自定义比较的规则,比较两个人的年龄(this,参数Person) //return this.getAge()-o.getAge();//年龄升序排序 return o.getAge()-this.getAge();//年龄降序排序 } /* * public static <T> void sort(List<T> list):将集合中元素按照默认规则排序。 * 注意事项: * sort(list<T> list)使用前提 * 被排序的集合里边存储的元素,必须实现Comparable,重写接口中的方法compareTo定义排序规则 * Comparable接口的排序规则: * 自己(this)-参数:升序 * 参数-自己(this):降序 * */ public class Demo02Sort { public static void main(String[] args) { ArrayList<Integer> list01=new ArrayList<>(); list01.add(1); list01.add(3); list01.add(2); System.out.println(list01);//[1, 3, 2] //public static <T> void sort(List<T> list):将集合中元素按照默认规则排序。 Collections.sort(list01);//默认是升序 System.out.println(list01);//[1, 2, 3] ArrayList<String> list02=new ArrayList<>(); list02.add("a"); list02.add("c"); list02.add("b"); System.out.println(list02);//[a, c, b] Collections.sort(list02); System.out.println(list02);//[a, b, c] ArrayList<Person> list03=new ArrayList<>(); list03.add(new Person("张三",18)); list03.add(new Person("李四",20)); list03.add(new Person("王五",15)); System.out.println(list03);//[Person{name='张三', age=18}, Person{name='李四', age=20}, Person{name='王五', age=15}] Collections.sort(list03); System.out.println(list03);//按年龄升序排序[Person{name='王五', age=15}, Person{name='张三', age=18}, Person{name='李四', age=20}] //return o.getAge()-this.getAge();//年龄降序排序[Person{name='李四', age=20}, Person{name='张三', age=18}, Person{name='王五', age=15}] } }
/* * public static <T> void sort(List<T> list,Comparator<? super T>):将集合中元素按照制定规则排序 * Comparator和Comparable的区别: * Comparable:自己(this)和别人(参数)比较,自己需要实现Comparable接口,重写比较规则compareTo方法。 * Comparator:相当于找一个第三方的裁判,比较两个. * Comparator排序比较规则: * o1-o2:升序 * o2-o1:降序 * */ public class Demo03Sort { public static void main(String[] args) { ArrayList<Integer> list01=new ArrayList<>(); list01.add(1); list01.add(3); list01.add(2); System.out.println(list01);//[1, 3, 2] Collections.sort(list01, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { //return o1-o2;//升序 return o2-o1;//降序[3, 2, 1] } }); System.out.println(list01); ArrayList<Student> list02=new ArrayList<>(); list02.add(new Student("a迪丽热巴",18)); list02.add(new Student("古力娜扎",20)); list02.add(new Student("杨幂",17)); list02.add(new Student("b杨幂",18)); System.out.println(list02);//[Student{name='迪丽热巴', age=18}, Student{name='古力娜扎', age=20}, Student{name='杨幂', age=17}] /*Collections.sort(list02, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { //按照年龄升序排序 return o1.getAge()-o2.getAge(); } });*/ //扩展 Collections.sort(list02, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { //按照年龄升序排序 int result=o1.getAge()-o2.getAge(); //如果两人年龄相同,再使用姓名的第一个字进行比较 if(result==0){ result=o1.getName().charAt(0)-o2.getName().charAt(0); } return result; } }); System.out.println(list02);//[Student{name='杨幂', age=17}, Student{name='迪丽热巴', age=18}, Student{name='古力娜扎', age=20}] //[Student{name='杨幂', age=17}, Student{name='a迪丽热巴', age=18}, Student{name='b杨幂', age=18}, Student{name='古力娜扎', age=20}] } }
Map集合概述:
Map中的集合,元素是成对存在的。每个元素由键和值两部分组成,通过键可以找到所对应的值。
Map中的集合称为双列集合。
/* * java.util.Map<k,v>集合 * Map集合的特点: *1.Map集合是一个双列集合,一个元素包含两个值(一个key,一个value) *2.Map集合中的元素,key和value的数据类型可以相同,也可以不同。 *3.Map集合中的元素,key是不允许重复的,value可以重复的。 *4.Map集合中的元素,key和value是一一对应*/
Map常用子类:
java.util.HashMap<k,v>集合 implements Map<k,v>接口 * HashMap集合的特点: * 1.HashMap集合底层是哈希表:查询的速度特别快。 * JDK1.8之前:数组+单向链表。 * JDK1.8之后:数组+单向链表/红黑树(链表的长度超过8):提高查询的速度。 * 2.hashMap集合是一个无序的集合,存储元素和取出元素的顺序可能不一致。 * java.util.LinkedHashMap<k,v>集合 extends HashMap<k,v>集合 * LinkedHashMap的特点: * 1.LinkedHashMap集合底层是哈希表+链表(保证迭代的顺序)。 * 2.LinkedHashMap集合是一个有序的集合,存储元素和取出元素的顺序是一致的。
Map接口中的常用方法:
public class Demo01Map { public static void main(String[] args) { //show01(); //show02(); //show03(); show04(); } /* * boolean containsKey(Object key) :如果此映射包含指定键的映射,则返回 true 。 * 包含返回true,不包含返回false*/ private static void show04() { //创建Map集合对象 Map<String,Integer> map=new HashMap<>(); map.put("赵丽颖",168); map.put("杨颖",165); map.put("林志玲",178); boolean b1 = map.containsKey("赵丽颖"); System.out.println("b1:"+b1);//b1:true boolean b2 = map.containsKey("赵颖"); System.out.println("b2:"+b2);//b2:false } /* * public V get(Object key) :返回到指定键所映射的值,或 null如果此映射包含该键的映射。 * 返回值: * key存在,返回对应的value值 * key不存在,返回null * */ private static void show03() { //创建Map集合对象 Map<String,Integer> map=new HashMap<>(); map.put("赵丽颖",168); map.put("杨颖",165); map.put("林志玲",178); Integer v1 = map.get("杨颖"); System.out.println("v1:"+v1);//v1:165 Integer v2 = map.get("迪丽热巴"); System.out.println("v2:"+v2);//v2:null } /* * public V remove(Object key) :如果存在(从可选的操作),从该地图中删除一个键的映射。 * 返回值:V * key存在,v返回被删除的值,key不存在,v返回null * */ private static void show02() { //创建Map集合对象 Map<String,Integer> map=new HashMap<>(); map.put("赵丽颖",168); map.put("杨颖",165); map.put("林志玲",178); System.out.println(map);//{林志玲=178, 赵丽颖=168, 杨颖=165} Integer v1 = map.remove("林志玲"); System.out.println("v1:"+v1);//v1:178 System.out.println(map);//{赵丽颖=168, 杨颖=165} Integer v2 = map.remove("林志颖"); System.out.println("v2:"+v2);//v2:null } /* * public V put(K key, V value) :将指定的值与该映射中的指定键相关联(可选操作)。 * 返回值:v * 存储键值对的时候,key不重复,返回值v是null * 存储键值对的时候,key重复,会使用新的value替换map中重复的value,返回被替换的value值。*/ private static void show01() { //创建Map集合对象,多态 Map<String,String> map=new HashMap<>(); String v1 = map.put("李晨", "范冰冰1"); System.out.println("v1:"+v1);//v1:null String v2 = map.put("李晨", "范冰冰2"); System.out.println("v2:"+v2);//v2:范冰冰1 System.out.println(map);//{李晨=范冰冰2} map.put("冷锋","王小云"); map.put("杨过","小龙女"); map.put("尹志平","小龙女"); System.out.println(map); } }
Map集合遍历键找值方式:
/* * Map集合的第一种遍历方式:通过键找值的方法 * Map集合中的方法: * Set<K> keySet():返回此地图中包含的键的Set视图 * 实现步骤: * 1.使用Map集合中的方法keySet(),把Map集合所有的key取出来,存储到一个Set集合当中 * 2.遍历set集合,获取Map集合当中的每一个key * 3.通过Map集合当中的get(key),通过key找到value * */ public class Demo02KeySet { public static void main(String[] args) { //创建Map集合对象 Map<String,Integer> map=new HashMap<>(); map.put("赵丽颖",168); map.put("杨颖",165); map.put("林志玲",178); // 1.使用Map集合中的方法keySet(),把Map集合所有的key取出来,存储到一个Set集合当中 Set<String> set = map.keySet(); // 2.遍历set集合,获取Map集合当中的每一个key //使用迭代器遍历Set集合 Iterator<String> it = set.iterator(); while (it.hasNext()){ String key=it.next(); //3.通过Map集合当中的get(key),通过key找到value Integer value = map.get(key); System.out.println(key+"="+value);//林志玲=178赵丽颖=168杨颖=165 } System.out.println("----------"); //使用增强for循环遍历Set集合 for (String key : set) { Integer value = map.get(key); System.out.println(key+"="+value);//林志玲=178赵丽颖=168杨颖=165 } System.out.println("----------"); //简化增强for循环遍历Set集合 for (String key : map.keySet()) { Integer value = map.get(key); System.out.println(key+"="+value);//林志玲=178赵丽颖=168杨颖=165 } } }
Entry键值对对象:
Map.Entry<k,v>:在Map接口中有一个内部接口Entry
作用:当Map集合一创建,那么就会在Map集合当中创建一个Entry对象,用来记录键与值(键值对对象,键与值的映射关系)。
Set<Map.Entry<k,v>> entrySet():把Map集合内部的多个Entry对象取出来,存储到一个Set集合中。
遍历Set集合
获取Set集合中的每一个Entry对象
Entry对象中的方法getKey()获取key
getValue()获取value。
/* * Map集合遍历的第二种方式:使用Entry对象遍历 * Map集合中的方法: * Set<Map.Entry<k,v>> entrySet()返回此映射中包含的映射关系的Set视图。 * 实现步骤: * 1.使用Map集合中的方法entrySet(),把Map集合中多个Entry对象取出来,存储到一个Set集合中 * 2.遍历Set集合,获取每一个Entry对象 * 3.使用Entry对象中的方法getKey()和getValue()获取键与值。*/ public class Demo03Entry { public static void main(String[] args) { //创建Map集合对象 Map<String,Integer> map=new HashMap<>(); map.put("赵丽颖",168); map.put("杨颖",165); map.put("林志玲",178); Set<Map.Entry<String, Integer>> set = map.entrySet(); //使用迭代器遍历set集合 Iterator<Map.Entry<String, Integer>> it = set.iterator(); while(it.hasNext()){ Map.Entry<String, Integer> entry = it.next(); String key = entry.getKey(); Integer value = entry.getValue(); System.out.println(key+"="+value);//林志玲=178赵丽颖=168杨颖=165 } System.out.println("---------"); for (Map.Entry<String, Integer> entry : set) { String key = entry.getKey(); Integer value = entry.getValue(); System.out.println(key+"="+value);//林志玲=178赵丽颖=168杨颖=165 } } }