集合之Collection体系集合
集合之Collection体系集合
一、集合概念
对象的容器,实现了对对象的常用操作,类似数组功能。
二、集合和数组的区别
- 数组长度固定,集合长度不固定。
- 数组可以存储基本类型和引用类型,集合只能存储引用类型。
- 集合的位置:java.util.*。
三、Collection体系集合
Collection(Interface):该体系结构的根接口,代表一组对象,成为集合。
Collections工具类:排序sort(list)、二分查找binarySearch(list,o)、复制copy(dest,src)、反转reverse(list)、打乱shuffle(list)
-
List(子接口):特点:有序、有下标、元素可重复。
- 代码实现
package com.sun.base.Collections; /** * List子接口的使用 * 特点:有序、有下标、可重复 * @author SFF */ import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.ListIterator; public class Demo01{ 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()); //删除元素(删除数字数据时需要手动装箱---list.remove(obj(num))/list.remove(new Integer(num))) //list.remove("华为"); list.remove(1); System.out.println("删除之后元素个数:"+list.size()); System.out.println(list.toString()); //遍历 //1、使用for循环 System.out.println("===========1、使用for循环=========="); for (int i= 0;i<list.size();i++){ System.out.println(list.get(i)); } //2、增强for System.out.println("===========2、增强for=========="); for (Object object:list) { System.out.println(object); } //3、使用迭代器Iterator Iterator it = list.iterator(); System.out.println("===========3、使用迭代器Iterator=========="); while (it.hasNext()){ System.out.println(it.next()); } //4、使用列表迭代器 和Iterator的区别,ListIterator可以向前或向后遍历,添加、删除、修改元素 System.out.println("===========4、使用列表迭代器Iterator向后=========="); ListIterator it1 = list.listIterator(); while (it1.hasNext()){ System.out.println(it1.nextIndex()+":"+it1.next()); } System.out.println("===========4、使用列表迭代器Iterator向前=========="); while (it1.hasPrevious()){ System.out.println(it1.previousIndex()+":"+it1.previous()); } //判断 System.out.println(list.contains("华为")); System.out.println(list.isEmpty()); //获取 System.out.println(list.indexOf("华为")); //补充方法sublist,返回子集合,含头不含尾! //List sublist=list.sublist(startIndex,EndIndex) } }- List实现类:ArrayList、LinkedList、Vector
-
ArrayList:数组结构实现,查询快、增删慢。(运行效率快,线程不安全)
代码实现
package com.sun.base.Collections; import java.util.ArrayList; import java.util.Iterator; import java.util.ListIterator; /** * ArrayList的使用 * 存储结构:数组,查找遍历速度快,增删慢 * @author SFF */ public class Demo02 { public static void main(String[] args) { //创建集合 ArrayList arrayList=new ArrayList(); //1、添加元素 Student s1=new Student("小明",18); Student s2=new Student("小李",17); Student s3=new Student("小红",20); Student s4=new Student("小王",19); arrayList.add(s1); arrayList.add(s2); arrayList.add(s3); arrayList.add(s4); System.out.println("元素个数:"+arrayList.size()); System.out.println(arrayList.toString()); //2、删除元素 // arrayList.remove(s1);//通过元素删除 // arrayList.remove(1);//通过下标删除 // System.out.println(arrayList.toString()); //3、遍历元素 //3.1增强for循环 System.out.println("========3.1增强for循环========="); for (Object obj:arrayList) { System.out.println(obj); } //3.2使用迭代器 System.out.println("========3.2使用迭代器========="); Iterator iterator = arrayList.iterator(); while (iterator.hasNext()){ Student s=(Student)iterator.next(); System.out.println(s); } //3.3使用列表迭代器 System.out.println("========3.2使用列表迭代器========="); ListIterator listIterator = arrayList.listIterator(); while (listIterator.hasNext()){ Student s5=(Student)listIterator.next(); System.out.println(s5); } System.out.println("========3.2使用列表迭代器逆序========="); ListIterator listIterator1 = arrayList.listIterator(); while (listIterator.hasPrevious()){ Student s6=(Student)listIterator.previous(); System.out.println(s6); } //4、判断 System.out.println(arrayList.contains(s1)); System.out.println(arrayList.isEmpty()); //5、查找 System.out.println(arrayList.indexOf(s2)); } } class Student{ private String name; private int age; 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 + ']'; } } -
LinkedList:链表结构实现,增删快,查询慢。
代码实现
package com.sun.base.Collections; import java.util.Iterator; import java.util.LinkedList; import java.util.ListIterator; /** * LinkedList的使用 * 存储结构:双向链表 * @author SFF */ public class Demo03 { public static void main(String[] args) { //创建集合 LinkedList linkedList=new LinkedList(); //1、添加元素 Student1 s1=new Student1("小明",18); Student1 s2=new Student1("小李",17); Student1 s3=new Student1("小红",20); Student1 s4=new Student1("小王",19); linkedList.add(s1); linkedList.add(s2); linkedList.add(s3); linkedList.add(s4); System.out.println("元素个数:"+linkedList.size()); System.out.println(linkedList.toString()); //2、删除元素 // linkedList.remove(s1); // linkedList.remove(2); // System.out.println("元素个数:"+linkedList.size()); // System.out.println(linkedList.toString()); //3、遍历 //3.1增强for循环 System.out.println("=======3.1增强for循环========"); for (Object obj:linkedList) { Student1 s5=(Student1)obj; System.out.println(s5.toString()); } //3.2使用迭代器 System.out.println("=======3.2使用迭代器========"); Iterator iterator = linkedList.iterator(); while (iterator.hasNext()){ Student1 s6=(Student1)iterator.next(); System.out.println(s6.toString()); } //3.3使用列表迭代器 System.out.println("=======3.3使用列表迭代器========"); ListIterator listIterator = linkedList.listIterator(); while (listIterator.hasNext()){ Student1 s7=(Student1)listIterator.next(); System.out.println(s7.toString()); } System.out.println("=======3.4使用列表迭代器逆序========"); while (listIterator.hasPrevious()){ Student1 s8=(Student1)listIterator.previous(); System.out.println(s8.toString()); } //4、判断 System.out.println(linkedList.contains(s2)); System.out.println(linkedList.isEmpty()); //获取 System.out.println(linkedList.indexOf(s2)); } } class Student1{ private String name; private int age; Student1(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 + ']'; } }
-
Set(子接口):特点:无序、无下标、元素不可重复。
代码实现
package com.sun.base.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.Set; /** * 测试Set接口的使用 * 特点;(1)无序、没有下标(2)不能重复 * @author SFF */ public class Demo04 { public static void main(String[] args) { //创建集合 Set<String> set = new HashSet<>(); //1、添加元素 set.add("小米"); set.add("华为"); set.add("苹果"); System.out.println("元素个数:"+set.size()); System.out.println(set.toString()); //2、删除元素 set.remove("小米"); System.out.println("元素个数:"+set.size()); System.out.println(set.toString()); //3、遍历 System.out.println("======使用增强for======="); for (String string:set) { System.out.println(string.toString()); } 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()); } }1. **实现类:**HashSet、TreeSet(实现SortedSet接口)。 -
HashSet
代码实现
package com.sun.base.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.Objects; /** * HashSet集合使用 * 存储结构:哈希表(数组+链表+红黑树) * 存储过程 * (1)根据HashCode计算保存的位置,如果此位置为空,则直接保存,如果不为空执行第二步。 * (2)执行equals方法,如果equals方法为true,则认为是重复,否则,形成链表 * @author SFF */ public class Demo05 { public static void main(String[] args) { //创建集合 HashSet<Person> persons = new HashSet<>(); //1、添加数据 Person p1 = new Person("刘德华", 19); Person p2 = new Person("郭富城", 29); Person p3 = new Person("李连杰", 39); persons.add(p1); persons.add(p2); persons.add(p3); System.out.println("元素个数:"+persons.size()); System.out.println(persons.toString()); //2、删除数据 // persons.remove(p1); // persons.remove(new Person("李连杰", 39));//重写HashCode和equals方法之后,可用 // System.out.println("元素个数:"+persons.size()); // System.out.println(persons.toString()); //3、遍历 System.out.println("=========使用增强for========"); for (Person person:persons) { System.out.println(person.toString()); } System.out.println("=========使用迭代器========"); Iterator<Person> iterator = persons.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } //4、判断 System.out.println(persons.contains(p1)); System.out.println(persons.contains(new Person("刘德华", 19))); System.out.println(persons.isEmpty()); } } class Person{ private String name; private int 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); } public Person(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Person[" + "name=" + name + ", 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; } } -
TreeSet--------可实现定制比较
代码实现
package com.sun.base.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.TreeSet; /** * TreeSet的使用 * 存储结构:红黑树 * Comparator:实现定制比较(比较器) * Comparable:可比较的 * @author SFF */ public class Demo06 { 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; } }); //1、添加元素 treeSet.add("beijing"); treeSet.add("shanghai"); treeSet.add("xian"); treeSet.add("tianjing"); treeSet.add("helloworld"); treeSet.add("cat"); System.out.println("元素个数:"+treeSet.size()); System.out.println(treeSet.toString()); //2、删除元素 treeSet.remove("cat"); System.out.println("元素个数:"+treeSet.size()); System.out.println(treeSet.toString()); //3、遍历 System.out.println("========使用增强for========"); for (String string:treeSet) { System.out.println(string.toString()); } System.out.println("========使用迭代器========"); Iterator<String> iterator = treeSet.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } //判断 System.out.println(treeSet.contains("xian")); System.out.println(treeSet.isEmpty()); } }

浙公网安备 33010602011771号