/*
|--set: Elements are unordered (the order in and out is not necessarily consistent) and elements cannot be duplicated. ,
|--hashset: The underlying data structure is a hash table. is not thread safe. Different steps.
HashSet is how to guarantee the uniqueness of the element.
is done by the two methods of the element, hashcode and equals.
if the element's Hashcode value is the same, the equals is judged to be true.
equals is not invoked if the hashcode value of the element is different.
Note that for actions such as determining whether an element exists, as well as deleting it, the dependent method is the hashcode and equals method of the element.
|--treeset:
The set set is functionally consistent with the collection.
* *
class Hashsetdemo
{public
static void sop (Object obj)
{
System.out.println (obj);
} Public
static void Main (string[] args)
{
HashSet hs = new HashSet ();
SOP (Hs.add ("Java01"));
SOP (Hs.add ("Java01"));
Hs.add ("Java02");
Hs.add ("java03");
Hs.add ("java03");
Hs.add ("Java04");
Iterator it = Hs.iterator ();
while (It.hasnext ())
{
sop (It.next ());
}
}
}
HashSet Store Custom Elements
/* Go to the HashSet collection to make a custom object name and the same age as the same person, repeating elements.
* * Class Hashsettest {public static void sop (Object obj) {System.out.println (obj);
public static void Main (string[] args) {hashset hs = new HashSet ();
Hs.add (New Person ("A1", 11));
Hs.add (New Person ("A2", 12));
Hs.add (New Person ("A3", 13));
Hs.add (New Person ("A2", 12));
Hs.add (New person ("A4", 14));
SOP ("A1:" +hs.contains (New Person ("A2", 12));
Hs.remove (New person ("A4", 13));
Iterator it = Hs.iterator ();
while (It.hasnext ()) {person P = (person) it.next ();
SOP (P.getname () + "::" + p.getage ());
}} class Hashsetperson {private String name;
private int age;
Hashsetperson (String name, int age) {this.name = name;
This.age = age;
public int hashcode () {System.out.println (THIS.name + "... hashcode");
return Name.hashcode () + age * 37; public boolean equals (Object obj) {if (!
obj instanceof person), return false;
Hashsetperson p = (hashsetperson) obj; SysteM.out.println (THIS.name + "... equals ..." + p.name);
Return This.name.equals (p.name) && this.age = = P.age;
Public String GetName () {return name;
public int getage () {return age; }
}