0. Overview of collections
(1) Collections are special objects that store other objects. You can think of a collection as a container
(2) The related interfaces and classes of the collection are located in the java.util package
(3) The interfaces and classes in a set are a whole and a system. The whole system is called a set framework.
The similarities and differences between sets and arrays:
The length of the array is fixed, and the length of the set can vary.
The elements stored in the collection must be reference type data
Interface:
The set framework defines some interfaces that determine the nature of the set class. Specific collection classes only provide different implementations of standard interfaces.
Be careful:
List: Represents an ordered collection of repeatable elements.
Set (Group, Set): Represents an unordered collection of elements that cannot be repeated. Elements in a group must be unique.
1,collection
package CollectionClass; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; /* *The difference between sets and arrays * 1,The size of the collection can be changed in real time. Once an array is created, the size is fixed. * 2,The elements stored in a collection are object data of reference type, and arrays are basic type data. */ /* *collection The set subclasses are list, set *1,list Ordered and repeatable elements *2,set The elements are disordered and unrepeatable * */ public class CollectionSample { public static void main(String[] args) { // TODO Auto-generated method stub Collection<String> list = new ArrayList<>();//polymorphic list.add("Zhang Wuji"); list.add("Zhao Min"); list.add("Zhou Zhi Luo"); list.add("Xiao Zhao"); System.out.println(list.size()); System.out.println(list.remove("Zhao Min")); System.out.println(list.remove(2));//Polymorphism can only point to subclass rewriting methods and cannot be deleted by index System.out.println(list.contains("Zhang Wuji")); System.out.println(list); Object [] array=list.toArray(); for (Object object : array) { System.out.println(object); } //list.clear(); System.out.println(list.isEmpty()); } }
2,List
2.1,ArrayList
package CollectionClass; import java.util.ArrayList; import java.util.List; import java.util.ListIterator; /* * list Collective characteristics: * 1,Elements are ordered in a set. * 2,Elements have indexed values and can be manipulated based on indexed values * 3,Elements can be repeated * */ public class ArraylistSample { public static void main(String[] args) { // TODO Auto-generated method stub List<String> list = new ArrayList<String>();//polymorphic list.add("Zhang Wuji"); list.add("Zhao Min"); list.add("Zhou Zhi Luo"); list.add("Xiao Zhao"); list.add("Xiao Zhao"); list.add(2, "Yin Su Su"); //System.out.println(list.get(3)); list.remove(1); list.set(2,"Zhang Cu Shan"); ListIterator<String> it = list.listIterator(); while (it.hasNext()) { String str = it.next(); if("Zhang Cu Shan".equals(str)) { it.add("Yang Xiao"); } } System.out.println(list); } }
2.2,LinkedList
package CollectionClass; import java.util.*; /* * linkedlist Characteristic: * 1,The underlying data structure is a bidirectional linked list * 2,The query is slow and the insertion and deletion are fast. * * * * */ public class LinkedListSample { public static void main(String[] args) { // TODO Auto-generated method stub LinkedList<String> list = new LinkedList<String>(); list.add("Yang Kang"); list.add("Guo Jing"); list.addFirst("Yang tie Xin");//Head adding list.addLast("Yang Guo");//Tail add Collection<String> list_x = new ArrayList<String>(); list_x.add("Bao Xi Wei"); list_x.add("Mu Ni Chi"); list_x.add("Little dragon maiden"); list.addAll(1, list_x);//Collection addition Collections.sort(list, new Comparator<String>() { @Override public int compare(String o1, String o2) { // TODO Auto-generated method stub return -o1.compareTo(o2); } }); System.out.println(list); //System.out.println(list.getFirst()); / / get the header //System.out.println(list.getLast()); // Get the tail } }
3,Set
3.1,HashSet
package CollectionClass; import java.util.HashSet; /* * set The characteristics of the set: * 1,Element disorder * 2,Elements are not repeatable * HashSet Characteristic: * 1,The bottom layer is realized by hash table: array + linked list / red-black tree, array index is hash value calculated (hashCode method). * Hash value is the same, calculate content through equals, content inconsistency is put into the linked list of corresponding index, after more than eight linked list elements, it is stored in red-black tree. * * */ public class HashSetSample { public static void test_hashcode() { System.out.println(new Person("Tang Seng",30).hashCode()); System.out.println(new Person("Tang Seng",30).hashCode()); System.out.println("abc".hashCode()); System.out.println("abc".hashCode()); System.out.println("Aa".hashCode());//Hash values are the same for different contents System.out.println("BB".hashCode()); } public static void test_hashset() { HashSet<String> set = new HashSet<String>(); set.add("Sun WuKong"); set.add("Zhu Bajie"); set.add("Sun WuKong"); System.out.println(set); } public static void test_custom() { HashSet<Person> set = new HashSet<Person>(); set.add(new Person("Tang Seng",30)); set.add(new Person("Sha Seng",29)); set.add(new Person("Tang Seng",30)); System.out.println(set); } public static void main(String[] args) { // TODO Auto-generated method stub test_hashcode(); //test_hashset(); test_custom(); } } class Person { private String name; private int 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; } public Person(String name, int age) { super(); this.name = name; this.age = age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } @Override public int hashCode() {//Rewriting ensures that elements with the same content have the same hash value final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Person other = (Person) obj; if (age != other.age) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } }
3.3,LinkedHashSet
package CollectionClass; import java.util.LinkedHashSet; /* * * LinkedHashSet Characteristic: * 1,Compared with hashset, there is one more list to store the order in which elements are put. * 2,Element ordering * * * */ public class LinkedHashSetSample { public static void main(String[] args) { // TODO Auto-generated method stub LinkedHashSet<String> link_set = new LinkedHashSet<String>(); link_set.add("Sun WuKong"); link_set.add("Zhu Bajie"); link_set.add("Sha Seng"); System.out.println(link_set); System.out.println(link_set); } }