标签:android 安卓 java map
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
/**
* @author TEANA E-mail: mmz06@163.com
* @version 创建时间:2011-1-21 下午02:23:07
* @DO LinkedHashMap与HashMap
*/
public class LinkedMap
{
public static void main(String[] args)
{
//LinkedHashMap 有序
Map maps = new LinkedHashMap();
maps.put("1", "张三");
maps.put("2", "李四");
maps.put("3", "王五");
maps.put("4", "赵六");
System.out.println("LinkedHashMap(有序):");
Iterator it = maps.entrySet().iterator();
while(it.hasNext())
{
Map.Entry entity = (Entry) it.next();
System.out.println("[ key = " + entity.getKey() +
", value = " + entity.getValue() + " ]");
}
//HashMap 无序
Map map = new HashMap();
map.put("1", "张三");
map.put("2", "李四");
map.put("3", "王五");
map.put("4", "赵六");
it = null;
System.out.println("HashMap(无序):");
it = map.entrySet().iterator();
while(it.hasNext())
{
Map.Entry entity = (Entry) it.next();
System.out.println("[ key = " + entity.getKey() +
", value = " + entity.getValue() + " ]");
}
}
} import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
public class MapAppTest {
/**
* @Create on Nov 9, 2009 by lrm
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MapAppTest.noOrder();
MapAppTest.hasOrder();
MapAppTest.likedHashMap();
}
public static void noOrder() {
System.out.println("------无序(随机输出------");
Map map = new HashMap();
map.put("1", "Level 1");
map.put("2", "Level 2");
map.put("3", "Level 3");
map.put("4", "Level 4");
map.put("F", "Level F");
map.put("Q", "Level Q");
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry e = (Map.Entry) it.next();
System.out.println("Key: " + e.getKey() + "; Value: "
+ e.getValue());
}
}
// 有序(默认排序,不能指定)
public static void hasOrder() {
System.out.println("------有序(但是按默认顺充,不能指定)------");
Map map = new TreeMap();
map.put("F", "Level F");
map.put("7", "Level 1");
map.put("8", "Level 2");
map.put("4", "Level 3");
map.put("4", "Level 4");
map.put("Q", "Level Q");
map.put("E", "Level E");
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry e = (Map.Entry) it.next();
System.out.println("Key: " + e.getKey() + "; Value: "
+ e.getValue());
}
}
public static void likedHashMap() {
System.out.println("------有序(根据输入的顺序输出)------");
Map map = new LinkedHashMap();
map.put("F", "Level F");
map.put("7", "Level 1");
map.put("8", "Level 2");
map.put("4", "Level 3");
map.put("4", "Level 4");
map.put("Q", "Level Q");
map.put("E", "Level E");
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry e = (Map.Entry) it.next();
System.out.println("Key: " + e.getKey() + "; Value: "
+ e.getValue());
}
}
} 版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:android 安卓 java map
原文地址:http://blog.csdn.net/u012889434/article/details/48055679