Java HashSet、LinkedHashSet、TreeSet判定元素重复的原则

HashSet和LinkedHashSet判定元素重复的原则

判定两个元素的hashCode返回值是否相同,若不同,返回false。

若两者hashCode相同,判定equals方法,若不同,返回false;否则返回truc。

注:hashCode和equals方法是所有类都有的,因为Object类有

import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.TreeSet;
 
public class ObjectHashSetTest {
 
    public static void main(String[] args) {
        System.out.println("==========Cat HashSet ==============");
        HashSet<Cat> hs = new HashSet<Cat>();
        hs.add(new Cat(2));
        hs.add(new Cat(1));
        hs.add(new Cat(3));
        hs.add(new Cat(5));
        hs.add(new Cat(4));
        hs.add(new Cat(4));
        System.out.println(hs.size());  //6
 
        System.out.println("========================");
        LinkedHashSet<Cat> lhs = new LinkedHashSet<Cat>();
        lhs.add(new Cat(2));
        lhs.add(new Cat(1));
        lhs.add(new Cat(3));
        lhs.add(new Cat(5));
        lhs.add(new Cat(4));
        lhs.add(new Cat(4));
        System.out.println(lhs.size());  //6
 
        System.out.println("==========Dog HashSet ==============");
        HashSet<Dog> hs2 = new HashSet<Dog>();
        hs2.add(new Dog(2));
        hs2.add(new Dog(1));
        hs2.add(new Dog(3));
        hs2.add(new Dog(5));
        hs2.add(new Dog(4));
        hs2.add(new Dog(4));
        System.out.println(hs2.size());  //5
 
        System.out.println("========================");
        LinkedHashSet<Dog> lhs2 = new LinkedHashSet<Dog>();
        lhs2.add(new Dog(2));
        lhs2.add(new Dog(1));
        lhs2.add(new Dog(3));
        lhs2.add(new Dog(5));
        lhs2.add(new Dog(4));
        lhs2.add(new Dog(4));
        System.out.println(lhs2.size());  //5
 
 
        System.out.println("==========Tiger HashSet ==============");
        HashSet<Tiger> hs3 = new HashSet<Tiger>();
        hs3.add(new Tiger(2));
        hs3.add(new Tiger(1));
        hs3.add(new Tiger(3));
        hs3.add(new Tiger(5));
        hs3.add(new Tiger(4));
        hs3.add(new Tiger(4));
        System.out.println(hs3.size());  //6
 
        System.out.println("========================");
        LinkedHashSet<Tiger> lhs3 = new LinkedHashSet<Tiger>();
        lhs3.add(new Tiger(2));
        lhs3.add(new Tiger(1));
        lhs3.add(new Tiger(3));
        lhs3.add(new Tiger(5));
        lhs3.add(new Tiger(4));
        lhs3.add(new Tiger(4));
        System.out.println(lhs3.size());  //6
    }
}
class Cat {
    private int size;
 
    public Cat(int size) {
        this.size = size;
    }
}
public class Tiger implements Comparable {
    private int size;
 
    public Tiger(int s) {
        size = s;
    }
 
    public int getSize() {
        return size;
    }
 
    @Override
    public int compareTo(Object o) {
        System.out.println("Tiger compareTo()~~~~~~~~~~~");
        return size - ((Tiger) o).getSize();
    }
    //HashSet的元素判定规则只和hashCode、 equals这2个方法有关,和compareTo方法无关。
}
class Dog {
    private int size;
 
    public Dog(int s) {
        size = s;
    }
 
    public int getSize() {
        return size;
    }
 
    @Override
    public boolean equals(Object obj2) {
        System.out.println("Dog equals()~~~~~~~~~~~");
        if (0 == size - ((Dog) obj2).getSize()) {
            return true;
        } else {
            return false;
        }
    }
 
    @Override
    public int hashCode() {
        System.out.println("Dog hashCode()~~~~~~~~~~~");
        return size;
    }
 
    @Override
    public String toString() {
        System.out.print("Dog toString()~~~~~~~~~~~");
        return size + "";
    }
    //注:这个三个方法是三位一体,equals()是相同的,hashCode()是相同的,则toString()也应该是相同的
}

TreeSet判定元素重复的原则

需要元素继承自Comparable接口

比较两个元素的compareTo方法是否相同,若相同返回true,否则返回false。

import java.util.TreeSet;
 
 
public class ObjectTreeSetTest {
 
    public static void main(String[] args) {
		/*
		System.out.println("==========Cat TreeSet ==============");
		TreeSet<Cat> ts = new TreeSet<Cat>();  
		ts.add(new Cat(2));  
		ts.add(new Cat(1));  
		ts.add(new Cat(3));  
		ts.add(new Cat(5));  
		ts.add(new Cat(4)); 
		ts.add(new Cat(4)); 
		System.out.println(ts.size());  //5		
		
		System.out.println("==========Dog TreeSet ==============");
		
		
		TreeSet<Dog> ts2 = new TreeSet<Dog>();  
		ts2.add(new Dog(2));  
		ts2.add(new Dog(1));  
		ts2.add(new Dog(3));  
		ts2.add(new Dog(5));  
		ts2.add(new Dog(4)); 
		ts2.add(new Dog(4)); 
		System.out.println(ts2.size());  //5
		*/
 
        //添加到TreeSet的,需要实现Comparable接口,即实现compareTo方法
        //HashSet的元素判定规则只和hashCode、 equals这2个方法有关,和compareTo方法无关。
 
        System.out.println("==========Tiger TreeSet ==============");
        
        TreeSet<Tiger> ts3 = new TreeSet<Tiger>();
        ts3.add(new Tiger(2));
        ts3.add(new Tiger(1));
        ts3.add(new Tiger(3));
        ts3.add(new Tiger(5));
        ts3.add(new Tiger(4));
        ts3.add(new Tiger(4));
        System.out.println(ts3.size());  //5
    }
}
public class Tiger implements Comparable {
    private int size;
 
    public Tiger(int s) {
        size = s;
    }
 
    public int getSize() {
        return size;
    }
 
    @Override
    public int compareTo(Object o) {
        System.out.println("Tiger compareTo()~~~~~~~~~~~");
        return size - ((Tiger) o).getSize();
    }
    //HashSet的元素判定规则只和hashCode、 equals这2个方法有关,和compareTo方法无关。
}
上一篇:js 文字LOGO效果


下一篇:007Java集合005详解HashSet、LinkedHashSet、TreeSet