JAVA自学教程之(常用对象API)— Set集合:HashSet集合演示



JAVA自学教程之(常用对象API)— Set集合:HashSet集合演示。

随着Java学习的深入,感觉大一时搞了一年的ACM,简直是明智之举,Java里很多数据结构、算法类的东西,理解起来就轻松多了

Set集合下有两大子类开发常用 HashSet集合 、TreeSet集合

Set集合的元素是不重复且无序

一、HashSet集合

API文档解释:此类实现 Set 接口,由哈希表(实际上是一个 HashMap 实例)支持。它不保证 set 的迭代顺序;特别是它不保证该顺序恒久不变。此类允许使用null 元素。

此类为基本操作提供了稳定性能,注意,此实现不是同步的。

由上可以总结出:HashSet集合的方法:内部数据结构是哈希表,且不同步,无论重复元素有多少,只存一个。

 

  1. import java.util.HashSet;
  2. import java.util.Iterator;
  3. public class Main
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.         HashSet hash = new HashSet();
  8.         hash.add(“123″);
  9.         hash.add(“456″);
  10.         hash.add(“asd”);
  11.         hash.add(“789″);
  12.         Iterator it = hash.iterator();
  13.         while(it.hasNext()){
  14.             System.out.println(it.next());
  15.         }
  16.     }
  17. }

哈希表

 

    数据经过哈希算法存储,会经过两次判断,第一此判断位置,第二次会判断内容,比如“ab”已经存储了,“ba”经过算法得到的位置是“ab”的位置,这时要经过第二次判断,确定内容是否相同,不相同会通过过算法得到新的位置,ACM里一般会经过线性探测再散列或者是链地址法

 

总结

哈希表判断元素是否相同:


1.判断的是两个元素的哈希值是否相同,如果相同,再判断两个对象的内容是否相同

2.判断哈希值相同,HashCode方法(返回此字符串的哈希码),判断内容相同,equals方法

注:哈希值不同,就不需要第二次判断内容是否相同

 

哈希表演示

 

  1. import java.util.HashSet;
  2. import java.util.Iterator;
  3. class Man extends Object//必须继承Object类才能复写HshCode,equals
  4. {
  5.     private String name;
  6.     private int age;
  7.     public Man() {
  8.         super();
  9.         // TODO Auto-generated constructor stub
  10.     }
  11.     public Man(String name, int age) {
  12.         super();
  13.         this.name = name;
  14.         //System.out.println(“1号”+this);
  15.         this.age = age;
  16.     }
  17.     @Override
  18.     public int hashCode() {
  19.         final int prim = 31;
  20.         //System.out.println(“namecode = ”+name.hashCode());
  21.         return name.hashCode()+age*prim;
  22.     }
  23.     @Override
  24.     public boolean equals(Object obj) {
  25.         if(this == obj)
  26.             return true;
  27.         //System.out.println(“2号”+this);
  28.         if(!(obj instanceof Man))
  29.             throw new ClassCastException(“类型错误”);
  30.         Man m = (Man)obj;
  31.         return this.name.equals(m.name) && this.age == m.age;
  32.     }
  33.     public String getName() {
  34.         return name;
  35.     }
  36.     public void setName(String name) {
  37.         this.name = name;
  38.     }
  39.     public int getAge() {
  40.         return age;
  41.     }
  42.     public void setAge(int age) {
  43.         this.age = age;
  44.     }
  45. }
  46. public class Main
  47. {
  48.     public static void main(String[] args)
  49.     {
  50.         HashSet hash = new HashSet();
  51.         hash.add(new Man(“a”,11));
  52.         hash.add(new Man(“b”,12));
  53.         hash.add(new Man(“c”,13));
  54.         hash.add(new Man(“a”,11));
  55.         Iterator it = hash.iterator();
  56.         while(it.hasNext()){
  57.             Man M = (Man)it.next();
  58.             System.out.println(M.getName()+”…..”+M.getAge());
  59.         }
  60.     }
  61. }

特别注意:上述代码的1号和2号两个this,代表的对象不同,不能混淆

 

练习

关于ArrayList的演示

 

  1. import java.util.ArrayList;
  2. import java.util.HashSet;
  3. import java.util.Iterator;
  4. class Man extends Object
  5. {
  6.     private String name;
  7.     private int age;
  8.     public Man() {
  9.         super();
  10.         // TODO Auto-generated constructor stub
  11.     }
  12.     public Man(String name, int age) {
  13.         super();
  14.         this.name = name;
  15.         this.age = age;
  16.     }
  17.     @Override
  18.     public boolean equals(Object obj) {
  19.         if(this == obj)
  20.             return true;
  21.         if(!(obj instanceof Man))
  22.             throw new ClassCastException(“类型错误”);
  23.         Man m = (Man)obj;
  24.         return this.name.equals(m.name) && this.age == m.age;
  25.     }
  26.     public String getName() {
  27.         return name;
  28.     }
  29.     public void setName(String name) {
  30.         this.name = name;
  31.     }
  32.     public int getAge() {
  33.         return age;
  34.     }
  35.     public void setAge(int age) {
  36.         this.age = age;
  37.     }
  38. }
  39. public class Main
  40. {
  41.     public static void main(String[] args)
  42.     {
  43.         ArrayList AL = new ArrayList();
  44.         AL.add(new Man(“a”,11));
  45.         AL.add(new Man(“b”,12));
  46.         AL.add(new Man(“c”,13));
  47.         AL.add(new Man(“a”,11));
  48.         printf(AL);
  49.         AL = ArrayListHashDemo(AL);
  50.         printf(AL);
  51.         System.out.println(AL.remove(new Man(“a”,11)));
  52.         printf(AL);
  53.     }
  54.     public static ArrayList ArrayListHashDemo(ArrayList al) {
  55.         ArrayList temp = new ArrayList();
  56.         Iterator it = al.iterator();
  57.         while(it.hasNext())
  58.         {
  59.             Object obj = it.next();
  60.             if(!temp.contains(obj))
  61.             temp.add(obj);
  62.         }
  63.         return temp;
  64.     }
  65.     public static void printf(ArrayList al)
  66.     {
  67.         Iterator it = al.iterator();
  68.         while(it.hasNext())
  69.         {
  70.             Man man = (Man)it.next();
  71.             System.out.print(man.getName()+”..”+man.getAge()+”,”);
  72.         }
  73.         System.out.printf(“\n”);
  74.     }
  75. }

API文档关于contains的解释:如果列表包含指定的元素,则返回 true。更确切地讲,当且仅当列表包含满足(o==null ? e==null : o.equals(e)) 的元素e 时才返回true

 

所以对于ArrayList集合,contains的判断与equals有关,而HashSet与HashCode和equals有关

 

  1. System.out.println(AL.remove(new Man(“a”,11)));//同理,remove也是有equals有关,只判断内容