Java学习之集合(HashSet)

HashSet的实例

1 1 import java.util.HashSet; 2 2 import java.util.Iterator; 3 3 4 4 public class HashSetDemo { 5 5 6 6 public static void main(String[] args) { 7 7 HashSet hs = new HashSet(); 8 8 hs.add("heihei"); 9 9 hs.add("hehe"); 1010 hs.add("hahha"); 1111 hs.add("xixi"); 1212 hs.add("hehe"); 1313 hs.add("hahha"); 1414 hs.add("xixi"); 1515 1616 Iterator it = hs.iterator(); 1717 1818 while (it.hasNext()) { 1919 System.out.println(it.next()); 2020 } 2121 } 2222 }

结果:

从结果发现HashSet不会由值重复,那就要判断其元素是否相同

判断的原理:

  判断两个元素的哈希值是否相同

    如果哈希值相同就判断两个元素的内容是否相同

    如果哈希值不同就不判断两个元素的内容

代码描述:就是判断HashSet中的元素的hashCode和equals方法。

HashSet元素为自定义类型,验证代码:

①、元素类型

1 1 public class Person { 2 2 3 3 private String name; 4 4 private int age; 5 5 6 6 public Person() { 7 7 super(); 8 8 } 9 9 1010 public Person(String name, int age) { 1111 super(); 1212 this.name = name; 1313 this.age = age; 1414 } 1515 1616 public String getName() { 1717 return name; 1818 } 1919 2020 public void setName(String name) { 2121 this.name = name; 2222 } 2323 2424 public int getAge() { 2525 return age; 2626 } 2727 2828 public void setAge(int age) { 2929 this.age = age; 3030 } 3131 3232 @Override 3333 public int hashCode() { 3434 System.out.println(this + ":-------hashCode"); 3535 return this.name.hashCode() + this.age; 3636 } 3737 3838 @Override 3939 public boolean equals(Object obj) { 4040 System.out.println(this + ":-------equals-------" + obj); 4141 if (!(obj instanceof Person)) 4242 throw new ClassCastException("类型不对"); 4343 Person person = (Person) obj; 4444 return this.name.equals(person.name) && person.age == this.age; 4545 } 4646 4747 @Override 4848 public String toString() { 4949 5050 return this.name + ":" + this.age; 5151 } 5252 }

②、HashSet存储

1 1 import java.util.HashSet; 2 2 import java.util.Iterator; 3 3 4 4 import cn.marw.common.bean.Person; 5 5 6 6 public class HashSetTest { 7 7 8 8 public static void main(String[] args) { 9 9 HashSet hs = new HashSet(); 1010 1111 hs.add(new Person("wang1", 31)); 1212 hs.add(new Person("wang2", 32)); 1313 hs.add(new Person("wang3", 33)); 1414 hs.add(new Person("wang4", 34)); 1515 hs.add(new Person("wang2", 32)); 1616 Iterator it = hs.iterator(); 1717 while (it.hasNext()) { 1818 Person p = (Person) it.next(); 1919 System.out.println(p.getName() + ":" + p.getAge()); 2020 2121 } 2222 } 2323 }

結果:

总结:

HastSet判断集合元素是否相同,就是看元素的hashCode和equals方法

HastSet的方法contains和remove都要看元素的hashCode和equals方法

点赞
收藏

评论区

加载中...

相关推荐

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Java中多个集合的交集,并集和差集

一、交集  java中交集使用A.retainAll(B),交集的结果在集合A中。1importorg.junit.Test;23importjava.util.HashSet;4importjava.util.Set;56/7交集

Java入门第五篇:Java集合框架的Collection、List、Set、Map接口

【java的集合框架】 Collection:      1.List        ①ArrayList        ②LinkedList      2.set        ①HashSet        ②LinkedHashSet        ③TreeSetMap:       1.HashMap 

Java爬虫之JSoup使用教程

title:Java爬虫之JSoup使用教程date:201812248:00:000800update:201812248:00:000800author:mecover:https://imgblog.csdnimg.cn/20181224144920712(https://www.oschin

Java 集合线程安全

线程不安全的的集合有(HashSet,TreeSet,ArrayList,ArrayDeque,LinkedList,HashMap,TreeMap);线程安全的集合有(Vector,HashTable);Java(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Flib.csd