HashSet
来源:互联网 发布:大数据在哪里买 编辑:程序博客网 时间:2024/05/27 01:45
/*|--Set :元素是无序(存入和取出的顺序不一定一致),元素不可用重复|--HashSet :底层数据结构是哈希表HashSet是如何保证元素唯一性?是通过元素的两个方法, hashCode 和 equals 来完成如果元素的HashCode 值相同,才会判断equals是否为true如果元素的hashCode值不同 ,才会调用equals注意,对于判断元素是否存在,以及删除等操作,依赖的方法是元素的hashCode 和 equals|--TreeSetSet 集合的功能和Coll*/import java.util.*;class Person{private String name;private int age;Person(String name,int age){this.name = name;this.age = age;}public boolean equals(Object obj){if(!( obj instanceof Person))return false;Person p = (Person)obj;return (this.name.equals(p.name) && this.age == p.age);}public int hashCode(){return this.name.hashCode();//此处调用String 类型的hashCode //不用这个也行 只不过用来做个标志 区别不同对象 }public String getName(){return name;}public int getAge(){return age;}}class HashSetDemo{public static void main(String []args){HashSet hs = new HashSet();hs.add(new Person("Person1",11));//存入四个人hs.add(new Person("Person2",11));hs.add(new Person("Person3",11));hs.add(new Person("Person1",11));Iterator it = hs.iterator();//迭代器while( it.hasNext())//循环输出{Person p = (Person)it.next();sop( p.getName() + " --- " + p.getAge());}sop(hs.contains(new Person("Person1",11)));sop(hs.remove(new Person("Person1",11)));sop(hs.contains(new Person("Person1",11)));}public static void sop(Object obj)//定义一个输出函数{System.out.println(obj);}}
0 0
- HashSet
- Hashset
- HashSet
- HashSet
- HashSet
- HashSet
- HashSet
- HashSet
- HashSet
- HashSet
- HashSet
- HashSet
- HashSet
- HashSet
- HashSet
- HashSet
- hashset
- HashSet
- Poj 1579 题解
- 我的2014碎碎念—学习篇、实习篇、工作篇、生活篇
- 推荐!国外程序员整理的Java资源大全
- javascript:对Array增加方法造成的坑。
- LIBSVM在MATLAB下的使用
- HashSet
- 手机卫士学习03- 弹出对话框的一些细节
- Linux 命令学习笔记
- 爱你就像爱生命
- HttpClient连接池抛出大量ConnectionPoolTimeoutException: Timeout waiting for connection
- 3094-小鑫找不到北
- i2c的时钟延展问题
- 手动配置 Hibenate 异常汇总
- TreeSet