Java Set

Java Set中不能保存重复元素,分为HashSet, TreeSet两种,其中HashSet为无序,TreeSet有序。

TreeSet应用范例

package com.set.test;

import java.util.Set;
import java.util.TreeSet;

class Book implements Comparable<Book>{
    String title;
    double price;

    public Book(String title, double price) {
        super();
        this.title = title;
        this.price = price;
    }
    @Override
    public String toString() {
        return "title: "+this.title+", price: "+this.price+"\n";
    }
    @Override
    public int compareTo(Book book) {
        if(book.price > this.price){
            return 1;
        }
        else if (book.price < this.price){
            return -1;
        }
        else {
            return this.title.compareTo(book.title);
        }
    }
    
}


public class TestDemo {
    public static void main(String[] args) {
        Set<Book> s = new TreeSet<Book>();
        s.add(new Book("Java",10));
        s.add(new Book("Java",10));
        s.add(new Book("JSP",20));
        s.add(new Book("SQL",10));
        s.add(new Book("C++",10));
        System.out.println(s);
    }

}

HashSet使用范例

package com.set.test;

import java.util.Set;
import java.util.HashSet;

class Book implements Comparable<Book>{
    String title;
    double price;

    public Book(String title, double price) {
        super();
        this.title = title;
        this.price = price;
    }
    @Override
    public String toString() {
        return "title: "+this.title+", price: "+this.price+"\n";
    }
    
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        long temp;
        temp = Double.doubleToLongBits(price);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        result = prime * result + ((title == null) ? 0 : title.hashCode());
        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 (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
            return false;
        if (title == null) {
            if (other.title != null)
                return false;
        } else if (!title.equals(other.title))
            return false;
        return true;
    }
    @Override
    public int compareTo(Book book) {
        if(book.price > this.price){
            return 1;
        }
        else if (book.price < this.price){
            return -1;
        }
        else {
            return this.title.compareTo(book.title);
        }
    }
    
}


public class TestDemo {
    public static void main(String[] args) {
        Set<Book> s = new HashSet<Book>();
        s.add(new Book("Java",10));
        s.add(new Book("Java",10));
        s.add(new Book("JSP",20));
        s.add(new Book("SQL",10));
        s.add(new Book("C++",10));
        System.out.println(s);
    }

}

从代码中也可以看到,HashSet需要多覆写equals和hashcode方法。

posted @ 2015-12-21 14:44  finalboss1987  阅读(507)  评论(0编辑  收藏  举报