Home > Database > Redis > What are the differences between set and list in redis?

What are the differences between set and list in redis?

Release: 2019-07-08 09:18:22
Original
14200 people have browsed it

What are the differences between set and list in redis?

The difference between list and set:

1. Both List and Set interfaces inherit from the Collection interface.

2. The biggest difference is that List can be repeated. And Set cannot be repeated. (Note: Although elements are not put in order, the position of the element in the set is determined by the element's 3. HashCode, and its position is actually fixed)

3. The List interface has three implementation classes: LinkedList, ArrayList, Vector, and Set interfaces have two implementation classes: HashSet (the bottom layer is implemented by HashMap), LinkedHashSet

4. List is suitable for frequently appending data, inserting, and deleting data. However, the efficiency of random number retrieval is relatively low.

5. Set is suitable for frequent random storage, insertion, and deletion. But the efficiency is relatively low when traversing.

Set accepts each object only once and uses its own internal sorting method (usually, you only care about whether an element belongs to the Set, not its order - otherwise you should use a List). Map also saves a copy of each element, but this is based on "key". Map also has built-in sorting, so it does not care about the order in which elements are added. If the order of adding elements is important to you, you should use LinkedHashSet or LinkedHashMap.

Summary: List has order, duplication and no sorting, set has no duplication and sorting, and the key of map is the same as set. If you want to insert elements in the same order as List, please use LinkedHashSet or LinkedHashMap.

Functional methods of List

There are actually two kinds of List: one is the basic ArrayList, whose advantage lies in random access to elements, and the other is the more powerful LinkedList, which is not designed for designed for fast random access, but has a more general set of methods.

List: Order is the most important feature of List: it ensures that the specific order of elements is maintained. List adds many methods to Collection, making it possible to insert and remove elements from the middle of the List (this is only recommended for LinkedList.) A List can generate a ListIterator, which can be used to traverse the List in two directions, and can also be inserted and moved from the middle of the List. Remove elements.

ArrayList: List implemented by array. Allows fast random access to elements, but inserts and removes elements from the middle of the List very slowly. ListIterator should only be used to traverse the ArrayList from back to front, not to insert and remove elements. Because that is much more expensive than LinkedList.

LinkedList: Sequential access is optimized, and the overhead of inserting and deleting into the middle of the List is not large. Random access is relatively slow. (Use ArrayList instead.) Also has the following methods: addFirst(), addLast(), getFirst(), getLast(), removeFirst() and removeLast(), which (not defined in any interface or base class) make LinkedList can be used as a stack, queue and deque.

For example:

Using ArrayList and Iterator

                List<Integer> list = new ArrayList<>();
		for (int i = 0; i < 10; i++) {
			list.add(i);
		}
		Iterator<Integer> iterator = list.iterator();
		while (iterator.hasNext()) {
			int i = iterator.next();
			System.out.println("iterator==" + i);
		}
		for(Integer i: list) {
			System.out.println("i=="+ i);
		}
                ListIterator<Integer> listIterator = list.listIterator();
		while (listIterator.hasNext()) {
			int  j = listIterator.next();
			System.err.println("listIterator==" + j);
		}
Copy after login

Functional methods of Set

Set: Each element stored in Set must be Unique because Set does not save duplicate elements. Elements added to the Set must define the equals() method to ensure the uniqueness of the object. Set and Collection have exactly the same interface. The Set interface does not guarantee that the order of elements is maintained.

HashSet: Set designed for quick search. Objects stored in HashSet must define hashCode().

TreeSet: Set to save the order, the bottom layer is a tree structure. Use it to extract an ordered sequence from a Set.

LinkedHashSet: Has the query speed of HashSet, and internally uses a linked list to maintain the order of elements (the order of insertion). So when using an iterator to traverse the Set, the results will be displayed in the order in which the elements were inserted.

For more Redis related knowledge, please visit the Redis usage tutorial column!

The above is the detailed content of What are the differences between set and list in redis?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template