In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article is about what is the difference between set and list in redis. The editor thought it was very practical, so I shared it with you as a reference. Let's follow the editor and have a look.
The difference between list and set:
1. Both List and Set are interfaces inherited from the Collection interface.
2. The biggest difference is that List can be repeated. And Set cannot be repeated. (note: although elements are not placed in order, the position of elements in set is determined by 3 and HashCode of the element, and its position is actually fixed.)
3. There are three implementation classes for List interface: LinkedList,ArrayList,Vector and two implementation classes for Set interface: HashSet (implemented by HashMap at the bottom) and LinkedHashSet
4. List is suitable for frequently adding, inserting and deleting data. But the efficiency of random sampling is relatively low.
5. Set is suitable for frequent random storage, insertion and deletion. But the efficiency in ergodic is relatively low.
Set accepts each object only once and uses its own internal sorting method (usually, you only care about whether an element belongs to Set, not its order-- otherwise you should use List). Map also keeps a copy of each element, but this is based on keys, and Map also has built-in sorting, so it doesn't care about the order in which elements are added. If the order in which you add elements is important to you, you should use LinkedHashSet or LinkedHashMap.
Summary: List has order, repetition and no sorting, set has no repetition and sorting, and map's key is the same as set. If you want to insert elements in the same order as List, use LinkedHashSet or LinkedHashMap.
The function and method of List
There are actually two types of List: the basic ArrayList, which has the advantage of random access to elements, and the more powerful LinkedList, which is not designed for fast random access, but with a more general set of methods.
List: order is the most important feature of List: it ensures that elements are maintained in a specific order. List adds a number of methods to Collection to make it possible to insert and remove elements in the middle of the List (this is recommended for LinkedList only). A List can generate ListIterator, which can be used to traverse the List in both directions, or to insert and remove elements from the middle of the List.
ArrayList: List implemented by an array. Quick random access to elements is allowed, but inserting and removing elements in the middle of the List is slow. ListIterator should only be used to traverse the ArrayList from back to front, not to insert and remove elements. Because it costs a lot more than LinkedList.
LinkedList: sequential access is optimized, with little overhead to insert and delete into the middle of the List. Random access is relatively slow. (use ArrayList instead.) There are also the following methods: addFirst (), addLast (), getFirst (), getLast (), removeFirst (), and removeLast (), which (not defined in any interface or base class) allow LinkedList to be used as a stack, queue, and bidirectional queue.
For example:
ArrayList and the use of iterators
List list = new ArrayList (); for (int I = 0; I < 10; iTunes +) {list.add (I);} Iterator 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 listIterator = list.listIterator () While (listIterator.hasNext ()) {int j = listIterator.next (); System.err.println ("listIterator==" + j);}
The function and method of Set
Set: each element stored in Set must be unique, because Set does not hold repeating elements. Elements that join 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 the order of maintenance elements.
HashSet: Set designed for quick lookup. Objects stored in HashSet must define hashCode ().
TreeSet: save the order of the Set, the underlying tree structure. Use it to extract ordered sequences from Set.
LinkedHashSet: has the query speed of HashSet, and internally uses linked lists to maintain the order of elements (the order of insertion). So when you use the iterator to iterate through the Set, the results are displayed in the order in which the elements are inserted.
Thank you for reading! So much for sharing the difference between set and list in redis. I hope the above content can be helpful to you, so that you can learn more knowledge. If you think the article is good, you can share it and let more people see it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.