HashMap散列无序存储测试
程序员文章站
2024-03-23 14:42:16
...
package com.boonya.map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
public class EntryTest {
public class User{
@SuppressWarnings("unused")
private String name;
@SuppressWarnings("unused")
private int id;
public User(String name, int id) {
this.name = name;
this.id = id;
}
}
/**
* HashMap散列存储,无序性
* u2 : [email protected]
* u4 : [email protected]
* u1 : [email protected]
* u3 : [email protected]
* @param args
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Map<String,Object> map=new HashMap<String, Object>();
User u1=new User("boonya", 1);
User u2=new User("boonya2", 2);
User u3=new User("boonya3", 3);
User u4=new User("boonya4", 4);
map.put("u1", u1);
map.put("u2", u2);
map.put("u3", u3);
map.put("u4", u4);
Set set=map.entrySet();
Iterator itor=set.iterator();
while (itor.hasNext()) {
Entry<Object, Object> entry=(Entry<Object, Object>) (itor.next());
System.out.print(entry.getKey()+" : ");
System.out.println(entry.getValue()+" ");
}
}
}
转载于:https://my.oschina.net/boonya/blog/132848
上一篇: 双向带头循环链表实现
下一篇: Python基本类型的方法总结