Collective system:
-------------- | Collection singleton set's root interface
If a List is a collection class that implements the List interface, it has the characteristics of orderliness and repeatability.
The bottom layer of the -------------------------------| ArrayList ArrayList maintains an Object array implementation. Features: Quick query speed, slow addition and deletion.
The bottom layer of LinkedList LinkedList is realized by using linked list data structure, which has the characteristics of slow query speed and fast increase and deletion.
The bottom layer of Vector also maintains an array implementation of Object, which is the same as ArrayList, but Vector is thread-safe and inefficient.
If Set is a collection class that implements Set interface, it has the characteristics of disorder and non-repeatability.
List
Specific Method in List Interface
Add to
- add(int index, E element)
- addAll(int index, Collection<? extends E> c)
Obtain:
- get(int index)
- indexOf(Object o)
- lastIndexOf(Object o)
- subList(int fromIndex, int toIndex)
Amendment:
- set(int index, E element)
iteration
- listIterator()
The unique method of List interface has its own characteristics: the operation methods all have index values, only the collection classes under List interface have index values, and the collection classes under other interfaces have no index values.
class Person{ private String name; private String cardId; public Person() { } public Person(String cardId,String name) { this.name = name; this.cardId = cardId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCardId() { return cardId; } public void setCardId(String cardId) { this.cardId = cardId; } @Override public String toString() { return "id:"+this.cardId+",name:"+this.name; } @Override public boolean equals(Object obj) { Person p = (Person)obj; return p.cardId == this.cardId; } } public class Test { public static void main(String[] args) { Person p1 = new Person("1","Zhang San"); Person p2 = new Person("2","Li Si"); Person p3 = new Person("3","WangTwo"); Person p4 = new Person("4","Pock"); List<Person> list1= new ArrayList<>(); list1.add(p1); list1.add(p2); list1.add(p3); List<Person> list2= new ArrayList<>(); list2.add(p1); //list1.addAll(list2); //indexOf, like lastIndexOf and contains, is also an equals that calls Object in comparison. //System. out. println (list 1. indexOf (new Person ("1", "Li Si")); //System.out.println(list1.lastIndexOf(new Person("1", "Li Si")); //System.out.println(list1.subList(0,1)); //list1.set(1,p3); //System.out.println(list1); Iterator<Person> iterator = list1.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } } }
ListIterator-specific methods:
Add to:
- hasPrevious() determines whether the previous element exists.
- hasNext() determines whether the next element exists.
- previous() The current pointer first moves up a unit, and then takes out the element that the current pointer points to.
- next(); first take out the element that the current pointer points to, and then move the pointer down a unit.
- Add (E) inserts the current element into the position pointed by the current pointer.
- Set (E) replaces the last element returned by the iterator.
Iterators should pay attention to variable elements: in the process of iterator iteration, it is not allowed to use set objects to change the number of elements in the set. If it is necessary to add or delete, it can only use iterator method to operate. If it has used set objects to change the number of elements in the set, it will come out. Now Concurrent ModificationException exception.
class Person{ private String name; private String cardId; public Person() { } public Person(String cardId,String name) { this.name = name; this.cardId = cardId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCardId() { return cardId; } public void setCardId(String cardId) { this.cardId = cardId; } @Override public String toString() { return "id:"+this.cardId+",name:"+this.name; } @Override public boolean equals(Object obj) { Person p = (Person)obj; return p.cardId == this.cardId; } } public class Test { public static void main(String[] args) { Person p1 = new Person("1","Zhang San"); Person p2 = new Person("2","Li Si"); Person p3 = new Person("3","WangTwo"); Person p4 = new Person("4","Pock"); List<Person> list1= new ArrayList<>(); list1.add(p1); list1.add(p2); list1.add(p3); list1.add(p4); ListIterator<Person> listIterator = list1.listIterator(); while(listIterator.hasNext()){ listIterator.next(); } while (listIterator.hasPrevious()){ System.out.println(listIterator.previous()); } } }
class Person{ private String name; private String cardId; public Person() { } public Person(String cardId,String name) { this.name = name; this.cardId = cardId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCardId() { return cardId; } public void setCardId(String cardId) { this.cardId = cardId; } @Override public String toString() { return "id:"+this.cardId+",name:"+this.name; } @Override public boolean equals(Object obj) { Person p = (Person)obj; return p.cardId == this.cardId; } } public class Test { public static void main(String[] args) { Person p1 = new Person("1","Zhang San"); Person p2 = new Person("2","Li Si"); Person p3 = new Person("3","WangTwo"); Person p4 = new Person("4","Pock"); List<Person> list1= new ArrayList<>(); list1.add(p1); list1.add(p2); list1.add(p3); list1.add(p4); ListIterator<Person> listIterator = list1.listIterator(); while(listIterator.hasNext()){ //ListIterator. add (new Person ("p", "Mr. p"); listIterator.next(); listIterator.set(new Person("p","p Sir")); } System.out.println(list1); } }
Return data when add
[id:p,name:p, id:1,name: Zhang San, id:p,name:p, Mr. id:2,name: Li Si, id:p,name:p, Mr. id:3,name: Wang II, id:p,name:p, Mr. id:4,name: Ma Zi]
Return data when set
[id:p,name:p, id:p,name:p, id: p, id: p, id:p,name:p, id: p]
Three ways are used to traverse the elements of a collection.
- The first is to use get method to traverse.
- The second is to use iterator positive traversal.
- The third one is to use foreach traversal.
class Person{ private String name; private String cardId; public Person() { } public Person(String cardId,String name) { this.name = name; this.cardId = cardId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCardId() { return cardId; } public void setCardId(String cardId) { this.cardId = cardId; } @Override public String toString() { return "id:"+this.cardId+",name:"+this.name; } @Override public boolean equals(Object obj) { Person p = (Person)obj; return p.cardId == this.cardId; } } public class Test { public static void main(String[] args) { Person p1 = new Person("1","Zhang San"); Person p2 = new Person("2","Li Si"); Person p3 = new Person("3","WangTwo"); Person p4 = new Person("4","Pock"); List<Person> list1= new ArrayList<>(); list1.add(p1); list1.add(p2); list1.add(p3); list1.add(p4); //Way 1: Use get method to traverse for (int i = 0; i<list1.size();i++){ System.out.println(list1.get(i)); } System.out.println("===================="); //Mode 2: Positive order traversal using iterators ListIterator<Person> listIterator = list1.listIterator(); while(listIterator.hasNext()){ System.out.println(listIterator.next()); } System.out.println("===================="); //Mode 3: Use foreach traversal for (Person p:list1) { System.out.println(p); } } }
ArrayList
ArrayList-specific methods
- Ensure Capacity (int min Capacity)
- trimToSize()
Assurance Capacity (int) method can expand the ArrayList low-level array. The function is called by display. If the parameter is larger than 1.5 times the length of the low-level array, the capacity of the array will be expanded to this parameter value. If the parameter is less than 1.5 times the length of the low-level array, the capacity will be expanded. To 1.5 times the length of the low-level array. All in all, just remember that this function can expand low-level arrays. Making good use of this function at the right time will improve the performance of programs we write, such as the following two pieces of code.
public class Test { public static void main(String[] args) { final int N=1000000; Object obj=new Object(); ArrayList list1=new ArrayList(); long start=System.currentTimeMillis(); for(int i=0;i<N;i++){ list1.add(obj); } System.out.println(System.currentTimeMillis()-start); ArrayList list2=new ArrayList(); long start2=System.currentTimeMillis(); list2.ensureCapacity(N);//Expansion of low-level arrays for(int i=0;i<N;i++){ list2.add(obj); } System.out.println(System.currentTimeMillis()-start2); }
The efficiency of the second paragraph is obviously much higher than that of the first paragraph, because if the first paragraph does not expand to the desired maximum capacity at one time, it will expand bit by bit in the process of adding elements. It will waste a lot of time to know that the expansion of arrays is to copy arrays. If you already know how many elements a container might contain, it's best to show that the call ensureCapacity is one-time scaled in place.
The bottom layer of ArrayList maintains an Object array implementation. With a parametric constructor, the default capacity of the Object array is 10, and automatically increases by 0.5 times when the length is insufficient.
The trimToSize method sets the length of the underlying array to the actual capacity of the ArrayList, and the dynamic growth of the redundant capacity is removed. Let's see what happens.
public static void main(String[] args) { ArrayList al = new ArrayList(10); for(int i=0;i<10;i++){ al.add(i); } al.add(1); al.trimToSize(); System.out.println(al); }
An ArrayList with an initial capacity of 10, al.add(1); then, when trimToSize is not executed, the debug content is found and the length of the underlying array is found to be 15.
When trimToSize is executed, the contents of debug are found and the length of the underlying array is found to be 11.
That is to say, this method sets the array of elementData to the actual capacity of ArrayList, and the dynamic growth of redundant capacity is removed.
When a small partner responded that he debug, he didn't see elementData length change to 15. Here I guess you should use idea development tool. Now I'll give you a debug view of idea, which is the default way for small science popularization. Idea shows List type by default. It's too smart, so it only shows you what users care about, so that your focus is on the most important data. We only need to do the next step to see more detailed information:
LinkedList
Linkedlist's unique approach:
Method Introduction
- addFirst(E e)
- addLast(E e)
- getFirst()
- getLast()
- removeFirst()
- removeLast()
data structure
- Stack (1.6): Mainly used to realize the storage of stack data structure.
First in, last out
push()
pop() - Queues (double-ended queue 1.5) are designed to allow us to use LinkedList to simulate the storage of queue data structures.
FIFO
offer()
poll()
Returns an inverse iterator object
- descendingIterator() returns an inverted iterator object
public class Test { public static void main(String[] args) { LinkedList list= new LinkedList(); list.add("Zhang San"); list.add("Li Si"); list.add("Wang Wu"); /* list.addFirst("Dog Doll; // Add elements to the top of the collection. list.addLast("Dog Leftovers; // Add elements to the end of the collection. System.out.println("Gets the element at the head of the collection: "+list. getFirst ();" System.out.println("Gets the element at the end of the collection: "+ list. getLast ();" System.out.println("Delete the header element in the collection and return: "+ list. removeFirst ();" System.out.println("Delete the endpoints in the collection and return: "+ list. removeLast ();" list.push("Dog Doll; // Insert this element at the beginning of this collection. System.out.println("Delete the first element of the collection: "+list.pop();//Remove and return the first element in the collection list.offer("Dog Leftovers; System.out.println("Delete the first element of the collection: "+list. poll ();" System.out.println("Elements in a collection: "+list"; */ Iterator it = list.descendingIterator(); while(it.hasNext()){ System.out.println(it.next()); } } }
LinkedList is used to store stack data structure and queue data structure.
class Team{ private LinkedList list; public Team() { this.list = new LinkedList(); } public int size(){ return list.size(); } public boolean offer(Object o){ return list.offer(o); } public Object poll(){ return list.poll(); } } class Stack{ private LinkedList list; public Stack() { this.list = new LinkedList(); } public int size(){ return list.size(); } public void push(Object o){ list.push(o); } public Object pop(){ return list.pop(); } } public class Test { public static void main(String[] args) { Team list = new Team(); list.offer("Zhang San"); list.offer("Li Si"); list.offer("WangTwo"); int size = list.size(); for (int i = 0; i<size;i++){ System.out.println(list.poll()); } } }