吴裕雄--天生自然java开发常用类库学习笔记:IdentityHashMap类
2024-09-04 22:46:30
import java.util.IdentityHashMap ;
import java.util.HashMap ;
import java.util.Set ;
import java.util.Iterator ;
import java.util.Map ;
class Person{
private String name ;
private int age ;
public Person(String name,int age){
this.name = name ;
this.age = age ;
}
public boolean equals(Object obj){
if(this==obj){
return true ;
}
if(!(obj instanceof Person)){
return false ;
}
Person p = (Person)obj ;
if(this.name.equals(p.name)&&this.age==p.age){
return true ;
}else{
return false ;
}
}
public int hashCode(){
return this.name.hashCode() * this.age ;
}
public String toString(){
return "姓名:" + this.name + ",年龄:" + this.age ;
}
};
public class IdentityHashMapDemo01{
public static void main(String args[]){
Map<Person,String> map = null ; // 声明Map对象
map = new HashMap<Person,String>() ;
map.put(new Person("张三",30),"zhangsan_1") ; // 加入内容
map.put(new Person("张三",30),"zhangsan_2") ; // 加入内容
map.put(new Person("李四",31),"lisi") ; // 加入内容
Set<Map.Entry<Person,String>> allSet = null ; // 准备使用Set接收全部内容
allSet = map.entrySet() ;
Iterator<Map.Entry<Person,String>> iter = null ;
iter = allSet.iterator() ;
while(iter.hasNext()){
Map.Entry<Person,String> me = iter.next() ;
System.out.println(me.getKey() + " --> " + me.getValue()) ;
}
}
};
import java.util.IdentityHashMap ;
import java.util.Set ;
import java.util.Iterator ;
import java.util.Map ;
class Person{
private String name ;
private int age ;
public Person(String name,int age){
this.name = name ;
this.age = age ;
}
public boolean equals(Object obj){
if(this==obj){
return true ;
}
if(!(obj instanceof Person)){
return false ;
}
Person p = (Person)obj ;
if(this.name.equals(p.name)&&this.age==p.age){
return true ;
}else{
return false ;
}
}
public int hashCode(){
return this.name.hashCode() * this.age ;
}
public String toString(){
return "姓名:" + this.name + ",年龄:" + this.age ;
}
};
public class IdentityHashMapDemo02{
public static void main(String args[]){
Map<Person,String> map = null ; // 声明Map对象
map = new IdentityHashMap<Person,String>() ;
map.put(new Person("张三",30),"zhangsan_1") ; // 加入内容
map.put(new Person("张三",30),"zhangsan_2") ; // 加入内容
map.put(new Person("李四",31),"lisi") ; // 加入内容
Set<Map.Entry<Person,String>> allSet = null ; // 准备使用Set接收全部内容
allSet = map.entrySet() ;
Iterator<Map.Entry<Person,String>> iter = null ;
iter = allSet.iterator() ;
while(iter.hasNext()){
Map.Entry<Person,String> me = iter.next() ;
System.out.println(me.getKey() + " --> " + me.getValue()) ;
}
}
};
最新文章
- 使用python处理子域名爆破工具subdomainsbrute结果txt
- 畅通工程——D
- 将页面打印成excel
- python算法——第四天
- JAVA中的NIO(二)
- CSS3--动态实现ToolTip效果(实例)
- SQL 面试题(一)
- php中文编码
- DeflateStream类
- Lucene全文检索学习笔记
- EC+VO+SCOPE for ES3
- linux(ubuntu)环境下安装IDEA
- 进程间通信——IPC之共享内存
- Codeforces 1110D. Jongmah 动态规划
- 转://Oracle数据库补丁分析实践
- (转)c++ 回调函数
- Houdini技术体系 过程化地形系统(一):Far Cry5的植被系统分析
- TextView字体,行距,html格式,超链接,对大长度的设定
- [LeetCode] 72. Edit Distance_hard tag: Dynamic Programming
- [LintCode] 394. Coins in a Line_ Medium tag:Dynamic Programming_博弈