The difference between Set and List in Java

Main differences between Set and List

  1. Duplicate elements are allowed to be inserted in the List, while duplicate elements are not allowed in the Set.
  2. Related to the storage order of elements, List is an ordered Set, which will retain the order in which elements are inserted, and Set is an unordered Set.
  3. List can be accessed by subscript, but Set cannot.

List interface

  1. ArrayList (array implementation): allows fast random access to elements. When inserting or deleting elements from the middle of ArrayList, the array needs to be copied, moved and expensive. Therefore, it is suitable for random search and traversal, not for insertion and deletion.
  2. LinkedList (linked List implementation): it is very suitable for dynamic insertion and deletion of data. The speed of random access and traversal is relatively slow (you can use ArrayList for traversal and access). It also provides methods not defined in the List interface, which are specially used to operate header and footer elements, and can be used as stacks, queues and bidirectional queues.
  3. Vector (array implementation): support thread synchronization. Only one thread can write vector at a time to avoid inconsistency caused by multiple threads writing at the same time. However, realizing synchronization requires a high cost. Therefore, accessing it is slower than accessing ArrayList. Vector belongs to the thread safety level, but vector is not used in most cases, because thread safety requires greater system overhead (the related methods are very similar to ArrayList, and the methods are decorated with synchronized).

Set interface

  1. HashSet: when an element is stored in the HashSet combination, the HashSet will call the hashCode() method of the object to get the hashCode value of the object, and then determine the storage location of the object in the HashSet according to the hashCode value (why and how to ensure that the HashSet is not repeated). That is to say, the criterion for judging the equality of two elements in the HashSet set is that the two objects are equal through the equals method, and the return values of the hashCode() method of the two objects are equal. The arrangement order of elements cannot be guaranteed, and the order may change; Collection elements can be null, but only one null can be put in;
  2. LinkedHashSet: LinkedHashSet also determines the storage location of elements according to the hashCode value of elements, but it also uses a linked list to maintain the order of elements. This makes the elements appear to be saved in insertion order, that is, when traversing the collection, the LinkedHashSet will access the elements of the collection in the order in which they are added. LinkedHashSet performs better than HashSet when iteratively accessing all elements in the Set, but its performance is slightly inferior to HashSet when inserting.
  3. TreeSet: TreeSet is the only implementation class of SortedSet interface. The underlying data structure is red black tree. TreeSet can ensure that the collection elements are in sorting state. TreeSet supports two sorting methods, natural sorting and custom sorting. Natural sorting is the default sorting method.

Comparison code case of ArrayList and TreeSet

package cn.tedu.test6;
import java.util.*;
public class SetVsList {
	public static void main(String[] args) {
		//Comparison between TreeSet and ArrayList
		Set<Integer> set = new TreeSet<>();
		List<Integer> list = new ArrayList<>();
		set.add(1); 	list.add(1);
		set.add(2);		list.add(2);
		set.add(3);		list.add(3);
		set.add(4);		list.add(4);
		set.add(5);		list.add(5);
		System.out.println("set: "+set); 	//Output set
		System.out.println("list"+list);	//Output list
		set.add(2);//Duplicate data will not be added
		list.add(2);//Duplicate data can be added
		System.out.println("After adding data:");
		System.out.println("set: "+set);
		System.out.println("list: "+list);
		System.out.println("Contains data 5:");
		System.out.println(set.contains(5));	//Does it contain data 5
		System.out.println(list.contains(5));
		System.out.println("After removing data:");
		set.remove(4);	//Remove data 4
		list.remove(4);	//Remove data with index 4
		System.out.println("set: "+set);
		System.out.println("list: "+list);
		System.out.println("get data(Only for list): ");
		System.out.println("Specify subscript to get content:"+list.get(1)); //Get the data with list subscript 1. set has no get() method
		System.out.println("Specify content acquisition subscript:"+list.indexOf(2));//Get the location where the list data content is 2
		System.out.println("set size: "+set.size());		//Output set size	
		System.out.println("list size: "+list.size());	//Output list size
		System.out.print("ergodic set After:");
		for(Integer value:set) {			//Traversal set
			System.out.print(value+" ");
		}
		System.out.println();
		System.out.print("ergodic list After:");
		for(Integer value:list) {			//Traversal list
			System.out.print(value+" ");
		}
	}
}

The results show that:
set: [1, 2, 3, 4, 5]
list[1, 2, 3, 4, 5]
After adding data:
set: [1, 2, 3, 4, 5]
list: [1, 2, 3, 4, 5, 2]
Contains data 5:
true
true
 After removing data:
set: [1, 2, 3, 5]
list: [1, 2, 3, 4, 2]
get data(Only for list): 
Specify subscript to get content: 2
 Get subscript for specified content: 1
set size: 4
list size: 5
 ergodic set After: 1 2 3 5 
ergodic list After: 1 2 3 4 2 

 

Keywords: Java queue set arraylist LinkedList

Added by OMorchoe on Mon, 31 Jan 2022 14:21:17 +0200