黑马程序员_08HashSet实例

-------  android培训 ios培训 、期待与您交流! ----------
一、HashSet实例1
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,小于返回-1
  return 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方法,以保证放入的对象的唯一性。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值