一:集合概念
对象的容器:实现了对对象常用的操作,类似数组功能。
二:集合和数组的区别
(1)数组长度固定,集合长度不固定
(2)数组可以存储基本类型和引用类型,集合只能存储引用类型
first example
package com.jihe.collection; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; /** * Collection接口的使用 * (1)添加元素 * (2)删除元素 * (3)遍历元素 * (4)判断 * @author lv */ public class Demo01 { public static void main(String[] args) { //创建集合 Collection collection = new ArrayList(); //(1)添加元素 collection.add("苹果"); collection.add("西瓜"); collection.add("榴莲"); System.out.println("元素个数:"+collection.size()); System.out.println(collection); //(2)删除元素 // collection.remove("榴莲"); // System.out.println("删除后:"+collection.size()); //(3)遍历元素 //(3.1)使用增强for System.out.println("------3.1使用增强for--------"); for (Object object : collection) { System.out.println(object); } //(3.2)使用迭代器(迭代器专门用来便利集合的一种方式) //hasNext();有没有下一个元素 //next();获取下一个元素 //remove();删除当前元素 System.out.println("------3.2使用迭代器--------"); Iterator iterator = collection.iterator(); while (iterator.hasNext()){ String s = (String)iterator.next(); System.out.println(s); //迭代过程中不能使用collection的删除方法 //collection.remove(s); iterator.remove(); } System.out.println("元素个数:"+collection.size()); //(4)判断 System.out.println(collection.contains("西瓜")); System.out.println(collection.isEmpty()); } }
second example
package com.jihe.collection; /** * 学生类 * @author lv * */ public class Student { private String name; private int age; public Student() { } public Student(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 "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
package com.jihe.collection; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; /** * Collection的使用:保存学生信息 * @author lv * */ public class Demo02 { public static void main(String[] args) { //新建一个Collection对象 Collection collection = new ArrayList(); Student s1 = new Student("张三",20); Student s2 = new Student("张无忌",18); Student s3 = new Student("王二",22); //1.添加数据 collection.add(s1); collection.add(s2); collection.add(s3); System.out.println(collection.size()); System.out.println(collection); //2.删除 //collection.remove(s1); //collection.clear(); //System.out.println("删除之后"+collection.size()); //3.遍历 //3.1增强for System.out.println("---------增强for--------------"); for (Object object : collection ) { Student s = (Student)object; System.out.println(s.toString()); } //3.2迭代器:hasNext() next(); remove(); 迭代过程中不能使用collection的删除方法 System.out.println("---------迭代器--------------"); Iterator iterator = collection.iterator(); while(iterator.hasNext()){ Student s = (Student) iterator.next(); System.out.println(s.toString()); } //4.判断 System.out.println(collection.contains(s1)); System.out.println(collection.isEmpty()); } }
特点:有序、有下表、元素可以重复
first example
package com.jihe.collection.list; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.ListIterator; /** * List子接口的使用 * 特点:1 有序 有下表 2 可以重复 * @author lv */ public class demo03 { public static void main(String[] args) { //先创建集合对象 List list = new ArrayList<>(); //1.添加元素 list.add("苹果"); list.add("小米"); list.add(0,"华为"); System.out.println("元素个数:"+list.size()); System.out.println(list.toString()); //2.删除元素 //list.remove("苹果"); //list.remove(0); //System.out.println("删除之后:"+list.size()); //System.out.println(list.toString()); //3.1使用for遍历 System.out.println("------3.1使用for便利------"); for (int i=0;i<list.size();i++){ System.out.println(list.get(i)); } //3.2使用增强for System.out.println("------3.2使用增强for------"); for (Object object : list ) { String s = (String)object; System.out.println(s); } //3.3使用迭代器 System.out.println("------3.3使用迭代器------"); Iterator iterator = list.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } //3.4使用列表迭代器,和Iterator的区别,ListIterator可以向前或向后遍历,添加、删除、修改元素 ListIterator listIterator = list.listIterator(); System.out.println("------3.4使用列表迭代器从前往后------"); while (listIterator.hasNext()){ System.out.println(listIterator.nextIndex()+":"+listIterator.next()); } System.out.println("------3.4使用列表迭代器从后往前------"); while(listIterator.hasPrevious()){ System.out.println(listIterator.previousIndex()+":"+listIterator.previous()); } //4判断 System.out.println(list.contains("苹果")); System.out.println(list.isEmpty()); //5获取位置 System.out.println(list.indexOf("华为")); } }
second example
package com.jihe.collection.list; import java.util.ArrayList; import java.util.List; /** * List的使用 * @author lv */ public class Demo04 { public static void main(String[] args) { //创建集合 List list = new ArrayList(); //添加数字数据(自动装箱) //自动装箱:这里的20已经不是基本类型20了而是Integer包装类 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()); //2删除操作 //list.remove(0); //list.remove(new Integer(20)); //list.remove((Object)20); //System.out.println("删除元素:"+list.size()); //System.out.println(list.toString()); //3补充方法subList:返回子集合 List sublist = list.subList(1,3); System.out.println(sublist.toString()); } }
ArrayList[重点]:
数组结构实现,查询快、增删慢
JDK1.2版本,运行效率快,线程不安全
源码分析:DEFAULT_CAPACITY = 10;默认容量
注意:如果没有向集合中添加任何元素时,容量0
elementData存放元素的数组
size 实际的元素个数
add()添加元素
package com.jihe.collection.list; import com.common.system.Student; import java.util.ArrayList; import java.util.Iterator; import java.util.ListIterator; /** * ArrayList的使用 * 存储结构:数组,查找遍历速度快,增删慢 * wgy * */ public class Demo05 { public static void main(String[] args) { //创建集合 ArrayList arrayList = new ArrayList<>(); //创建对象 Student s1 = new Student("刘德华",20); Student s2 = new Student("郭富城",22); Student s3 = new Student("梁朝伟",18); //1.添加元素 arrayList.add(s1); arrayList.add(s2); arrayList.add(s3); System.out.println("元素个数:"+arrayList.size()); System.out.println(arrayList.toString()); //2.删除元素 arrayList.remove(s1); System.out.println("删除之后:"+arrayList.size()); //3.遍历元素 //3.2使用迭代器 System.out.println("---------3.1使用迭代器-------------"); Iterator it = arrayList.iterator(); while (it.hasNext()){ Student s = (Student) it.next(); System.out.println(s.toString()); } //3.2列表迭代器 ListIterator list = arrayList.listIterator(); System.out.println("--------3.2使用列表迭代器-----------"); while(list.hasNext()){ Student s = (Student)list.next(); System.out.println(s.toString()); } System.out.println("--------3.2使用列表迭代器逆序-----------"); while(list.hasPrevious()){ Student s = (Student)list.previous(); System.out.println(s.toString()); } //4.判断 System.out.println(arrayList.contains(new Student("梁朝伟",18))); System.out.println(arrayList.isEmpty()); //5.查找 System.out.println(arrayList.indexOf(new Student("梁朝伟", 18))); } }
Vector:
数组结构实现,查询快、增删慢
JDK1.0版本,运行效率慢、线程安全
package com.jihe.collection.list.vector; import java.util.Enumeration; import java.util.Vector; public class Demo06 { public static void main(String[] args) { //创建集合 Vector vector = new Vector<>(); //1.添加元素 vector.add("草莓"); vector.add("芒果"); vector.add("西瓜"); System.out.println("元素个数:"+vector.size()); //2.删除 // vector.remove(0); // vector.remove("西瓜"); // vector.clear(); //3.遍历 //使用枚举器 Enumeration en = vector.elements(); while(en.hasMoreElements()){ String s = (String)en.nextElement(); System.out.println(s); } //4.判断 System.out.println(vector.contains("西瓜")); System.out.println(vector.isEmpty()); //5.vecotr其他方法 //firstElement、lastElement、elementAt() } }
LinkedList:
链表结构实现,增删快,查询慢
package com.jihe.collection.list.linklist; import com.jihe.collection.Student; import java.util.Iterator; import java.util.LinkedList; import java.util.ListIterator; public class Demo07 { public static void main(String[] args) { //创建集合 LinkedList linkedList = new LinkedList<>(); //1.添加元素 Student s1 = new Student("刘德华",20); Student s2 = new Student("郭富城",22); Student s3 = new Student("梁朝伟",18); linkedList.add(s1); linkedList.add(s2); linkedList.add(s3); System.out.println("元素个数"+linkedList.size()); System.out.println(linkedList.toString()); //2.删除 // linkedList.remove(new Student("刘德华",20)); // System.out.println("删除之后:"+linkedList.size()); // linkedList.clear(); //3.遍历 //3.1for遍历 System.out.println("--------------for--------------"); for (int i = 0; i < linkedList.size(); i++) { System.out.println(linkedList.get(i)); } //3.2增强for System.out.println("--------------增强for--------------"); for (Object object : linkedList) { Student s = (Student) object; System.out.println(s.toString()); } //3.3使用迭代器 System.out.println("--------------使用迭代器--------------"); Iterator it = linkedList.iterator(); while (it.hasNext()){ Student s = (Student) it.next(); System.out.println(s.toString()); } //3.4使用列表迭代器 System.out.println("--------------使用列表迭代器--------------"); ListIterator lit = linkedList.listIterator(); while(lit.hasNext()){ Student s = (Student) lit.next(); System.out.println(s.toString()); } System.out.println("--------------使用列表迭代器逆序--------------"); while(lit.hasPrevious()){ Student s = (Student) lit.previous(); System.out.println(s.toString()); } //4.判断 System.out.println(linkedList.contains(s1)); System.out.println(linkedList.isEmpty()); //5.获取 System.out.println(linkedList.indexOf(s2)); } }
Java泛型是JDK1.5中引入的一个新特性,其本质是参数化类型,把类型作为参数传递
常见形式有泛型类、泛型接口、泛型方法
语法:
<T,...> T称为类型占位符,表示一种引用类型
好处:
(1)提高代码的重用性
(2)防止类型转换异常,提高代码安全性
package com.jihe.collection.generic; public class TestGeneric { public static void main(String[] args) { //使用泛型类来拆功能键对象 //注意:1.泛型只能使用引用类型 2.不同的泛型对象不能相互赋值 MyGeneric<String> myGeneric = new MyGeneric<String>(); myGeneric.t = "hello"; myGeneric.show("大家好,加油"); String string = myGeneric.getT(); MyGeneric<Integer> myGeneric2 = new MyGeneric<Integer>(); myGeneric2.t = 100; myGeneric2.show(200); Integer integer = myGeneric2.getT(); } }
package com.jihe.collection.generic; public interface MyInterface<T> { //接口由默认常量默认由public static final修饰 String name = "张三"; T server(T t); } package com.jihe.collection.generic; public class MyInterfaceImpl implements MyInterface<String>{ @Override public String server(String s) { System.out.println(s); return null; } }
package com.jihe.collection.generic; public class MyInterfaceImpl2<T> implements MyInterface<T>{ @Override public T server(T t) { System.out.println(t); return t; } }
package com.jihe.collection.generic; public class TestGeneric { public static void main(String[] args) { MyInterfaceImpl impl = new MyInterfaceImpl(); impl.server("xxxx"); MyInterfaceImpl2<Integer> impl2 = new MyInterfaceImpl2<>(); impl2.server(1000); } }
package com.jihe.collection.generic; public class MyGenericMethod { //泛型方法 public <T> T show(T t){ System.out.println("泛型方法"); return t; } }
package com.jihe.collection.generic; public class TestGeneric { public static void main(String[] args) { //泛型方法 MyGenericMethod myGenericMethod = new MyGenericMethod(); myGenericMethod.show("中国加油"); myGenericMethod.show(200); myGenericMethod.show(3.14); } }
概念:参数化类型、类型安全的集合,强制集合元素的类型必须一致
特点:
编译时即可检查,而非运行时抛出异常
访问时,不必类型转换(拆箱)
不同泛型之间引用不能相互赋值,泛型不存在多态
package com.jihe.collection.generic; import com.jihe.collection.Student; import java.util.ArrayList; import java.util.Iterator; public class Demo03 { public static void main(String[] args) { ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("XXX"); arrayList.add("yyy"); // arrayList.add(10); // arrayList.add(20); for (String string : arrayList) { System.out.println(string); } ArrayList<Student> arrayList1 = new ArrayList<Student>(); Student s1 = new Student("刘德华", 20); Student s2 = new Student("郭富城", 22); Student s3 = new Student("梁朝伟", 18); arrayList1.add(s1); arrayList1.add(s2); arrayList1.add(s3); Iterator<Student> iterator = arrayList1.iterator(); while (iterator.hasNext()){ Student s = iterator.next(); System.out.println(s.toString()); } } }
特点:无序、无下标、元素不可重复
package com.jihe.collection.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<>(); //1.添加元素 set.add("苹果"); set.add("华为"); set.add("小米"); // set.add("华为"); System.out.println("数据个数"+set.size()); System.out.println(set.toString()); //2.删除数据 set.remove("小米"); System.out.println(set.toString()); //3.遍历[重点] //3.1使用增强for System.out.println("--------增强for--------"); for (String string : set) { System.out.println(string); } //3.2使用迭代器 System.out.println("--------使用迭代器--------"); Iterator<String> iterator = set.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } //4.判断 System.out.println(set.contains("华为")); System.out.println(set.isEmpty()); } }
HashSet【重点】:
基于HashCode实现元素不重复
当存入元素的哈希码相同时,会调用equals进行确认,如结果为true,则拒绝后者存入
package com.jihe.collection.set; import java.util.HashSet; import java.util.Iterator; /** * HashSet集合 * 存储结构:哈希表(数组+链表+红黑树) */ public class Demo02 { public static void main(String[] args) { //新建集合 HashSet<String> hashSet = new HashSet<>(); //1.添加元素 hashSet.add("刘德华"); hashSet.add("梁朝伟"); hashSet.add("林志玲"); hashSet.add("周润发"); // hashSet.add("刘德华"); System.out.println("元素的个数"+hashSet.size()); System.out.println(hashSet.toString()); //2.删除数据 // hashSet.remove("刘德华"); // System.out.println("删除之后"+hashSet.size()); //3.遍历操作 //3.1增强for System.out.println("--------增强for--------"); for (String s : hashSet) { System.out.println(s); } //3.2迭代器 System.out.println("--------迭代器--------"); Iterator<String> iterator = hashSet.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } //4.判断 System.out.println(hashSet.contains("郭富城")); System.out.println(hashSet.isEmpty()); } }
package com.jihe.collection.set; import java.util.Objects; 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 int hashCode() { // int n1 = this.name.hashCode(); // int n2 = this.age+31; // // return n1+n2; // } // // @Override // public boolean equals(Object obj) { // if (this==obj){ // return true; // } // if (obj==null){ // return false; // } // if (obj instanceof Person){ // Person p = (Person)obj; // if (this.name.equals(p.getName())&&this.age==p.getAge()){ // return true; // } // } // return false; // } @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); } }
package com.jihe.collection.set; import java.util.HashSet; import java.util.Iterator; /** * HashSet的使用 * 存储结构:哈希表(数组+链表+红黑树) * 存储过程 * (1)根据hashcode计算保存的位置,如果此位置为空,则直接保存,如果不为空执行第二步 * (2)再执行equals方法,如果equals方法为true,则认为是重复,否则,形成链表 */ public class Demo03 { public static void main(String[] args) { //创建集合 HashSet<Person> persons = new HashSet<>(); //添加数据 Person p1 = new Person("刘德华",20); Person p2 = new Person("林志玲",18); Person p3 = new Person("梁朝伟",22); persons.add(p1); persons.add(p2); persons.add(p3); // persons.add(p3);重复不能添加 System.out.println("元素个数:"+persons.size()); System.out.println(persons.toString()); //2.删除操作 // persons.remove(p1); // persons.remove(new Person("刘德华",20)); // System.out.println("删除之后:"+persons.size()); //遍历[重点] //3.1使用增强for System.out.println("-----------使用增强for-----------"); for (Person person : persons) { System.out.println(person.toString()); } //3.2迭代器 System.out.println("-----------使用迭代器-----------"); Iterator<Person> it = persons.iterator(); while (it.hasNext()){ System.out.println(it.next()); } //4.判断 System.out.println(persons.contains(new Person("刘德华",20))); System.out.println(persons.isEmpty()); } }
TreeSet:
基于排列顺序实现元素不重复
实现了SortedSet接口,对集合元素自动排序
元素对象的类型必须实现Comparable接口,指定排序规则
通过CompareTo方法确定是否为重复元素
package com.jihe.collection.treeset; import java.util.Iterator; import java.util.TreeSet; /** * TreeSet的使用 * 存储结构:红黑树 */ public class Demo01 { public static void main(String[] args) { //创建集合 TreeSet<String> treeSet = new TreeSet<>(); //1.添加元素 treeSet.add("xyz"); treeSet.add("abc"); treeSet.add("hello"); treeSet.add("xyz"); System.out.println("元素个数"+treeSet.size()); System.out.println(treeSet.toString()); //2.删除 treeSet.remove("xyz"); System.out.println("删除之后:"+treeSet.size()); //3.遍历 //3.1使用增强for for (String string: treeSet) { System.out.println(string); } System.out.println("---------------"); //3.2使用迭代器 Iterator<String> it = treeSet.iterator(); while (it.hasNext()){ System.out.println(it.next()); } //4.判断 System.out.println(treeSet.contains("abc")); } }
package com.jihe.collection.set; import java.util.Objects; 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 int hashCode() { // int n1 = this.name.hashCode(); // int n2 = this.age+31; // // return n1+n2; // } // // @Override // public boolean equals(Object obj) { // if (this==obj){ // return true; // } // if (obj==null){ // return false; // } // if (obj instanceof Person){ // Person p = (Person)obj; // if (this.name.equals(p.getName())&&this.age==p.getAge()){ // return true; // } // } // return false; // } @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; } }
package com.jihe.collection.set.hashset; import java.util.HashSet; import java.util.Iterator; /** * HashSet集合 * 存储结构:哈希表(数组+链表+红黑树) */ public class Demo02 { public static void main(String[] args) { //新建集合 HashSet<String> hashSet = new HashSet<>(); //1.添加元素 hashSet.add("刘德华"); hashSet.add("梁朝伟"); hashSet.add("林志玲"); hashSet.add("周润发"); // hashSet.add("刘德华"); System.out.println("元素的个数"+hashSet.size()); System.out.println(hashSet.toString()); //2.删除数据 // hashSet.remove("刘德华"); // System.out.println("删除之后"+hashSet.size()); //3.遍历操作 //3.1增强for System.out.println("--------增强for--------"); for (String s : hashSet) { System.out.println(s); } //3.2迭代器 System.out.println("--------迭代器--------"); Iterator<String> iterator = hashSet.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } //4.判断 System.out.println(hashSet.contains("郭富城")); System.out.println(hashSet.isEmpty()); } }
package com.jihe.collection.set.treeset; import com.jihe.collection.set.Person; import java.util.Comparator; import java.util.TreeSet; /** * TreeSet集合的使用 * Comparator:实现定制比较 * Comparable:可比较的 * */ public class Demo03 { 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 0; } }); Person p1 = new Person("xyz", 20); Person p2 = new Person("hello", 22); Person p3 = new Person("zhangsan", 25); Person p4 = new Person("lisi", 25); persons.add(p1); persons.add(p2); persons.add(p3); persons.add(p4); System.out.println(persons.toString()); } }
package com.jihe.collection.set.treeset; import java.util.Comparator; import java.util.TreeSet; /** * 要求:使用TreeSet集合实现字符串长度进行排序 * helloworld zhangsan lisi wangwu beijing xian nanjing *Comparator接口实现定制比较 */ public class Demo04 { 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("zhangsan"); treeSet.add("lisi"); treeSet.add("wangwu"); treeSet.add("beijing"); treeSet.add("xian"); treeSet.add("nanjing"); System.out.println(treeSet.toString()); } }
特点:存储一对数据(Key-Value),无序、无下标,键不可重复,值可重复。
方法
V put(K key, V value) //将对象存入到集合中,关联键值。key重复则覆盖原值
Object get(Object key)//根据键获取对应的值
Set<K>//返回所有key
Collection<V> values() //返回包含所有值的Collection集合
Set<Map.Entry<K,V>> //键值匹配的Set集合
package com.jihe.collection.map; import java.util.HashMap; import java.util.Map; import java.util.Set; /** * Map接口的使用 * 特点:(1)存储键值对(2)键不饿能重复,值可以重复(3)无序 * */ public class Demo01 { public static void main(String[] args) { //创建Map集合 Map<String, String> map = new HashMap<>(); //1.添加元素 map.put("cn","中国"); map.put("uk","英国"); map.put("usa","美国"); // map.put("cn","中国"); System.out.println("元素个数:"+map.size()); System.out.println(map.toString()); //2.删除 map.remove("usa"); System.out.println("删除之后"+map.size()); //3.遍历 //3.1使用keySet(); System.out.println("-------keySet()-------"); // Set<String> keyset = map.keySet(); for (String key : map.keySet()) { System.out.println(key+"-----"+map.get(key)); } //3.2使用entrySet()方法 System.out.println("-------entrySet()-------"); // Set<Map.Entry<String, String>> entries = map.entrySet(); for (Map.Entry<String, String> entry : map.entrySet()) { System.out.println(entry.getKey()+"----------"+entry.getValue()); } //4.判断 System.out.println(map.containsKey("cn")); System.out.println(map.containsValue("泰国")); } }
HashMap【重点】:
JDK1.2版本,线程不安全,运行效率快;允许用null 作为key或是value
package com.jihe.collection.map; import java.util.Objects; public class Student { private String name; private int stuNo; public Student() { } public Student(String name, int stuNo) { this.name = name; this.stuNo = stuNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getStuNo() { return stuNo; } public void setStuNo(int stuNo) { this.stuNo = stuNo; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", stuNo=" + stuNo + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return stuNo == student.stuNo && Objects.equals(name, student.name); } @Override public int hashCode() { return Objects.hash(name, stuNo); } }
package com.jihe.collection.map; import java.util.HashMap; import java.util.Map; /** * HashMap集合的使用 * 存储结构:哈希表(数组+链表+红黑树) * */ 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(s3,"杭州"); System.out.println("元素个数"+students.size()); System.out.println(students.toString()); //2.删除 // students.remove(s1); // System.out.println("删除之后"+students.size()); //3.遍历 //3.1使用keySet(); System.out.println("------keySet------"); for (Student key : students.keySet()) { System.out.println(key.toString()+"======"+students.get(key)); } System.out.println("------entrySet------"); //3.2使用entrySet(); for (Map.Entry<Student, String> entry : students.entrySet()) { System.out.println(entry.getKey()+"======"+entry.getValue()); } //4判断 System.out.println(students.containsKey(new Student("孙悟空", 100))); System.out.println(students.containsValue("杭州")); } }
Hashtable:
JDK1.0版本,线程安全,运行效率慢;不允许null作为key或是value
Properties:
Hashtable的子类,要求key和values都是String。通常用于配置文件的读取
TreeMap
实现了SortedMap接口(是Map的子接口),可以对key自动排序
package com.jihe.collection.map; import java.util.Map; import java.util.TreeMap; public class Demo03 { public static void main(String[] args) { //新建集合 TreeMap<Student, String> treeMap = new TreeMap<>(); //1.添加元素 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()); //2.删除 // treeMap.remove(new Student("猪八戒", 101)); // System.out.println(treeMap.size()); //3.遍历 //3.1使用keySet System.out.println("--------keySet()--------"); for (Student key : treeMap.keySet()){ System.out.println(key + "-------" + treeMap.get(key)); } //3.1使用entrySet System.out.println("--------entrySet()--------"); for (Map.Entry<Student, String> entry : treeMap.entrySet()){ System.out.println(entry.getKey()+"-------" + entry.getValue()); } //4.判断 System.out.println(treeMap.containsKey(new Student("沙和尚",102))); } }
概念:集合工具类,定义了除了存取意外的集合常用方法。
方法:
public staitc void reverse(List<?> list)//反转集合中元素的顺序
public static void shuffle(List<?> list)//随机重置集合元素的顺序
public static void sort(List<T> list)//升序排序(元素类型必须实现Comparable接口)
package com.jihe.collection.map; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; 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); //sort排序 System.out.println("排序之前:"+list.toString()); Collections.sort(list); System.out.println("排序之后:"+list.toString()); //binarySearch二分查找 int i = Collections.binarySearch(list, 12); System.out.println(i); //copy复制 List<Integer> dest = new ArrayList<>(); for (int k = 0; k < list.size(); k++) { dest.add(0); } Collections.copy(dest, list); System.out.println(dest.toString()); //reverse反转 Collections.reverse(list); System.out.println("反转之后:" + list); //shuffle 打乱 Collections.shuffle(list); System.out.println("打乱之后:" + list); //补充:list转成数组 System.out.println("--------list转成数组--------"); Integer[] arr = list.toArray(new Integer[10]); System.out.println(arr.length); System.out.println(Arrays.toString(arr)); //数组转成集合 System.out.println("--------list转成集合--------"); 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); } }