1. Definition
duplicate elements are not allowed in set and are compared using the Equals () method. If true, the hashcode value of two objects should also be equal
2. Methods
Add (): adding elements
Remove (): Removes the specified element
.......
Common methods in TreeSet:
First (): Returns element One, last return
Lower (): Returns the previous element of the specified element, higher (): Specifies the next element of the element
Subset (A, b) of the SortedSet interface: Returns the element of the specified region, in the range [A, b]
Headset (first,b) of the SortedSet interface: Returns all elements before the specified element, in the range [First,b], Tailset (b,end): Returns all elements after the specified element, in the range [B,end]
3. Common Implementation Classes
HashSet: A null value can be placed , when an element is passed into the HashSet collection, HashSet calls the Hashcode method of the object to get the hash value, and then determines the storage location (unordered)
Linkedhashset:hashset subclasses, which do not allow duplicate values, use Hashcode to determine the position in the collection, use the linked list to determine the position (ordered, output in order of input)
Treeset:1. By default, the Set object is created directly using the TreeSet parameterless constructor, and when the element is placed, the comparable interface (for sorting) must be implemented , sorted by CompareTo method;
2. If you pass in a class that implements the comparator interface when you create a TreeSet object, TreeSet uses the Compare method of the comparator interface to sort. The elements in the collection do not need to be implemented
comparable interface; < Span style= "color: #000000;" If you put the element that implements the comparable interface, comparator as standard
Note: The ToString method provided by the <1>object class always returns the class name of the object implementation class [email protected]+hashcode(16 decimal) value, You can output your desired form by overriding the ToString method.
<2> If you put an object into HashSet, If you override the object Equals () method, you should also override its Hashcode () method.
4. Example
Import static org.junit.assert.*;
Import Java.util.Comparator;
Import Java.util.HashSet;
Import Java.util.LinkedHashSet;
Import Java.util.Set;
Import Java.util.TreeSet;
Import Org.junit.Test;
public class Settest {
@Test
The HashSet implementation class can put null values, and when an element is passed into the HashSet collection, HashSet calls the object's Hashcode method to get the hash value and then decides where to store it (unordered)
public void TestHashSet() {
Set c1=new HashSet ();
C1.add (New Customer (1, "AAA"));
C1.add (New Customer (1, "AAA"));
C1.add (New Customer (2, "AAA"));
C1.add (NULL);
System.out.println (C1.size ());
for (Object c:c1) {
System.out.println (c);
}
}
@Test
//linkedhashset is a subclass of HashSet, does not allow duplicate values, uses Hashcode to determine the position in the collection, uses the linked list to determine the position (ordered, output in order of input)
public void testlinkedhashset() {
Set c2=new linkedhashset ();
C2.add (New Customer (1, "AAA"));
C2.add (New Customer (3, "CCC"));
C2.add (New Customer (2, "BBB"));
for (Object c:c2) {
System.out.println (c);
}
}
@Test
/* TreeSet internal ordering:
by default, a set object is created directly using the TreeSet parameterless constructor, where the element must be implemented with the comparable interface (for sorting), sorted by CompareTo method */
public void TestTreeSet1 () {
TreeSet c3=new TreeSet ();
C3.add (New Customer (1, "AAA"));
C3.add (New Customer (3, "CCC"));
C3.add (New Customer (2, "BBB"));
Customer C31=new Customer (4, "DDD");
C3.add (C31);
for (Object c:c3) {
System.out.println (c);
}
Object obj;
//first Returns the first element, last returns the final element
Obj=c3.first ();
System.out.println (obj);
//lower Returns the previous element of the specified element, higher the next element of the specified element
Obj=c3.lower (C31);
System.out.println (obj);
the subset method of the//sortedset interface returns the element of the specified range, which is [A, b]
Set C32=c3.subset (C3.first (), C31);
System.out.println (C32);
the headset method of the//sortedset interface returns all elements before the specified element, with a range of [first,b], and Tailset returns all elements after the specified element, in the range [B,end]
Obj=c3.headset (C31);
System.out.println (obj);
}
@Test
/*treeset Internal ordering:
when a TreeSet object is created, a class that implements the comparator interface is passed in, and TreeSet is ordered using the Compare method of the comparator interface. At this point the elements in the collection do not need to implement the comparable interface;
If you put an element that implements the comparable interface at the same time, comparator is the standard. */
public void TestTreeSet2 () {
Comparator comparator=new customercomparator ();
TreeSet c4=new TreeSet (comparator);
C4.add (New Customer (1, "AAA"));
C4.add (New Customer (3, "CCC"));
C4.add (New Customer (2, "BBB"));
Customer C41=new Customer (4, "DDD");
C4.add (C41);
for (Object c:c4) {
System.out.println (c);
}
}
}
Full code involved: http://www.cnblogs.com/jfl-xx/p/4707643.html
Java Collection--set Interface