HashSet与TreeSet去重的区别
2016-08-21 16:31
162 查看
一、HashSet
在HashSet去重是根据Hash码是否相同,即对应两个对象的内存地址是否相同,如果一个对象要去重,要重写HashSet中的equals和hashCode方法
例子:
结论:没重写equals 和 hashCode方法时,两本书名和价格一样的书都存入了hashset中
重写equals方法和hashCode方法后:
结论:重写了equals 和 hashCode方法后,只有一本书存在了hashSet方法中
二、TreeSet
TreeSet去重是根据二叉树原理,根据比较器设置的比较属性去重
例子:
在HashSet去重是根据Hash码是否相同,即对应两个对象的内存地址是否相同,如果一个对象要去重,要重写HashSet中的equals和hashCode方法
例子:
public class Book { private String name; private double price; public Book(String name, double price) { this.name = name; this.price = price; } @Override public String toString() { return "Book [name=" + name + ", price=" + price + "]"; } public static void main(String[] args) { HashSet<Book> hash = new HashSet<Book>(); hash.add(new Book("java开发", 80.9)); hash.add(new Book("java开发", 80.9)); // 输出hashset,两本都存在了hashset中 for (Book s : hash) { System.out.println(s); } } }
结论:没重写equals 和 hashCode方法时,两本书名和价格一样的书都存入了hashset中
重写equals方法和hashCode方法后:
public class Book { private String name; private double price; public Book(String name, double price) { this.name = name; this.price = price; } //------------------------------------------------- //重写的hashCode和equals方法,在eclipse--Source中可以自动生成这两个方法 @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); long temp; temp = Double.doubleToLongBits(price); result = prime * result + (int) (temp ^ (temp >>> 32)); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Book other = (Book) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price)) return false; return true; } //-------------------------------------------------- @Override public String toString() { return "Book [name=" + name + ", price=" + price + "]"; } public static void main(String[] args) { HashSet<Book> hash = new HashSet<Book>(); hash.add(new Book("java开发", 80.9)); hash.add(new Book("java开发", 80.9)); // 输出hashset,只有一本书存在了hashSet中 for (Book s : hash) { System.out.println(s); } } }
结论:重写了equals 和 hashCode方法后,只有一本书存在了hashSet方法中
二、TreeSet
TreeSet去重是根据二叉树原理,根据比较器设置的比较属性去重
例子:
public class Book implements Comparable<Book> { private String name; private double price; public Book(String name, double price) { this.name = name; this.price = price; } @Override public String toString() { return "Book [name=" + name + ", price=" + price + "]"; } // 重写比较器中的compare方法,按照书名来排序 @Override public int compareTo(Book o) { return this.name.compareTo(o.name); } public static void main(String[] args) { TreeSet<Book> hash = new TreeSet<Book>(); hash.add(new Book("ja 4000 va开发", 80.9)); hash.add(new Book("java开发", 80.9));// 同名同价格 hash.add(new Book("java开发", 20.1));// 同名不同价格 hash.add(new Book("Android开发", 80.9));// 不同名同价格 // 输出Treeset for (Book s : hash) { System.out.println(s); } /* * 输出结果:同名的书都去重了 * Book [name=Android开发, price=80.9] * Book [name=java开发,price=80.9] */ } }
相关文章推荐
- HashSet 与TreeSet和LinkedHashSet的区别
- 【java提高】---HashSet 与TreeSet和LinkedHashSet的区别
- 浅谈HashSet和TreeSet区别及用法(附源码)
- HashSet和TreeSet有什么区别?
- HashSet、LinkedHashSet和TreeSet三者区别与联系
- Collection TreeSet TreeMap排序 ArrayList HashSet去重之间的区别
- HashSet和TreeSet使用方法的区别解析
- HashSet、LinkedHashSet和TreeSet三者区别与联系
- HashSet 与TreeSet和LinkedHashSet的区别
- Java集合框架:Set(HashSet,LinkedHashSet,TreeSet)
- 黑马程序员_集合之Set(HashSet与TreeSet)
- HashSet的hashCode方法和equals方法的重写,TreeSet中compareTo方法的重写,Comparator在treeSet中的应用。
- HashSet和TreeSet 分别实现对象的排序操作
- java怎么判断两个Set 里的对象的值是否相同【两个set中的值是否相等】、java treeset和hashset如何判断元素是否相同【即对象是否完全相同;利用一个set去除重复元素】
- 【Java相关】Java中TreeSet和HashSet的排序和去重
- List、Map、Set的理解(LinkedList和ArrayList、Vector和ArrayList、HashMap和HashTable和HashSet区别与使用)
- hashmap和treemap、hashset和treeset、linkedhashmap
- HashSet和TreeSet中最好只放入不可变对象
- JAVA菜鸟入门(17) 有排序的TreeSet和无排序的HashSet, LinkedHashSet
- Java基础第十三天--Set, HashSet, TreeSet