The Set collection
This is the 19th day of my participation in the August Wenwen Challenge.More challenges in August
The main features are: internal storage of duplicate elements is not allowed
The inheritance structure is as follows:
public interface Set<E> extends Collection<E>
Copy the code
Core code:
// There can be no duplicate values, if there is an error
/ / the Exception in the thread "is the main" Java. Lang. IllegalArgumentException: duplicate element: the world
/ / Set the < String > all. = the Set of (" hello ", "xbhog", "world", "world");
Set<String> all = Set.of("Hello"."xbhog"."The world");
System.out.println(all);
Copy the code
HashSet subclass:
Features: Hash stores and does not allow to store duplicate elements, i.e., unordered stores
The inheritance structure is as follows:
public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, Serializable
Copy the code
HashSet saves data:
// Data is stored in an unordered manner, and duplicate data is not allowed to be saved
// The saved type is String
Set<String> all = new HashSet<String>();
all.add("Xiao li");
all.add("Little red");
all.add("Little one");
all.add("Small 2");
all.add("Flower");
all.add("Flower");
System.out.println(all);
Iterator<String> iter = all.iterator();
while(iter.hasNext()){
String str = iter.next();
System.out.print(str+"、");
}
Copy the code
LinkedHashSet subclass: JDK1.4 add – Resolve the problem of storing data in order in HashSet
The implementation is based on the data stored in a linked list: the order in which the collection is stored is the order in which it is added, and no duplicate data is stored.
// The operation is based on the linked list, and the saved data is saved sequentially
Set<String> all = new LinkedHashSet<String>();
all.add("Miss Li");
all.add("Little bai");
all.add("Xiao Ming");
all.add("Yellow");
System.out.println(all);
Iterator<String> iter = all.iterator();
while (iter.hasNext()) {
String str = iter.next();
System.out.println(str + "、");
}
Copy the code
TreeSet subclass:
Features: Allows the data stored in a collection to be arranged in an orderly manner
Its inheritance structure is as follows:
public class TreeSet<E> extends AbstractSet<E> implements NavigableSet<E>, Cloneable.Serializable
Copy the code
The TreeSet subclass inherits the AbstractSet class and implements the NavigableSet interface.
TreeSet saves data:
packageJava from entry to project practice. Java class set framework. Set the Set;import java.util.*;
public class TreeSetAn ordered arrangement of subclasses{
// All saved data in alphabetical order (characters in alphabetical order)
public static void main(String[] args) {
Set<String> all = new TreeSet<String>();
all.add("11");
all.add("hello");
all.add("hello");
all.add("world");
all.add("add");
all.add("Deployment");
all.add("Aha!"); System.out.println(all); }}Copy the code
Priority: Numeric sort > alphabetical sort > Chinese sort
TreeSet subclass sorting analysis:
The subclass uses the Comparable interface for ordering ordered data storage. Note that when overwriting the compareTo() method, you need to compare all the attributes in the class; Otherwise, some attributes are misjudged as the same object; Failure to judge repeated elements;
packageJava from entry to project practice. Java class set framework. Set the Set;import java.util.Set;
import java.util.TreeSet;
class Member implements Comparable<Member>{
private String name;
private int age;
public Member(String name,int age){
this.name = name;
this.age = age;
}
public String toString(a){
return "Name:"+this.name +", Age:+this.age;
}
@Override
public int compareTo(Member per){
if(this.age < per.age) return -1;
else if(this.age > per.age) return 1; // Compare names at the same age
else {
return this.name.compareTo(per.name); }}}public class TreeSetSubclass sorting analysis{
public static void main(String[] args) {
Set<Member> all = new TreeSet<Member>();
all.add(new Member("Zhang".12));
all.add(new Member("Bill".12));
all.add(new Member("Fifty".20));
all.add(new Member("Fifty".20)); all.forEach(System.out::println); }}Copy the code
For a description of compareTo, you can translate it into a comment in the source code.
Duplicate element elimination :(duplicate elements in an unsorted collection)
Rely on two methods:
- Hashcode: public int Hashcode();
- Public Boolean equals(Object obj);
During the object comparison process, the hashCode() method is first used to match and compare the code already saved in the collection; If the code is the same, then use equals() to compare the attributes in sequence; If all are the same; Is the same element;
packageJava from entry to project practice. Java class set framework. Set the Set;import java.util.*;
class Person{
private String name;
private int age;
public Person(String name,int age){
this.name = name;
this.age = age;
}
public String getName(a) {
return name;
}
public int getAge(a) {
return age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if(! (oinstanceof Person)) return false;
Person person = (Person) o;
if(age ! = person.age)return false;
if(name ! =null? ! name.equals(person.name) : person.name ! =null) return false;
return true;
}
@Override
public int hashCode(a) {
intresult = name ! =null ? name.hashCode() : 0;
result = 31 * result + age;
returnresult; }}public classDuplicate element elimination{
public static void main(String[] args) {
Set<Person> all = new HashSet<Person>();
all.add(new Person("Zhang".19));
all.add(new Person("Bill".19));
all.add(new Person("Fifty".20));
all.add(new Person("Fifty".20));
all.add(new Person("Wei six".66));
all.add(new Person("xbhog".10));
System.out.println("------------ first input method -----------");
all.forEach((person -> {
System.out.println(person.getName()+"--"+person.getAge());
}));
System.out.println("------------ second input method -----------");
Iterator<Person> iter = all.iterator();
while(iter.hasNext()){
Person per = iter.next();
System.out.println(per.getName() + ""+per.getAge()); }}}Copy the code
The duplicate elements are saved in a hashSet, and the equals and hashCode methods are used to remove the duplicate elements.
The end:
If you see here or just to help you, I hope you can click 👍 or ★, thank you;
There are mistakes, welcome to point out in the comments, the author will see the modification.