The main purpose of learning collections is to understand the source code and choose the appropriate collection according to the actual situation

one : aggregate

Set refers to the aggregation of a class of things with the same properties , Sort of like an array , But it is different from array .

The difference between sets and arrays :

(1) Fixed array length , And the set length is dynamic .

(2) An array can only store one data type , Collections can store multiple data types .

(3) Increase with array / Deleting elements is troublesome , And collections provide add,remove,set,get Etc , It makes it convenient to add and delete .

A collection is a container

Collection Is a single column set ,Map Is a two column set

(1)List,Set,Queue Differences between

List The set is ordered , Subscript , Repeatable

Queue Set ordered , Subscript , Repeatable

Set The set is unordered , No subscript , Not repeatable

(2)List The main implementation classes of are :ArrayList,LinkedList,Vector  

ArrayList The bottom layer is realized by array

LinkedList The bottom layer is realized through linked list

Vector The bottom layer is realized by array

(3)Set The implementation class of is :Hash Set,LinkedHashSet,TreeSet     

Hash Set The bottom layer is realized through hash table

LinkedHashSet Through linked list + Hash table implementation

TreeSet The bottom layer is realized by tree structure

two :Collection Common methods ( Not commonly used ones are not listed )
int size(); // Calculate the length of the set boolean isEmpty(); // Judge whether the set is empty boolean contains(Object
o); // Judge whether the set contains the specified object Iterator<E> iterator(); // Returns an iterator for example : public class Demo {
public static void main(String[] args) { Collection<String> arr = new
ArrayList<>(); arr.add(" Hello "); arr.add(" tearing "); arr.add("OK"); Iterator<String>
iterator = arr.iterator(); while(iterator.hasNext()){
System.out.println(iterator.next()+" "); } } } Object[] toArray(); // Convert a collection to an array
for example : public class Demo { public static void main(String[] args) {
Collection<String> arr = new ArrayList<>(); arr.add(" Hello "); arr.add(" tearing ");
arr.add("OK"); Object[] array = arr.toArray(); for (Object s: array) {
System.out.print(s+" "); } } } boolean add(E e); // Add elements to the collection boolean
remove(Object o); // Delete the specified element in the collection boolean containsAll(Collection<?> c);
// Determine whether to include all elements in the set boolean addAll(Collection<? extends E> c); // Set c All elements of are added to the collection
boolean removeAll(Collection<?> c); // Delete all sets in the set c Elements in boolean
retainAll(Collection<?> c); // Delete set except in set c All elements except , If the collection changes after deletion, return true, No, return false
void clear(); // Empty collection boolean equals(Object o); // Judge whether the element is equal to the specified element int hashCode();
// Returns the hash code value of this collection
three : Collection framework ( very important )

 

  four .Collections Tools

Collections Is an operation Set,List,Map Tool classes for collections such as .Collections Provides a series of static methods to sort collection elements , Query and modify operations

The following is a list of commonly used Collections method
reverse(List): reversal List Order of elements in shuffle(List): heap List Random sorting of collection elements
sort(List): Assign according to the natural order of elements List Set elements are sorted in ascending order
sort(List,Comparator): According to the specified Comparator Generated sequence pair List Sort collection elements
swap(List,int,int): Will specify list In set i Element and at j Exchange elements at Object
max(Collection): According to the natural order of elements , Returns the largest element in a given collection Object
max(Collection,Comparator): according to Comparator Specified order , Returns the largest element in a given collection Object
min(Collection): According to the natural order of elements , Returns the smallest element in a given set Object
min(Collection,Comparator): according to Comparator Specified order , Returns the smallest element in a given set int
frequency(Collection,Object): Returns the number of occurrences of the specified element in the collection void copy(List dest,List
src): take src Copy class content in to dest in ( requirement dest The length of should not be less than src Length of , Otherwise, an array subscript out of bounds exception will be thrown ) boolean
replaceAll(List list,Object oldVal,Object newVal): Replace with new value List All old values of the object public
class Collections_ { public static void main(String[] args) { List<String> list
= new ArrayList<>(); list.add(" Cao Zhi "); list.add(" Cao Pi "); list.add(" Guan Yu ");
list.add(" Zhang Fei "); list.add(" Guan Yu "); List<Integer> list1 = new ArrayList<>();
list1.add(2); list1.add(1); list1.add(12); list1.add(0);
//reverse(List): reversal List Elements in System.out.print(list+" ");
Collections.reverse(list); System.out.print(list+" ");
//shuffle(List): heap List Random sorting of collection elements for (String s: list) {
Collections.shuffle(list); System.out.print(list+" "); }
//sort(List): Assign according to the natural order of elements List Set elements are sorted in ascending order Collections.sort(list1);
System.out.print(list1+" ");
//sort(List,Comparator): According to the specified Comparator Generated sequence pair List Sort collection elements
Collections.sort(list1, new Comparator<Integer>() { @Override public int
compare(Integer o1, Integer o2) { return o2-o1; } }); System.out.print(list1+"
"); //swap(List,int,int): Will specify list In set i Element and at j Exchange elements at Collections.swap(list,1,3);
System.out.print(list+" "); //Object max(Collection): According to the natural order of elements , Returns the largest element in a given collection
Integer max = Collections.max(list1); System.out.println(max); //Object
max(Collection,Comparator): according to Comparator Specified order , Returns the largest element in a given collection Integer max1 =
Collections.max(list1, new Comparator<Integer>() { @Override public int
compare(Integer o1, Integer o2) { return o1 - o2; } });
System.out.println(max1); //int frequency(Collection,Comparator): Returns the number of occurrences of the specified element in the collection
System.out.println(Collections.frequency(list," Guan Yu ")); //void copy(List
dest,List src): take src Copy class content in to dest in ( requirement dest The length of should not be less than src Length of , Otherwise, an array subscript out of bounds exception will be thrown )
List<Integer> list3 = new ArrayList<>(); for (int i = 0; i < list1.size(); i++)
{ list3.add(0); } Collections.copy(list3,list1); System.out.print(list3+" ");
//boolean replaceAll(List list,Object oldVal,Object newVal): Replace with new value List All old values of the object
Collections.replaceAll(list," Guan Yu "," Zhugeliang "); System.out.print(list+" "); } }

Technology