HashSet
package com.yuzhenc.collection;
import java.util.HashSet;
public class Test12 {
public static void main(String[] args) {
HashSet<String> hashSet = new HashSet<>();
System.out.println(hashSet.add("abc"));
System.out.println(hashSet.add("def"));
System.out.println(hashSet.add("ghi"));
System.out.println(hashSet.add("def"));
System.out.println(hashSet.size());
System.out.println(hashSet);
HashSet<Animal> hashSet1 = new HashSet<>();
hashSet1.add(new Animal("cat",10));
hashSet1.add(new Animal("dog",5));
hashSet1.add(new Animal("pig",1));
hashSet1.add(new Animal("dog",5));
for (Animal animal : hashSet1) {
System.out.println(animal);
}
}
}
class Animal {
private String name;
private int age;
public Animal() {}
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Animal{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
LinkedHashSet
package com.yuzhenc.collection;
import java.util.LinkedHashSet;
public class Test13 {
public static void main(String[] args) {
LinkedHashSet<Integer> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add(1);
System.out.println(linkedHashSet.add(2));
linkedHashSet.add(3);
System.out.println(linkedHashSet.add(2));
System.out.println(linkedHashSet);
}
}
TreeSet
package com.yuzhenc.collection;
import java.util.TreeSet;
public class Test14 {
public static void main(String[] args) {
TreeSet<Integer> treeSet = new TreeSet<>();
treeSet.add(45);
treeSet.add(24);
treeSet.add(38);
treeSet.add(96);
treeSet.add(11);
treeSet.add(24);
System.out.println(treeSet);
}
}
内部比较器
package com.yuzhenc.collection;
import java.util.TreeSet;
public class Test15 {
public static void main(String[] args) {
TreeSet<Cat> treeSet = new TreeSet<>();
treeSet.add(new Cat("Lili",18));
treeSet.add(new Cat("Ana",19));
treeSet.add(new Cat("Amy",20));
treeSet.add(new Cat("Lili",18));
treeSet.add(new Cat("Lili",19));
treeSet.add(new Cat("Lili",17));
for (Cat cat : treeSet) {
System.out.println(cat);
}
}
}
class Cat implements Comparable <Cat>{
String name;
int age;
public Cat() {}
public Cat(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Cat{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public int compareTo(Cat o) {
return this.getAge()-o.getAge() == 0 ? this.getName().hashCode()-o.getName().hashCode() : this.getAge()-o.getAge();
}
}
外部比较器
package com.yuzhenc.collection;
import java.util.Comparator;
import java.util.TreeSet;
public class Test16 {
public static void main(String[] args) {
Bijiao bijiao = new Bijiao();
TreeSet<Dog> treeSet = new TreeSet<>(bijiao);
treeSet.add(new Dog("Lili",18));
treeSet.add(new Dog("Ana",19));
treeSet.add(new Dog("Amy",20));
treeSet.add(new Dog("Lili",18));
treeSet.add(new Dog("Lili",19));
treeSet.add(new Dog("Lili",17));
for (Dog dog : treeSet) {
System.out.println(dog);
}
}
}
class Dog {
String name;
int age;
public Dog() {}
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
class Bijiao implements Comparator<Dog> {
@Override
public int compare(Dog o1, Dog o2) {
return o1.getName().hashCode()-o2.getName().hashCode()==0 ? o1.getAge()-o2.getAge() : o1.getName().hashCode()-o2.getName().hashCode();
}
}