欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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