一、List 元素有序(储存数据顺序与迭代顺序一致或相反)
1.Vector : 底层数组实现,线程安全,效率低
2.ArrayList : 底层数组实现,线程不安全,效率高,较LinkedList查询快,增删慢
3.LinkedList : 底层链表实现,线程不安全,效率高,较ArrayList增删快,查询慢
二、Set 元素具有唯一性
1.HashSet : 底层哈希表(元素是链表的数组)实现,哈希表依赖哈希值存储。不保证元素的迭代顺序。此类允许使用null值元素。添加功能底层依赖两个方法:hashCode()、equals()
2.TreeSet : 底层二叉树结构,元素有序(比较方法排序):
自然排序:元素对象实现Comparable接口,重写compareTo()方法
1. import java.util.Comparator;
2. import java.util.TreeSet;
3. public class TreeSetTest1 {
4. public static void main(String[] args) {
5. //自然排序
6. TreeSet<Student> set = new TreeSet<Student>();
7. set.add(new Student("年三",20));
8. set.add(new Student("王一",15));
9. set.add(new Student("牛二",20));
10. set.add(new Student("三年",15));
11. set.add(new Student("一王",17));
12. set.add(new Student("二牛",20));
13. for(Student s : set) {
14. System.out.println(s);
15. }
16. }
17. }
18. class Student implements Comparable<Student>{
19. private String name;
20. private int age;
21. /*年龄的数值 从小到大*/
22. public int compareTo(Student o) {
23. if(this.age > o.age)
24. return 1;
25. else if(this.age < o.age)
26. return -1;
27. else
28. //年龄相等情况:return 0——>不添加,return 1——>往前排,return -1——>往后排
29. return -1;
30. }
31. public Student(){}
32. public Student(String name,int age){
33. this.name = name;
34. this.age = age;
35. }
36. public void setName(String name) {
37. this.name = name;
38. }
39. public String getName() {
40. return name;
41. }
42. public void setAge(int age) {
43. this.age = age;
44. }
45. public int getAge() {
46. return age;
47. }
48. public String toString() {
49. return "Student [name="+name+",age="+age+"]";
50. }
51. public int hashCode() {
52. final int prime = 31;
53. int result = 1;
54. result = prime * result + age;
55. result = prime * result + ((name == null) ? 0 : name.hashCode());
56. return result;
57. }
58. public boolean equals(Object obj) {
59. if (this == obj)
60. return true;
61. if (obj == null)
62. return false;
63. if (getClass() != obj.getClass())
64. return false;
65. Student other = (Student) obj;
66. if (age != other.age)
67. return false;
68. if (name == null) {
69. if (other.name != null)
70. return false;
71. } else if (!name.equals(other.name))
72. return false;
73. return true;
74. }
75. }
比较器排序:比较器实现Comparator接口,重写compare()方法
1. import java.util.Comparator;
2. import java.util.TreeSet;
3. public class TreeSetTest2 {
4. public static void main(String[] args) {
5. //比较器排序
6. TreeSet<Student> set = new TreeSet<Student>(
7. //创建一个Comparator的子类实例对象(匿名)
8. new Comparator<Student>() {
9. public int compare(Student o1,Student o2) {
10. if(o1.getAge() > o2.getAge())
11. return -1;
12. else if(o1.getAge() < o2.getAge())
13. return 1;
14. else
15. return -1;
16. }
17. }
18. );
19. set.add(new Student("年三",20));
20. set.add(new Student("王一",15));
21. set.add(new Student("牛二",20));
22. set.add(new Student("三年",15));
23. set.add(new Student("一王",17));
24. set.add(new Student("二牛",20));
25. for(Student s : set) {
26. System.out.println(s);
27. }
28. }
29. }
30. class Student{
31. private String name;
32. private int age;
33. public Student(){}
34. public Student(String name,int age){
35. this.name = name;
36. this.age = age;
37. }
38. public void setName(String name) {
39. this.name = name;
40. }
41. public String getName() {
42. return name;
43. }
44. public void setAge(int age) {
45. this.age = age;
46. }
47. public int getAge() {
48. return age;
49. }
50. public String toString() {
51. return "Student [name="+name+",age="+age+"]";
52. }
53. public int hashCode() {
54. final int prime = 31;
55. int result = 1;
56. result = prime * result + age;
57. result = prime * result + ((name == null) ? 0 : name.hashCode());
58. return result;
59. }
60. public boolean equals(Object obj) {
61. if (this == obj)
62. return true;
63. if (obj == null)
64. return false;
65. if (getClass() != obj.getClass())
66. return false;
67. Student other = (Student) obj;
68. if (age != other.age)
69. return false;
70. if (name == null) {
71. if (other.name != null)
72. return false;
73. } else if (!name.equals(other.name))
74. return false;
75. return true;
76. }
77. }
3.LinkedHashSet
底层由哈希表和链表实现。哈希表保证元素的唯一性,链表保证元素具有可预知的迭代顺序
三、Map 存储键值对
1.HashMap
基于哈希表的Map实现,哈希表的作用是用来保证键的唯一性,键可以为null,值也可以为null,此实现是不同步的,效率高
2.TreeMap
键是二叉树结构,可以保证键的有序和唯一性,键不能为null(添加元素时,需要调用键的比较方法),值也不能为null
3.Hashtable
此类实现一个哈希表,哈希表将键映射到相应的值,键和值都不能为null(JDK1.0出现)
1. //遍历Map集合的方式:
2. import java.util.TreeMap;
3. public class TreeMapTest {
4. public static void main(String[] args) {
5. TreeMap map = new TreeMap();
6. map.put("one","1" );
7. map.put("two", "2");
8. map.put("three", "3");
9. //方式一
10. Set set1 = map.keySet();
11. for(String v : set1) {
12. System.out.println("——>"+v);
13. }
14. //方式二
15. Set set2 = map.keySet();
16. for(String k : set2) {
17. System.out.println(k+"——>"+map.get(k));
18. }
19. //方式三
20. Set<Map.Entry> set3 = map.entrySet();
21. for(Map.Entry<String,String> e : set3) {
22. System.out.println(e.getKey()+"——>"+e.getValue());
23. }
24. }
25. }
四、Arrays类(数组工具类)
1.void sort(int[] a):对数组进行排序
2.int binarySearch(int[] a,int value):二分查找排好序的int类型数组中某个元素出现的下标
3.boolean deepEquals(Object[] o1,Object[] o2):比较两个数组的深度是否相等
4.void fill(int[] a,int value):将int类型数组中所有元素替换成指定int类型元素
5.int[] copyOf(int [] original,int newLength):复制数组中的元素,以使副本数组具有指定的长度
五、Collections类(集合工具类)
1.void sort(List list,Comparator c):使用比较器对集合进行排序
2.void shuffle(List list):打乱集合中的元素
3.T max(Collection c):获取集合中最大的元素
4.T min(Collection c):获取集合中最小的元素
5.void copy(List source,List target):将一个集合中的元素全部替换成指定的元素
六、泛型(Generic)
JDk 1.5开始,Java允许定义和使用泛型类、泛型接口、泛型方法
一种把类型明确的工作空间推迟到创建对象或者调用方法时明确,参数化类型的能力的特殊类型。
使用泛型的主要优点是能够在编译时而不是在运行时检测出错误
使用泛型来提高软件的可靠性和可读性
1.泛型类
1. public class GenericTest1 {
2. public static void main(String[] args) {
3. GenericClass<Student> gc = new GenericClass<Student>();
4. gc.setObj(new Student("刘亦菲",20));
5. System.out.println(gc.getObj());
6. }
7. }
8. //泛型类
9. class GenericClass<T> {
10. private T obj;
11. public T getObj() {
12. return obj;
13. }
14. public void setObj(T obj) {
15. this.obj = obj;
16. }
17. }
18. class Student {
19. private String name;
20. private int age;
21. public Student() {
22. }
23. public Student(String name,int age) {
24. this.name = name;
25. this.age = age;
26. }
27. public void setName(String name) {
28. this.name = name;
29. }
30. public String getName() {
31. return name;
32. }
33. public void setAge() {
34. this.age = age;
35. }
36. public int getAge() {
37. return age;
38. }
39. public String toString() {
40. return "String [name="+name+",age="+age+"]";
41. }
42. }
2.泛型接口
1. public class GenericTest2 {
2. public static void main(String[] args) {
3. GenericInterface<String> gt = new Tool<String>();
4. gt.show("123");
5. }
6. }
7. //泛型接口
8. interface GenericInterface<T> {
9. public void show(T t);
10. }
11. class Tool<T> implements GenericInterface<T> {
12. public void show(T t) {
13. System.out.println(t);
14. }
15. }
3.泛型通配符
1. import java.util.Collection;
2. import java.util.ArrayList;
3. public class GenericTest3 {
4. public static void main(String[] args) {
5. Collection<Object> c1 = new ArrayList<Object>();
6. //?任意类型
7. Collection<?> c2 = new ArrayList<Object>();
8. //? extends E : 向下限定,E及其子类
9. Collection<? extends Animal> c3 = new ArrayList<Animal>();
10. Collection<? extends Animal> c4 = new ArrayList<Dog>();
11. //?super Animal : 向上限定,E及其父类
12. Collection<? super Animal> c5 = new ArrayList<Animal>();
13. Collection<? super Animal> c6 = new ArrayList<Object>();
14. }
15. }
16. class Animal {
17. }
18. class Dog extends Animal {
19. }
20. class Cat extends Animal {
21. }