import java.util.*;public class TestHashSet{
public static void main(String args[]){
Set s=new HashSet();s.add("hello");s.add("world");s.add(new Name("firstName1","lastName1"));s.add(new Name("firstName2","lastName2"));s.add("hello");s.add(new Name("firstName","lastName"));Iterator it=s.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
Set sn=new HashSet(s);Set s1=new HashSet();s1.add("hello");s1.add("world");s1.add(new Name("firstName","lastName"));s1.add("i am new");s1.retainAll(s); //取两个容器的交集Set su=new HashSet(s1);su.add(s); //将一个容器添加到另一个容器中System.out.println(s1);System.out.println(sn);System.out.println(su);
Iterator its=su.iterator();while(its.hasNext()){
//这里循环只能循环su内部的元素,至于被添加进来的s容器的内容只会当做整体循环出来
System.out.println(its.next());}}
}class Name{
String firstName;String lastName;Name(String firstName,String lastName){
this.firstName=firstName;this.lastName=lastName;
}
}//HashSet中相同元素只能存储一次
二、HashSet实例2
import java.util.*;
public class TestHashSet{public static void main(String args[]){
Set s=new HashSet();s.add("hello");s.add("world");s.add(new Name("firstName1","lastName1"));s.add(new Name("firstName2","lastName2"));s.add("hello");s.add(new Name("firstName","lastName"));for(Object ss1:s){
System.out.println(ss1);
}Iterator it=s.iterator();while(it.hasNext()){
System.out.println(it.next());
}Set sn=new HashSet(s);Set s1=new HashSet();s1.add("hello");s1.add("world");s1.add(new Name("firstName","lastName"));s1.add("i am new");System.out.println(s1);s1.retainAll(s);Set su=new HashSet(s1);su.add(s);System.out.println(s1);System.out.println(sn);System.out.println(su);Iterator its=su.iterator();while(its.hasNext()){
System.out.println(its.next());
}System.out.println("-------------------------------------------------");List ll=new LinkedList();ll.add(new Name("karl","smish"));ll.add(new Name("karl","jack"));ll.add(new Name("adiwa","smish"));ll.add(new Name("susan","clerk"));System.out.println(ll);Collections.sort(ll);System.out.println(ll);}
}class Name implements Comparable{
String firstName;String lastName;Name(String firstName,String lastName){
this.firstName=firstName;this.lastName=lastName;
}public String getFirstName(){
return this.firstName;
}public String toString(){
return firstName+lastName;
}public String getLastName(){
return this.lastName;
}public int hashCode(){
return this.firstName.hashCode();
}public boolean equals(Object obj){
if(obj instanceof Name){
Name name=(Name)obj;//这里调用了String类的equals方法,因为String重写了equals方法return firstName.equals(name.firstName)&&lastName.equals(name.lastName);
}return false;//return super.equals(obj)
}public int compareTo(Object obj){ //返回值为int类型//if(obj instanceof Name){// Name name=(Object)obj// if(firstName==name.firstName&&lastName==name.lastName) return true;
Name name=(Name)obj;int lastCmp=lastName.compareTo(name.lastName); //相等返回0,大于返回1,小于返回-1return lastCmp!=0?lastCmp:firstName.compareTo(name.firstName);
}
}
总结:
1、HashSet实现Set接口,由哈希表(实际上是一个HashMap实例)支持。它不保证set 的迭代顺序;特别是它不保证该顺序恒久不变。此类允许使用null元素。2、对于HashSet而言,它是基于HashMap实现的,HashSet底层使用HashMap来保存所有元素,因此HashSet 的实现比较简单,相关HashSet的操作,基本上都是直接调用底层HashMap的相关方法来完成。3、对于HashSet中保存的对象,请注意正确重写其equals和hashCode方法,以保证放入的对象的唯一性。