- Set: Elements are out of order (the order of storage and removal is not necessarily the same), and elements cannot be repeated.
public class Text {
public static void main(String[] args) {
HashSet h = new HashSet();
h.add("java01");
//h.add("java01"); the output of adding this sentence remains unchanged because the elements are not repeatable
h.add("java02");
h.add("java03");
h.add("java04");
Iterator it = h.iterator();
while (it.hasNext()) {
Sop.sopl(it.next());
}
}
}
//Operation results:
java04
java03
java02
java01
//Unlike the order of storage, the hash table is stored at the address of the string.
//Elemental irrepeatability
public class Text {
public static void main(String[] args) {
HashSet h = new HashSet();
Sop.sopl(h.add("java01"));//Return boolean
Sop.sopl(h.add("java01"));
h.add("java02");
Iterator it = h.iterator();
while (it.hasNext()) {
Sop.sopl(it.next());
}
}
}
//Operation results:
true//The first java01 is stored in
false//Re-deposit in java01, not repeatable, so no deposit, return false
java02
java01
I. Two Common Subclasses of Set
- HashSet: The underlying data structure is a hash table
- Hash table: Stored according to the size of hash value, hash value is the same, compare whether the object is the same, different in the same hash value address to store more than one object with the same hash value.
- HashSet guarantees element uniqueness:
- Through two methods of elements: hashCode and equals
- If the hashCode value of the element is the same, equals will be called
- If the hashCode value of the element is different, equals is not called
- So the judgment and deletion of elements, the hashCode and equals methods of dependent elements
- HashSet stores custom objects
class Person {
private String name;
private String age;
Person(String name, String age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public String getAge() {
return age;
}
//Override hashCode and equals methods, collecting internal calls at the bottom, without calling them yourself
public int hashCode() {
return name.hashCode() + age.hashCode();//Customize the hash value and call the equals method of the element only if the hash value is the same
}
public boolean equals(Object obj) {
if(!(obj instanceof Person))//Object obj = new Person
return false;
Person p = (Person)obj;
return this.name.equals(p.name) && this.age.equals(p.age);
}
}
public class Demo {
public static void main(String[] args) {
HashSet h = new HashSet();
h.add(new Person("Li Si","18"));
h.add(new Person("Li Si","19"));
h.add(new Person("Li Si","20"));
h.add(new Person("Li Si","18"));
h = singleElement(h);
Iterator it = h.iterator();
while (it.hasNext()) {
Person p = (Person)it.next();
sopl(p.getName() + p.getAge());
}
}
public static HashSet singleElement(HashSet a) {
HashSet aNew = new HashSet();
Iterator it = a.iterator();
while (it.hasNext()) {
Object obj = it.next();
if (!aNew.contains(obj)) {
aNew.add(obj);
}
}
return aNew;
}
}
- TreeSet: You can sort elements in a Set collection (natural order)
public static void main(String[] args) {
TreeSet h = new TreeSet();
h.add("a1");
h.add("c2");
h.add("b3");
h.add("d3");
Iterator it = h.iterator();
while (it.hasNext()) {
sopl(it.next());
}
}
}
//Running results: natural sequencing abcd
a1
b3
c2
d3
- TreeSet stores custom objects
class Person implements Comparable {//This interface forces its objects to be comparable
private String name;
private int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int compareTo(Object obj) {
if(!(obj instanceof Person))
throw new RuntimeException("No Person object");
Person p = (Person)obj;
if (this.age > p.age)
return 1;
if (this.age == p.age)//When the main conditions are the same, judge the secondary conditions and sort them.
return this.name.compareTo(p.name);//String overrides the compareTo method
return -1;
}
}
public class Demo {
public static void main(String[] args) {
TreeSet h = new TreeSet();
//The stored objects must be comparable and implement Comparable interface (compareTo method)
h.add(new Person("Li Si01",22));
h.add(new Person("Li Si02",19));
h.add(new Person("Li Si03",20));
h.add(new Person("Li Si03",20));//Duplicate elements, not saved
h.add(new Person("Li Si04",18));
h.add(new Person("Li Si05",18));
Iterator it = h.iterator();
while (it.hasNext()) {
Person p = (Person)it.next();
sopl(p.getName() + "..." + p.getAge());
}
}
}
-
The underlying data structure is a binary tree (default from small to large)
Sequence and reverse extraction
class Person implements Comparable {//This interface forces it to be comparable
private String name;
private int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int compareTo(Object obj) {
return 1;
//return 1; take out in reverse order
//return 0; only one data fetch
}
}
public class Demo {
public static void main(String[] args) {
TreeSet h = new TreeSet();
//The stored objects must be comparable and implement Comparable interface (compareTo method)
h.add(new Person("Li Si01",22));
h.add(new Person("Li Si02",19));
h.add(new Person("Li Si03",20));
h.add(new Person("Li Si03",20));//Duplicate elements, not saved
h.add(new Person("Li Si04",18));
h.add(new Person("Li Si05",18));
Iterator it = h.iterator();
while (it.hasNext()) {
Person p = (Person)it.next();
sopl(p.getName() + "..." + p.getAge());
}
}
}
- principle
- return 1; turn the data to the right
- Return-1; turn data to the left
- TreeSet sort
- The first is to make elements comparable, implement Comparable interface, and override compareTo method. (Also known as the natural order of elements, Oh default order)
- The second is that when the element itself does not have comparability, or the comparability is not needed, then the set itself needs to have comparability. A collection has a way of comparison as soon as it is initialized (constructor)
//Define a comparator that passes the comparator object as a parameter to the constructor of the TreeSet collection
//When both kinds of sorting exist, comparator is the main method.
//Comparator Definition: Define a class to implement Comparator interface, override compare method
class Compare implements Comparator {
public int compare(Object o1, Object o2) {
Person p1 = (Person)o1;
Person p2 = (Person)o2;
int num = p1.getName().compareTo(p2.getName());
if (num == 0) {
return new Integer(p1.getAge()).compareTo(new Integer(p2.getAge()));
/*if (p1.getAge() > p2.getAge())
return 1;
if (p1.getAge() == p2.getAge())
return 0;
return -1;*/
}
return num;
}
}
class Person {
private String name;
private int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
public class Demo {
public static void main(String[] args) {
TreeSet h = new TreeSet(new Compare());
h.add(new Person("Li Si01",22));
h.add(new Person("Li Si01",23));
h.add(new Person("Li Si02",19));
h.add(new Person("Li Si03",20));
h.add(new Person("Li Si04",18));
Iterator it = h.iterator();
while (it.hasNext()) {
Person p = (Person)it.next();
sopl(p.getName() + "..." + p.getAge());
}
}
}
TreeSet Exercise
- Store strings, sorted by length
class Compare implements Comparator {
public int compare(Object o1, Object o2) {
String p1 = (String)o1;
String p2 = (String)o2;
int num = new Integer(p1.length()).compareTo(new Integer(p2.length()));
//If the length of the string is the same, compare the secondary conditions
if(num == 0)
return p1.compareTo(p2);//Natural sequencing of strings
return num;
}
}
public class Demo {
public static void main(String[] args) {
TreeSet h = new TreeSet(new Compare());
h.add("Li Si01");
h.add("Li Si07");
h.add("Li Si011");
h.add("Li Si05");
h.add("Li Si04");
Iterator it = h.iterator();
while (it.hasNext()) {
sopl(it.next());
}
}
}