Container frame diagram:

The Collection interface

>> Unordered non-repeatable HashSet: Implementation class of the Set interface

List interface: Sub-interface of Collection >> Ordered repeatable

ArrayList: Lis interface t implementation class (ArrayList) high query efficiency LinkedList: List implementation class (LinkedList) high add and delete efficiency Vector: List implementation class (array) thread safety

The Map interface

HashMap: Implementation class of the Map interface

Common methods of Collection< E > interface

The following method is generally used between two containers; However, you can also use a container, and their own hand, and differential operations, generally not this necessary. Note: The data types of both containers must be the same

The code test is as follows:

import java.util.ArrayList;
import java.util.Collection;

public class TestCollection {
	
	public static void main(String[] args) {
		
		Collection<String> coll = new ArrayList<>();		
		coll.add("1");
		coll.add("2");
		coll.add("3"); System.out.println(coll); //[1, 2, 3] coll.clear(); System.out.println(coll); //[] coll.add("A");
		coll.add("B");
		coll.add("C");
		coll.add("DE"); System.out.println(coll); //[A, B, C, DE] // coll.remove(2); Remove (col.remove (col.remove (col.remove (col.remove));"DE"); System.out.println(coll); //[A, B, C] int a = coll.size(); System.out.println(a); //3 boolean b = coll.isEmpty(); System.out.println(b); //false
		
		boolean c = coll.contains("A"); System.out.println(c); //trueObject[] d = coll.toArray(); System.out.println(d); //[Ljava.lang.Object;@15db9742 System.out.println("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =");
		
		Collection<String> coll02 = new ArrayList<>();
		coll02.add("B");
		coll02.add("C");
		coll02.add("D");
		System.out.println("coll:"+coll); //coll:[A, B, C] System.out.println("coll02:"+coll02); //coll02:[B, C, D] boolean e = coll.containsAll(coll02); System.out.println(e); //falsecoll.retainAll(coll02); System.out.println(coll); //[B, C] coll.addAll(coll02); System.out.println(coll); //[B, C, B, C, D] coll.removeAll(coll02); System.out.println(coll); / / []}}Copy the code

List< E > Common interface methods

The code test is as follows:

import java.util.ArrayList; import java.util.List; Public class TestList {public static void main(String[] args) {List<Integer> List = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); System.out.println(list); //[1, 2, 3, 4, 5] list.add(1,11); System.out.println(list); //[1, 11, 2, 3, 4, 5] // list.remove(11); List.remove (1); list.remove(1); list.remove(1); list.remove(1); list.remove(1); System.out.println(list); //[1, 2, 3, 4, 5] list.set(0,11); System.out.println(list); //[11, 2, 3, 4, 5] int a = list.get(0); System.out.println(a); //11 list.add(5); list.add(4); list.add(3); list.add(2); list.add(11); int b = list.indexOf(11); System.out.println(b); //0 int c = list.lastIndexOf(11); System.out.println(c); //9 List<Integer> list02 = new ArrayList<>(); list02.add(12); list02.add(14); list02.add(16); list.addAll(1,list02); System.out.println(list); / / [11, 12, 14, 16, 2, 3, 4, 5, 5, 4, 3, 2, 11]}}Copy the code

Distinguish the remove method in Collection interface from the remove method in List interface

import java.util.ArrayList; import java.util.List; Public class TestRemove {public static void main(String[] args) {public static void main(String[] args) { List<String> list01 = new ArrayList<>(); list01.add("1");
		list01.add("2");
		list01.add("3");
		list01.add("4");
		list01.add("5"); System.out.println(list01); //[1, 2, 3, 4, 5] // The parent interface Collection remove method list01.remove("1"); System.out.println(list01); //[2, 3, 4, 5] // list01.remove(1); System.out.println(list01); //[2, 4, 5] System.out.println("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ="); List<Integer> list02 = new ArrayList<>(); list02.add(1); list02.add(2); list02.add(3); list02.add(4); list02.add(5); list02.add(11); list02.remove(1); System.out.println(list02); //[1, 3, 4, 5, 11] /** * java.lang.IndexOutOfBoundsException: Index: 11, Size: 5 */ list02.remove(11); // The following table out of bounds exception is reported, because in int it refers to the subscript index}}Copy the code

Exercise 1: Create an array by hand using an ArrayList.

/** * Public class WriteArrayList<E> {private Object[] array; private int size; Private static final int DEFAULT = 10; publicWriteArrayList() {
		array = new Object[DEFAULT];
	}
	
	public WriteArrayList(int index) {
		array = new Object[index];
	}
	
	public void add(E data) {
		array[size++] = data;
	}
	
	@Override
	public String toString() {StringBuilder sb = new StringBuilder(); sb.append("[");
		for(int i=0; i<size; i++) {
			sb.append(array[i]+",");
		}
		sb.setCharAt(sb.length()-1,'] '); // Will output the last comma', 'Replace with'] 'Symbol. Single quotes in code cannot be changed to double quotesreturnsb.toString(); } public static void main(String[] args) { WriteArrayList<Integer> wal = new WriteArrayList<>(); wal.add(11); wal.add(12); wal.add(13); System.out.println(wal); }}Copy the code

Exercise 2: Add array expansion based on Exercise 1

Public class WriteArrayList<E> {private static Object[] array; private int size; Private static final int DEFAULT = 10; publicWriteArrayList() { array = new Object[DEFAULT]; } public WriteArrayList(int index) { array = new Object[index]; } public void add(E data) {// When to add?if(size==array.length) {// How to expand the capacity? Object[] newArray = new Object[array.length+(array.length>>1)]; // Make the analogy of 10+10/2, note the plus sign here'+'Priority over'> >'
			System.arraycopy(array, 0, newArray, 0, array.length);
			array = newArray;
		}
		array[size++] = data;
	}
	
	@Override
	public String toString() {StringBuilder sb = new StringBuilder(); sb.append("[");
		for(int i=0; i<size; i++) {
			sb.append(array[i]+",");
		}
		sb.setCharAt(sb.length()-1,'] '); // Will output the last comma', 'Replace with'] 'Symbol. Single quotes in code cannot be changed to double quotesreturn sb.toString();
	}
	
	public static void main(String[] args) {
		WriteArrayList<Integer> wal = new WriteArrayList<>();
		for(int i=0; i<40; i++) { wal.add(i); } System.out.println(wal); }}Copy the code

Exercise 3: Add set(), get() and array boundary detection

/ * * *setPublic class WriteArrayList<E> {private static Object[] array; private int size; Private static final int DEFAULT = 10; publicWriteArrayList() { array = new Object[DEFAULT]; } public WriteArrayList(int index) {// Add judgmentif(index<0) {
			throw new RuntimeException("Container capacity cannot be negative:"+index);
		} else if(index==0) {
			array = new Object[DEFAULT];
		} else{ array = new Object[index]; }} public void add(E data) {if(size==array.length) {// How to expand the capacity? Object[] newArray = new Object[array.length+(array.length>>1)]; // Make the analogy of 10+10/2, note the plus sign here'+'Priority over'> >'
			System.arraycopy(array, 0, newArray, 0, array.length);
			array = newArray;
		}
		array[size++] = data;
	}
	
	@Override
	public String toString() {StringBuilder sb = new StringBuilder(); sb.append("[");
		for(int i=0; i<size; i++) {
			sb.append(array[i]+",");
		}
		sb.setCharAt(sb.length()-1,'] '); // Will output the last comma', 'Replace with'] 'Symbol. Single quotes in code cannot be changed to double quotesreturn sb.toString();
	}
	
	public E get(int index) {
		checkIndex(index);
		return (E)array[index];
	}
	
	public void set(int index, E data) { checkIndex(index); // // valid index, index interval should meet [0,size) //if(index < 0 | | index > size - 1) {/ / / / not legitimate, manual throws an exception / / throw new RuntimeException ("Index invalid:"+index); // } array[index] = data; } public void checkIndex(int index) {public void checkIndex(int index) {public void checkIndex(int index) { Index interval should satisfy [0,size]if(index < 0 | | index > size - 1) {/ / illegal, manual throw new exception: a RuntimeException ("Index invalid:"+index);
		}
	}

	public static void main(String[] args) {
		WriteArrayList<Integer> wal = new WriteArrayList<>();
		for(int i=0; i<40; i++) {
			wal.add(i);
		}
		System.out.println(wal.get(10));
		
		wal.set(1,100);
		System.out.println(wal);
		
	}
	
}

Copy the code

Exercise 4: Add the implementation of remove(), size(), isEmpty() methods to exercise 3

Public class WriteArrayList<E> {private static Object[] array; private int size; Private static final int DEFAULT = 10; publicWriteArrayList() { array = new Object[DEFAULT]; } public WriteArrayList(int index) {// Add judgmentif(index<0) {
			throw new RuntimeException("Container capacity cannot be negative:"+index);
		} else if(index==0) {
			array = new Object[DEFAULT];
		} else{ array = new Object[index]; }} public void add(E data) {if(size==array.length) {// How to expand the capacity? Object[] newArray = new Object[array.length+(array.length>>1)]; // Make the analogy of 10+10/2, note the plus sign here'+'Priority over'> >'
			System.arraycopy(array, 0, newArray, 0, array.length);
			array = newArray;
		}
		array[size++] = data;
	}
	
	@Override
	public String toString() {StringBuilder sb = new StringBuilder(); sb.append("[");
		for(int i=0; i<size; i++) {
			sb.append(array[i]+",");
		}
		sb.setCharAt(sb.length()-1,'] '); // Will output the last comma', 'Replace with'] 'Symbol. Single quotes in code cannot be changed to double quotesreturn sb.toString();
	}
	
	public E get(int index) {
		checkIndex(index);
		return (E)array[index];
	}
	
	public void set(int index, E data) { checkIndex(index); // // valid index, index interval should meet [0,size) //if(index < 0 | | index > size - 1) {/ / / / not legitimate, manual throws an exception / / throw new RuntimeException ("Index invalid:"+index); // } array[index] = data; } public void checkIndex(int index) {public void checkIndex(int index) {public void checkIndex(int index) { Index interval should satisfy [0,size]if(index < 0 | | index > size - 1) {/ / illegal, manual throw new exception: a RuntimeException ("Index invalid:"+index); }} public void remove(int index) {numMoved = array.length-index - 1;if(numMoved>0) {// ArrayCopy (array, index+1, array, index, numMoved); } array[--size] = null; } public void remove(E data) {for(int i=0; i<size; i++) {
			if(array[i].equals(data)) {
				remove(i);
			}
		}
	}
	
	public int size() {
		return size;
	}
	
	public boolean isEmpty() {
		return size==0?true:false;
	}
	
	public static void main(String[] args) {
		WriteArrayList<Integer> wal = new WriteArrayList<>();
		for(int i=0; i<40; i++) { wal.add(i); } System.out.println(wal.get(10)); Wal. Set (1100); System.out.println(wal); wal.remove(2); System.out.println(wal); WriteArrayList<String> s = new WriteArrayList<String>(); s.add("A");
		s.add("B");
		s.add("C");
		s.add("D");
		System.out.println(s);
		s.remove(1);
		System.out.println(s);
		s.remove("D"); System.out.println(s); System.out.println(s.size()); System.out.println(s.isEmpty()); }}Copy the code

Learn the StringBuilder class and its append() and setCharAt() methods to learn the arrayCopy () method: System. Arraycopy (the number of elements to be copied from, to which array to copy, where to paste, and how many elements to copy); Rewrite the ArrayList implementation class itself

Lessons from Exercises 1 to 4:

The following code is independently done by me. The essential difference with the code in Exercise 4 is that size and array.length represent specific meanings. In the following code, size represents the number of elements in the container and array.length represents the size of the container. Due to array expansion, the size of the container is usually larger than the number of elements. Note the difference between == and equals.

public class WriteArrayList02<E> {

	private Object[] array;
	private int size;
	private static final int DEFAULT = 10;
	
	public WriteArrayList02() {
		array = new Object[DEFAULT];
	}
	
	public WriteArrayList02(int index) {
		if(index == 0) {
			array = new Object[DEFAULT];
		}else if(index<0) {
			throw new RuntimeException("Index cannot be negative:"+index);
		}else {
			array = new Object[index];
		}
	}
	
	public void add(E element) {
		if(size==array.length) {
			Object[] newArray = new Object[array.length+(array.length>>1)];
			System.arraycopy(array, 0, newArray, 0, array.length);
			array = newArray;
		}
		array[size++] = element;
	}
	
	public void set(int index, E element) {
		checkIndex(index);
		array[index] = element;
	}
	
	public E get(int index) {
		checkIndex(index);
		return (E)array[index];
	}
	
	public void checkIndex(int index) {
		if(index<0||index>=size) {
			throw new RuntimeException("Index exception:"+index);
		}
	}
	
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("[");
		for(int i=0; i<size; i++) {
			sb.append(array[i]+",");
		}
		sb.setCharAt(sb.length()-1,'] ');
		return sb.toString();
	}
	
	public void remove(E element) {
		for(int i=0; i<size; i++) {
			if(array[i].equals(element)) { remove(i); } } } public void remove(int index) { System.out.println(array.length); //1,2,3,4,5,6,7 int numCopy = size-index - 1;if(numCopy<0) {
			throw new RuntimeException("Index exception:"+index);
		}else if(numCopy==0) {
			array[size] = null;
		}else {
			System.arraycopy(array, index+1, array, index, numCopy);
		}	
		//array[size-1] = null;
		size--;
		System.out.println(array.length);
	}
	
	public int size() {
		return size;
	}
	
	public boolean isEmpty() {
		return size==0?true:false;
	}
	
	public static void main(String[] args) {
		WriteArrayList02<String> wal = new WriteArrayList02<>();
		for(int i=0; i<40; i++) {
			wal.add(i+"");
		}
		System.out.println(wal);
		
		wal.set(1,"11");
		System.out.println(wal);
		
		System.out.println(wal.get(39));
		
		wal.set(39,"12");
		System.out.println(wal);
		
		wal.remove(0);
		System.out.println(wal);
//		wal.remove(38);
//		System.out.println(wal);
		wal.remove("12"); System.out.println(wal); System.out.println(wal.size()); System.out.println(wal.isEmpty()); }}Copy the code

LinkedList stores the structure

The last

Thank you for reading here, after reading what do not understand, you can ask me in the comments section, if you think the article is helpful to you, remember to give me a thumbs up, every day we will share Java related technical articles or industry information, welcome to pay attention to and forward the article!