【容器】Collection接口---hashCode
java基础中的容器:
Collection接口
hashCode
实例(马士兵视频源码):
import java.util.*; public class basicCollectionTest2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Collection c=new HashSet(); c.add("11"); c.add(new Name("f1","wang")); c.add("zhu"); System.out.println(c); //remove 删除 c.remove("11"); c.remove(new Name("f1","wang")); c.remove("zhu"); System.out.println(c); } } class Name { String firstname,lastname; //构造函数 public Name(String firstname,String lastname){ this.firstname=firstname; this.lastname=lastname; } //返回 public String getFirstname(){ return firstname; } public String getLasttname(){ return lastname; } public String toString(){ return lastname+" "+firstname; } /* * 重写equals方法后要同时重写hashCode方法 * 当对象变为索引时,需要hashCode方法 * (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) */ public boolean equals(Object obj){ if(obj instanceof Name){ Name name=(Name)obj; return (firstname.equals(name.firstname)&& lastname.equals(name.lastname)); } return super.equals(obj); } public int hashCode(){ return firstname.hashCode(); } }
相关推荐
雷潇 2020-08-16
happylife 2020-05-31
Bloddy 2020-04-23
范范 2020-03-27
faiculty 2020-02-14
mbcsdn 2020-01-07
hanyujianke 2020-01-01
Happyunlimited 2019-12-11
fenghuoliuxing0 2016-08-25
zhuyonge 2016-01-02
85251742 2014-08-30
极乐净土 2013-07-14
newtrekWang 2019-07-01
WindChaser 2019-06-30
落地窗前梦残夜 2019-06-28
caipeng 2019-06-27
WindChaser 2019-06-28
潇汀 2011-09-14