集合
集合概念
对象的容器,实现了对对象常用的操作,类似数组功能。
集合和数组的区别
数组长度固定,集合长度不固定
数组可以存储基本类型和引用类型,集合只能存储引用类型
Collection体系集合
Interface:
Collection:该体系结构的根接口,代表一组对象,成为“集合”
List接口特点:有序、有下标、元素可重复
Set接口特点:无序、无下标、元素不能重复
Collection父接口
Collection使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 package com.collection;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;public class Demo01 { public static void main (String[] args) { Collection collection = new ArrayList (); collection.add("苹果" ); collection.add("西瓜" ); collection.add("榴莲" ); System.out.println("元素个数:" +collection.size()); System.out.println(collection); System.out.println("================3.1使用增强for===================" ); for (Object object : collection){ System.out.println(object); } System.out.println("================3.2使用迭代器===================" ); Iterator it = collection.iterator(); while (it.hasNext()){ String s = (String)it.next(); System.out.println(s); it.remove(); } System.out.println("元素个数:" +collection.size()); System.out.println(collection.contains("西瓜" )); System.out.println(collection.isEmpty()); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 package com.collection;import com.collection.Student;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;public class Demo02 { public static void main (String[] args) { Collection collection = new ArrayList (); Student s1 = new Student ("张三" ,20 ); Student s2 = new Student ("李四" ,18 ); Student s3 = new Student ("王五" ,22 ); collection.add(s1); collection.add(s2); collection.add(s3); System.out.println("元素个数:" +collection.size()); System.out.println(collection.toString()); System.out.println("===============增强for==================" ); for (Object object : collection){ Student s = (Student)object; System.out.println(s.toString()); } System.out.println("===============迭代器==================" ); Iterator it = collection.iterator(); while (it.hasNext()){ Student s = (Student) it.next(); System.out.println(s.toString()); } System.out.println(collection.contains(new Student ("张三" ,0 ))); System.out.println(collection.isEmpty()); } }
List集合
List子接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 package com.collection;import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.ListIterator;public class Demo03 { public static void main (String[] args) { List list = new ArrayList <>(); list.add("苹果" ); list.add("小米" ); list.add(0 ,"华为" ); System.out.println("元素个数:" +list.size()); System.out.println(list.toString()); System.out.println("==============3.1使用for遍历==============" ); for (int i = 0 ; i < list.size(); i++) { System.out.println(list.get(i)); } System.out.println("==============3.2使用增强for==============" ); for (Object object : list){ System.out.println(object); } Iterator it = list.iterator(); while (it.hasNext()){ System.out.println(it.next()); } ListIterator lit = list.listIterator(); System.out.println("================3.4使用列表迭代器从前往后==================" ); while (lit.hasNext()){ System.out.println(lit.nextIndex()+":" +lit.next()); } System.out.println("================3.4使用列表迭代器从后往前==================" ); while (lit.hasPrevious()){ System.out.println(lit.previousIndex()+":" +lit.previous()); } System.out.println(list.contains("苹果" )); System.out.println(list.isEmpty()); System.out.println(list.indexOf("华为" )); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 package com.collection;import java.util.ArrayList;import java.util.List;public class Demo04 { public static void main (String[] args) { List list = new ArrayList (); list.add(20 ); list.add(30 ); list.add(40 ); list.add(50 ); list.add(60 ); System.out.println("元素个数:" +list.size()); System.out.println(list.toString()); List subList = list.subList(1 ,3 ); System.out.println(subList.toString()); } }
List实现类
ArrayList使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 package com.collection;import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;import java.util.ArrayList;import java.util.Iterator;import java.util.ListIterator;public class Demo05 { public static void main (String[] args) { ArrayList arrayList = new ArrayList <>(); Student s1 = new Student ("刘德华" ,20 ); Student s2 = new Student ("郭富城" ,20 ); Student s3 = new Student ("梁朝伟" ,20 ); arrayList.add(s1); arrayList.add(s2); arrayList.add(s3); System.out.println("元素个数" +arrayList.size()); System.out.println(arrayList.toString()); System.out.println("==========3.1使用迭代器==============" ); Iterator it = arrayList.iterator(); while (it.hasNext()){ Student s = (Student) it.next(); System.out.println(s.toString() ); } ListIterator lit = arrayList.listIterator(); System.out.println("==========3.2列表迭代器==============" ); while (lit.hasNext()){ Student s = (Student) lit.next(); System.out.println(s.toString()); } System.out.println("==========3.2列表迭代器逆序==============" ); while (lit.hasPrevious()){ Student s = (Student) lit.previous(); System.out.println(s.toString()); } System.out.println(arrayList.contains(new Student ("梁朝伟" ,20 ))); System.out.println(arrayList.isEmpty()); System.out.println(arrayList.indexOf(new Student ("梁朝伟" ,20 ))); } }
ArrayList源码分析
默认容量:DEFAULT_CAPACITY = 10;
注意:没有向集合中添加元素时,容量为0;添加一个元素之后,容量为10
每次扩容大小是原来的1.5倍
存放元素的数组:elementData
实际元素个数:size
add()添加元素
1 2 3 4 5 public boolean add (E e) { ensureCapacityInternal(size + 1 ); elementData[size++] = e; return true ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 private static int calculateCapacity (Object[] elementData, int minCapacity) { if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { return Math.max(DEFAULT_CAPACITY, minCapacity); } return minCapacity; } private void ensureCapacityInternal (int minCapacity) { ensureExplicitCapacity(calculateCapacity(elementData, minCapacity)); } private void ensureExplicitCapacity (int minCapacity) { modCount++; if (minCapacity - elementData.length > 0 ) grow(minCapacity); }
1 2 3 4 5 6 7 8 9 10 11 private void grow (int minCapacity) { int oldCapacity = elementData.length; int newCapacity = oldCapacity + (oldCapacity >> 1 ); if (newCapacity - minCapacity < 0 ) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0 ) newCapacity = hugeCapacity(minCapacity); elementData = Arrays.copyOf(elementData, newCapacity); }
Vector使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 package com.collection.vector;import java.util.Enumeration;import java.util.Vector;public class Demo01 { public static void main (String[] args) { Vector vector = new Vector <>(); vector.add("草莓" ); vector.add("芒果" ); vector.add("西瓜" ); System.out.println("元素个数:" +vector.size()); Enumeration en = vector.elements(); while (en.hasMoreElements()){ String o = (String)en.nextElement(); System.out.println(o.toString()); } System.out.println(vector.contains("西瓜" ));; System.out.println(vector.isEmpty()); } }
LinkedList使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 package com.collection.linkedlist;import com.collection.Student;import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;import java.util.Iterator;import java.util.LinkedList;import java.util.ListIterator;public class Demo01 { public static void main (String[] args) { LinkedList linkedList = new LinkedList <>(); Student s1 = new Student ("刘德华" ,20 ); Student s2 = new Student ("郭富城" ,20 ); Student s3 = new Student ("梁朝伟" ,20 ); linkedList.add(s1); linkedList.add(s2); linkedList.add(s3); System.out.println("元素个数:" +linkedList.size()); System.out.println(linkedList.toString()); linkedList.remove(new Student ("刘德华" ,20 )); System.out.println("删除之后:" +linkedList.size()); System.out.println("===========for=============" ); for (int i = 0 ; i < linkedList.size(); i++) { System.out.println(linkedList.get(i)); } System.out.println("===========增强for=============" ); for (Object object :linkedList){ Student s = (Student)(object); System.out.println(s.toString()); } System.out.println("===========使用迭代器=============" ); Iterator it = linkedList.iterator(); while (it.hasNext()){ Student s = (Student) it.next(); System.out.println(s.toString()); } System.out.println("===========使用列表迭代器=============" ); ListIterator lit = linkedList.listIterator(); while (lit.hasNext()){ Student s = (Student) lit.next(); System.out.println(s.toString()); } System.out.println(linkedList.contains(s1)); System.out.println(linkedList.isEmpty()); System.out.println(linkedList.indexOf(s3)); } }
源码分析
int size:集合的大小
Node first;链表的头节点
Node last; 链表的尾节点
1 2 3 4 5 6 7 8 9 10 11 private void linkFirst (E e) { final Node<E> f = first; final Node<E> newNode = new Node <>(null , e, f); first = newNode; if (f == null ) last = newNode; else f.prev = newNode; size++; modCount++; }
1 2 3 4 5 6 7 8 9 10 11 private static class Node <E> { E item; Node<E> next; Node<E> prev; Node(Node<E> prev, E element, Node<E> next) { this .item = element; this .next = next; this .prev = prev; } }
不同结构实现方式
Set集合
Set子接口
特点:无序、无下标、元素不可重复
方法:全部继承自Collection中的方法
Set接口使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 package com.set;import java.util.HashSet;import java.util.Iterator;import java.util.Set;public class Demo01 { public static void main (String[] args) { Set<String> set = new HashSet <>(); set.add("苹果" ); set.add("华为" ); set.add("小米" ); System.out.println("数据个数:" +set.size()); System.out.println(set.toString()); System.out.println("============增强for===============" ); for (String string :set){ System.out.println(string); } System.out.println("============使用迭代器===============" ); Iterator<String> it = set.iterator(); while (it.hasNext()){ System.out.println(it.next()); } System.out.println(set.contains("华为" )); System.out.println(set.isEmpty()); } }
Set实现类
HashSet【重点】
基于HashCode计算元素存放位置
当存入元素的哈希码相同时,会调用equals进行确认,如结果为true,则拒绝后者存入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 package com.set;import java.util.HashSet;import java.util.Iterator;public class Demo02 { public static void main (String[] args) { HashSet<String> hashSet = new HashSet <>(); hashSet.add("刘德华" ); hashSet.add("梁朝伟" ); hashSet.add("林志玲" ); hashSet.add("周润发" ); System.out.println("元素个数:" +hashSet.size()); System.out.println(hashSet.toString()); System.out.println("============增强for===============" ); for (String string : hashSet){ System.out.println(string); } System.out.println("============使用迭代器===============" ); Iterator<String> it = hashSet.iterator(); while (it.hasNext()){ System.out.println(it.next()); } System.out.println(hashSet.contains("郭富城" )); System.out.println(hashSet.isEmpty()); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 package com.set;import java.util.HashSet;import java.util.Iterator;import java.util.Objects;public class Demo03 { public static void main (String[] args) { HashSet<Person> persons = new HashSet <>(); Person p1 = new Person ("刘德华" , 20 ); Person p2 = new Person ("林志玲" , 22 ); Person p3 = new Person ("梁朝伟" , 25 ); persons.add(p1); persons.add(p2); persons.add(p3); persons.add(new Person ("梁朝伟" , 25 )); System.out.println("元素个数:" +persons.size()); System.out.println(persons.toString()); persons.remove(new Person ("梁朝伟" , 25 )); System.out.println("删除之后:" +persons.size()); System.out.println("============增强for===============" ); for (Person person : persons){ System.out.println(person.toString()); } System.out.println("============使用迭代器===============" ); Iterator<Person> it = persons.iterator(); while (it.hasNext()){ System.out.println(it.next()); } System.out.println(persons.contains(p1)); System.out.println(persons.isEmpty()); } } public class Person { private String name; private int age; public Person () { } public Person (String name, int age) { this .name = name; this .age = age; } public String getName () { return name; } public void setName (String name) { this .name = name; } public int getAge () { return age; } public void setAge (int age) { this .age = age; } @Override public String toString () { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}' ; } @Override public boolean equals (Object o) { if (this == o) return true ; if (o == null || getClass() != o.getClass()) return false ; Person person = (Person) o; return age == person.age && Objects.equals(name, person.name); } @Override public int hashCode () { return Objects.hash(name, age); } } */
TreeSet
基于排列数序实现元素不重复
实现了SortedSet接口,对集合元素自动排序
元素对象的类型必须实现Comparable接口,指定排序规则
通过CompareTo方法确定是否为重复元素
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 package com.set;import java.util.Iterator;import java.util.TreeSet;public class Demo04 { public static void main (String[] args) { TreeSet<String> treeSet = new TreeSet <>(); treeSet.add("xyz" ); treeSet.add("abc" ); treeSet.add("hello" ); System.out.println("元素个数:" +treeSet.size()); System.out.println(treeSet.toString()); treeSet.remove("xyz" ); System.out.println("删除之后:" +treeSet.size()); System.out.println("============增强for===============" ); for (String string : treeSet){ System.out.println(string); } System.out.println("============使用迭代器===============" ); Iterator<String> it = treeSet.iterator(); while (it.hasNext()){ System.out.println(it.next()); } System.out.println(treeSet.contains("郭富城" )); System.out.println(treeSet.isEmpty()); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 package com.set;import java.util.Iterator;import java.util.TreeSet;public class Demo05 { public static void main (String[] args) { TreeSet<Person> persons = new TreeSet <>(); Person p1 = new Person ("xyz" , 20 ); Person p2 = new Person ("abc" , 22 ); Person p3 = new Person ("zhangsan" , 25 ); persons.add(p1); persons.add(p2); persons.add(p3); System.out.println("元素个数:" +persons.size()); System.out.println(persons.toString()); System.out.println("============增强for===============" ); for (Person person : persons){ System.out.println(person.toString()); } System.out.println("============使用迭代器===============" ); Iterator<Person> it = persons.iterator(); while (it.hasNext()){ System.out.println(it.next()); } System.out.println(persons.contains(p1)); System.out.println(persons.isEmpty()); } } public class Person implements Comparable <Person>{ private String name; private int age; public Person () { } public Person (String name, int age) { this .name = name; this .age = age; } public String getName () { return name; } public void setName (String name) { this .name = name; } public int getAge () { return age; } public void setAge (int age) { this .age = age; } @Override public String toString () { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}' ; } @Override public boolean equals (Object o) { if (this == o) return true ; if (o == null || getClass() != o.getClass()) return false ; Person person = (Person) o; return age == person.age && Objects.equals(name, person.name); } @Override public int hashCode () { return Objects.hash(name, age); } @Override public int compareTo (Person o) { int n1 = this .getName().compareTo(o.getName()); int n2 = this .age-o.getAge(); return n1==0 ?n2:n1; } }
Comparator:实现定制比较(比较器)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 package com.set;import java.util.Comparator;import java.util.TreeSet;public class Demo06 { public static void main (String[] args) { TreeSet<Person> persons = new TreeSet <>(new Comparator <Person>() { @Override public int compare (Person o1, Person o2) { int n1 = o1.getAge()- o2.getAge(); int n2 = o1.getName().compareTo(o2.getName()); return n1==0 ?n2:n1; } }); Person p1 = new Person ("xyz" , 20 ); Person p2 = new Person ("abc" , 22 ); Person p3 = new Person ("zhangsan" , 25 ); persons.add(p1); persons.add(p2); persons.add(p3); System.out.println(persons.toString()); } }
TreeSet案例(按照字符串长度排序)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 package com.set;import java.util.Comparator;import java.util.TreeSet;public class Demo07 { public static void main (String[] args) { TreeSet<String> treeSet = new TreeSet <>(new Comparator <String>() { @Override public int compare (String o1, String o2) { int n1 = o1.length()-o2.length(); int n2 = o1.compareTo(o2); return n1==0 ?n2:n1; } }); treeSet.add("helloworld" ); treeSet.add("pingguo" ); treeSet.add("lisi" ); treeSet.add("zhangsan" ); treeSet.add("beijing" ); treeSet.add("cat" ); treeSet.add("nanjing" ); System.out.println(treeSet.toString()); } }
Map集合
Map集合概述
Map父接口:
Map接口使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 package com.collection.map;import java.util.HashMap;import java.util.Map;import java.util.Set;public class Demo01 { public static void main (String[] args) { Map<String,String> map = new HashMap <>(); map.put("cn" ,"中国" ); map.put("uk" ,"英国" ); map.put("usa" ,"美国" ); System.out.println("元素个数:" +map.size()); System.out.println(map.toString()); System.out.println("===========keySet()=============" ); Set<String> keySet = map.keySet(); for (String key : keySet) { System.out.println(key+"-----" +map.get(key)); } System.out.println("===========entrySet()=============" ); Set<Map.Entry<String,String>> entries = map.entrySet(); for (Map.Entry<String,String> entry : entries) { System.out.println(entry.getKey()+"-----" +entry.getValue()); } System.out.println(map.containsKey("cn" )); System.out.println(map.containsValue("泰国" )); } }
Map集合的实现类
HashMap【重点】
JDK1.2版本,线程不安全,运行效率快;允许用null作为key或是value
HashMap使用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 package com.collection.map;import com.oop.demo02.Person;import java.util.HashMap;import java.util.HashSet;import java.util.Map;import java.util.Set;public class Demo02 { public static void main (String[] args) { HashMap<Student,String> students = new HashMap <Student,String>(); Student s1 = new Student ("孙悟空" , 100 ); Student s2 = new Student ("猪八戒" , 101 ); Student s3 = new Student ("沙和尚" , 102 ); students.put(s1,"北京" ); students.put(s2,"上海" ); students.put(s3,"深圳" ); students.put(new Student ("沙和尚" , 102 ),"深圳" ); System.out.println("元素个数:" +students.size()); System.out.println(students.toString()); students.remove(s1); System.out.println("删除之后:" +students.size()); System.out.println("===========keySet()=============" ); for (Student key : students.keySet()) { System.out.println(key+"-----" +students.get(key)); } System.out.println("===========entrySet()=============" ); Set<Map.Entry<Student,String>> entries = students.entrySet(); for (Map.Entry<Student,String> entry : entries) { System.out.println(entry.getKey()+"-----" +entry.getValue()); } System.out.println(students.containsKey("s1" )); System.out.println(students.containsValue("深圳" )); } }
HashMap源码分析总结:
HashMap刚创建时,table是null,为了节省空间,当添加第一个元素时,table容量调整为16
当元素个数大于阈值(16*0.75=12)时,会进行扩容,扩容后大小为原来的2倍。目的是减少元素的个数
jdk1.8 当每个链表长度大于8,并且元素个数大于等于64时,会调整为红黑树,目的是提高执行效率
jdk1.8 当链表长度小于6时,调整为链表
jdk1.8以前,链表是头插入,jdk1.8以后是尾插入
Hashtable
JDK1.0版本,线程安全,运行效率慢;不允许null作为key或是value
Properties
Hashtable的子类,要求key和value都是String。通常用于配置文件的读取
TreeMap
实现了SortedMap接口(是Map的子接口),可以对key自动排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 package com.collection.map;import java.util.Map;import java.util.Set;import java.util.TreeMap;public class Demo03 { public static void main (String[] args) { TreeMap<Student, String> treeMap = new TreeMap <>(); Student s1 = new Student ("孙悟空" , 100 ); Student s2 = new Student ("猪八戒" , 101 ); Student s3 = new Student ("沙和尚" , 102 ); treeMap.put(s1,"北京" ); treeMap.put(s2,"上海" ); treeMap.put(s3,"深圳" ); treeMap.put(new Student ("沙和尚" , 102 ),"深圳" ); System.out.println("元素个数:" +treeMap.size()); System.out.println(treeMap.toString()); treeMap.remove(s1); System.out.println("删除之后:" +treeMap.size()); System.out.println("===========keySet()=============" ); for (Student key : treeMap.keySet()) { System.out.println(key+"-----" +treeMap.get(key)); } System.out.println("===========entrySet()=============" ); Set<Map.Entry<Student,String>> entries = treeMap.entrySet(); for (Map.Entry<Student,String> entry : entries) { System.out.println(entry.getKey()+"-----" +entry.getValue()); } System.out.println(treeMap.containsKey(s1)); System.out.println(treeMap.containsValue("深圳" )); } }
泛型
泛型概述
泛型类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 package com.collection.generic;public class TestGeneric { public static void main (String[] args) { MyGeneric<String> myGeneric = new MyGeneric <>(); myGeneric.t = "hello" ; myGeneric.show("大家好,加油" ); String string = myGeneric.getT(); MyGeneric<Integer> myGeneric2 = new MyGeneric <>(); myGeneric2.t = 666 ; myGeneric2.show(888 ); Integer integer = myGeneric2.getT(); } } public class MyGeneric <T> { T t; public void show (T t) { System.out.println(t); } public T getT () { return t; } } */
泛型接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 package com.collection.generic;public class TestGeneric { public static void main (String[] args) { MyInterfaceImpl impl = new MyInterfaceImpl (); impl.server("xxxxxx" ); MyInterfaceImpl2<Integer> impl2 = new MyInterfaceImpl2 <>(); impl2.server(999 ); } } public interface MyInterface <T> { String name = "张三" ; T server (T t) ; } */
泛型方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 package com.collection.generic;public class TestGeneric { public static void main (String[] args) { MyGenericMethod myGenericMethod = new MyGenericMethod (); myGenericMethod.show("中国加油" ); myGenericMethod.show(200 ); myGenericMethod.show(3.14 ); } } public class MyGenericMethod { public <T> void show (T t) { System.out.println("泛型方法" +t); return t; } } */
泛型集合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 package com.collection.generic;import java.util.ArrayList;public class Demo01 { public static void main (String[] args) { ArrayList<String> arraylist = new ArrayList <>(); arraylist.add("xxx" ); arraylist.add("yyy" ); for (String string : arraylist){ System.out.println(string); } } }
Collections工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 package com.collection.map;import java.util.*;public class Demo04 { public static void main (String[] args) { List<Integer> list = new ArrayList <>(); list.add(20 ); list.add(5 ); list.add(12 ); list.add(30 ); list.add(6 ); System.out.println("排序之前:" +list.toString()); Collections.sort(list); System.out.println("排序之后:" +list.toString()); int i = Collections.binarySearch(list, 13 ); System.out.println(i); List<Integer> dest = new ArrayList <>(); for (int j = 0 ; j < list.size(); j++) { dest.add(0 ); } Collections.copy(dest,list); System.out.println(dest.toString()); Collections.reverse(list); System.out.println("反转之后:" +list); Collections.shuffle(list); System.out.println("打乱之后:" +list); System.out.println("=================list转成数组===================" ); Integer[] arr = list.toArray(new Integer [0 ]); System.out.println(arr.length); System.out.println(Arrays.toString(arr)); System.out.println("=================数组转成集合===================" ); String[] names = {"张三" ,"李四" ,"王五" }; List<String> list2 = Arrays.asList(names); System.out.println(list2); Integer[] nums = {100 ,200 ,300 ,400 ,500 }; List<Integer> list3 = Arrays.asList(nums); System.out.println(list3); } }