Java Map 按value排序、TreeMap、linkedHashMap笔记_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Java Map 按value排序、TreeMap、linkedHashMap笔记

Java Map 按value排序、TreeMap、linkedHashMap笔记

 2013/10/9 22:02:50  raozhiyong11  程序员俱乐部  我要评论(0)
  • 摘要:publicstaticvoidmain(String[]args){Map<String,Object[]>m=newHashMap<String,Object[]>();m.put("1",newObject[]{1,"ff"});m.put("2",newObject[]{11,"dd"});m.put("3",newObject[]{23,"ee"});m.put("4",newObject[]{11,"gg"});for(Iteratoriterator=m
  • 标签:笔记 has Map Hash Java
class="java">	public static void main(String[] args) {
		Map<String,Object[]> m = new HashMap<String, Object[]>();
		
		m.put("1", new Object[]{1,"ff"});
		m.put("2", new Object[]{11,"dd"});
		m.put("3", new Object[]{23,"ee"});
		m.put("4", new Object[]{11,"gg"});
		
		for (Iterator iterator = m.entrySet().iterator(); iterator.hasNext();) {
			Entry<String, Object[]> type = (Entry<String, Object[]>) iterator.next();
			Object[] obj1s = ((Entry<String,Object[]>)type).getValue();
			System.out.println(type.getKey()+"-->"+obj1s[0]+"::"+obj1s[1]);
		}
		
		
		ArrayList<Entry<String,Object[]>> list = new ArrayList<Entry<String,Object[]>>(m.entrySet());   
		//根据value排序
		Collections.sort(list, new Comparator<Object>() {
			public int compare(Object e1, Object e2) {

				Object[] obj1s = ((Entry<String, Object[]>) e1).getValue();
				Object[] obj2s = ((Entry<String, Object[]>) e2).getValue();
				// String obj1 = (String) obj1s[1];
				// String obj2 = (String) obj2s[1];

				Integer obj1 = (Integer) obj1s[0];
				Integer obj2 = (Integer) obj2s[0];

				return obj1.compareTo(obj2);

			}
		});  
		System.out.println("============");
		
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Entry<String, Object[]> type = (Entry<String, Object[]>) iterator.next();
			Object[] obj1s = ((Entry<String,Object[]>)type).getValue();
			System.out.println(type.getKey()+"-->"+obj1s[0]+"::"+obj1s[1]);
		}
		
		//LinkedHashMap记录元素顺序,默认为插入顺序输出,够着函数可以指定,为访问最近的元素输出,LUR算法
		Map<String,Object[]> linked = new LinkedHashMap<String, Object[]>(20,0.75f,true);
		
		linked.put("1", new Object[]{1,"ff"});
		linked.put("2", new Object[]{11,"dd"});
		linked.put("3", new Object[]{23,"ee"});
		linked.put("4", new Object[]{11,"gg"});
		
		System.out.println("============");
		
		linked.get("4");
		linked.get("4");
		linked.get("1");
		linked.get("1");
		linked.get("4");
		
		for (Iterator iterator = linked.entrySet().iterator(); iterator.hasNext();) {
			Entry<String, Object[]> type = (Entry<String, Object[]>) iterator.next();
			Object[] obj1s = ((Entry<String,Object[]>)type).getValue();
			System.out.println(type.getKey()+"-->"+obj1s[0]+"::"+obj1s[1]);
		}

		//TreeMap实现排序,sortedMap是TreeMap的实现
		TreeMap<String,Object[]> tree = new TreeMap<String, Object[]>();
		
		tree.put("1", new Object[]{1,"ff"});
		tree.put("6", new Object[]{11,"dd"});
		tree.put("3", new Object[]{23,"ee"});
		tree.put("4", new Object[]{11,"gg"});
		
		System.out.println("============");

		
		for (Iterator iterator = tree.entrySet().iterator(); iterator.hasNext();) {
			Entry<String, Object[]> type = (Entry<String, Object[]>) iterator.next();
			Object[] obj1s = ((Entry<String,Object[]>)type).getValue();
			System.out.println(type.getKey()+"-->"+obj1s[0]+"::"+obj1s[1]);
		}
		
	}
	

?

上一篇: 尝试简化基于hbase的java应用开发的simplehbase介绍 下一篇: 没有下一篇了!
发表评论
用户名: 匿名