-interfaces: Set interface HashSet (main implementation class), LinkedHashSet, TreeSet
> Unordered storage. The common methods in Set are defined under Collection (unordered! = Random: the order of addition is different from the order of output, but the result obtained after running is ordered. The true disorder refers to the unordered position of elements in the underlying storage, underlying implementation, such as the linked list in C Language)> no
storage location of elements based on the HashCode value of the elements. However, it can also maintain the order of adding elements using a linked list, this allows elements to be saved in the insert sequence.
Public class LinkHashSetTest {public static void main (String [] args) {LinkedHashSet lh = new LinkedHashSet (); lh. add (1); lh. add (2); lh. add (3); // output: [1, 2, 3] System. out. println (lh); lh. remove (1); lh. add (1); // output: [2, 3, 1] System. out. println (lh );}}
3.
additions do not go in Note: The same judgment of non-repeatability, according to the Equals () and Hashcode () methods in the class where the elements in the set are added, so be sure to override the Equals () and Hashcode () methods. |---HashSet: Main Implementation class |---linkedhashset: HashSet subclasses, when we traverse the collection elements, are carried out in the order in which they are added, with frequent traversal, with fewer additions and insertions |---
()); System.out.println (h); }}Results:Start: size=54.01st32ndMon Feb 20:52:17 CST 2015After removing an element: size=4[4.0, 1st, 3, Mon Feb 20:52:17 CST 2015]4.2 TreeSetTreeSet describes a variant of set-a collection of functions such as sorting, which is automatically inserted into an ordered sequence of objects by a comparison rule when the object element is added to the collection, and guarantees that the set elements are organized in ascending order.Examples of Use:Package Com.co
be null, but can have only one null element.
TreeSet
Several characteristics of TreeSet:
1. There can be no duplicate elements in the TreeSet;
2. TreeSet has a sort function, and the default is to arrange by natural sort
3. elements in TreeSet must implement the comparable
Java-9.9 set
In this chapter, we will discuss some sets.
Because the objects in the set are non-repetitive, it determines that the functions searched and queried in the set are frequently used. Therefore, we generally use hashset, because it performs special Optimization on the search.
1. HashSet
Because HashSet performs the hash operation on the object, because its search is based on the hash code, its output is unordered.
package com.ray.ch09;import java.util.Arrays;import java.util.HashSet;i
In Java container Study Notes (1), I have outlined the basic concepts and interface implementation of collection, and summarized the implementation and usage of an important subinterface list and its sub-classes.
This article mainly summarizes the usage of the Set interface and its implementation classes, including hashset (unordered and non-repeated), javashashset (sorted and non-repeated in order), and treeset (sorted and non-repeated in red/black t
of the element is the same, the system checks whether equals is true. If the hashcode value of an element is different, equals is not called. (when performing rewrite on the hashcode and equals methods, you must pay attention to the return value and parameter list. You cannot change the value at will, otherwise, it will not be re-written, but will be reloaded)
Note: For operations such as determining whether an element exists and deleting it, the dependent methods are the hashcode and equals me
s2 is a subset of s1, true is returned; otherwise, false is returned.
S1.AddAll(S2)-store the Union of s1 and s2 to s1
S1.RetainAll(S2)-store the intersection of s1 and s2 to s1
S1.RemoveAll(S2)-remove elements in s1 that overlap with s2
The Java platform provides three general implementation classes: HashSet, TreeSet, and javashashset.
HashSetUsing a hash table as a container is the best implementation class, but it does not guarantee the ite
;protected String email;public UserDO() {}public UserDO(String name , String email) {this.name = name;this.email = email;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}}
Two attributes are defined: name and email. In this case, we want to sort by name. Then we define the sorting class as follows:
import java.text.Collator;import java.util.Comparator;public
Set:
Architecture:
Set: The elements are unordered (the order in which they are stored and retrieved is not necessarily the same) and cannot be repeated.
| --Hashset: The underlying data structure is a hash table.
| --Treeset: Underlying data structure Binary Tree.
The Set set function is consistent with the collection function.
How does hashset ensure the uniqueness of elements?
Is done through two methods of elements, hashcode and equ
stacks and queues.
In general, which List interface is used for the Implementation class?
If you want to add or delete files quickly, consider using the shortlist
If fast query is required, use ArrayList
If thread security is required, use Vector.
| ---> Set: Set. elements are unordered (because no index is available) and cannot be duplicated. It can have a null element.
| ---> HashSet (JDK1.2): the underlying data structure is a hash table, which features fast access sp
plus 1:2
get Key1 Value: 2 key2 value
minus 1:1
Gets the Key2 value: 1 adds the
key1 value to the integer 5:7
gets the value of Key1:7
Subtracts the Key2 value from the integer 5:-4
gets the Key2 value:-4
The has this command in redis2.6 or above: Incrbyfloat, which is not supported in the value of the key store plus the floating-point number amount,jedis-2.1.0. List
@Test public void Testlist () {jedis.flushdb ();
System.out.println ("=========== Add a list===========");
Jedis.lpu
attribution, and you can easily ask if an object is in a set. Set has exactly the same interface as collection, so there is no additional functionality. In fact set is collection, but behavior is different.
The main features of the set interface are HashSet, TreeSet, and Linkedhashset. Each of the same items is saved only one copy. They also have different points, the difference is as follows:
1.HashSet:
HashSet uses a rather complex way of storing
classLikedhashset is a subclass of HashSet, which also determines where an element is stored based on its hashcode value, but it can use a linked list to maintain the order in which elements are added, allowing elements to be saved in the order in which they are inserted. Public classLinkhashsettest { Public Static voidMain (string[] args) {linkedhashset LH=NewLinkedhashset (); Lh.add (1); Lh.add (2); Lh.add (3); //output: [1, 2, 3]System.out.println (LH); Lh.remove (1); Lh.add (1); //output: [
returns the content in the set as an array.10. The comparator object provides a sorting method to sort collection elements in the order of different colletions. The reverseorder method returns a comparator object, which represents the reverse sorting of the set. You can also implement your own sorting method on the premise that the comparator interface is implemented and the compare method of the comparator interface is implemented.11. The shuffle algorithm sorts the elements in the list random
two identical elements are inserted into the set, the latter element will not be inserted.
Set can be roughly divided into two categories: unordered set and ordered set, unordered set including HashSet and Linkedhashset, and sort set mainly refers to TreeSet. where HashSet and linkedhashset can contain null.HashSet
HashSet is a collection that is supported by a hash table and is not thread-safe.
Let's look at the following example, which is essent
set. Set has exactly the same interface as collection, so there is no additional functionality. In fact set is collection, but behavior is different.
The main features of the set interface are HashSet, TreeSet, and Linkedhashset. Each of the same items is saved only one copy. They also have different points, the difference is as follows:
1.HashSet:
HashSet uses a rather complex way of storing elements, and using HashSet to get the elements in the
performance will be slightly lower than HashSet's performance.
TreeSet:TreeSet can ensure that the collection element is in the sorted state.TreeSet are not sorted according to the order in which the elements are inserted, but are sorted based on the size of the element's actual values.TreeSet uses a red-black tree's data structure to store collection elements.TreeSet supports two sorts of sorting methods: natural sorting and custom sorting. By default, Tre
which the elements are inserted into the collectionTreeSet guarantees that the elements of the set are orderly, there are 2 ways to achieve comparability between objects: 1, objects added to TreeSet implement the comparable interface, 2, specify a comparer for the elements of the ruleset (Comparator)TreeSet the bottom layer is stored with treemap, in fact, the elements in the
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.