HashMap散列无序存储测试

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 : com.boonya.map.EntryTest$User@1b90b39
     * u4 : com.boonya.map.EntryTest$User@18fe7c3
     * u1 : com.boonya.map.EntryTest$User@b8df17
     * u3 : com.boonya.map.EntryTest$User@13e8d89
	 * @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()+" ");
		}
	}

}

你可能感兴趣的:(java,HashMap)