package com.swift;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap; public class Map_keySet_entrySet { public static void main(String[] args) {
/*
* TreeMap 集合存储自定义对象,并使用 2 中方式遍历获取
*/ Map<Person,String> hm = new HashMap<Person, String>();
hm.put(new Person("lisi",18), "加拿大");
hm.put(new Person("zhangsa",17), "澳大利亚");
hm.put(new Person("zhangsa",17), "澳大利亚");
hm.put(new Person("wangwu",20), "新加坡");
hm.put(new Person("zhaoliu",19), "新西兰");
hm.put(new Person("zhaoliu",19), "新西兰");
hm.put(new Person("lisa",22), "迪拜"); //使用TreeMap无法排序比较是会出现Comparable错误 这时需要自己弄比较器
Map<Person,String> tree = new TreeMap<Person, String>(new Comparator<Person>() { @Override
public int compare(Person arg0, Person arg1) {
int num=arg0.getAge()-arg1.getAge();
return num==0?arg0.getName().compareTo(arg1.getName()):num;
} });
tree.put(new Person("lisisi",18), "china"); //keySet方法一
Set<Person> set=hm.keySet();
for(Person per:set) {
System.out.println("人员:"+per.toString()+"来自:"+hm.get(per));
} Set<Person> set1=tree.keySet();
for(Person per:set1) {
System.out.println("人员:"+per.toString()+"来自:"+tree.get(per));
} //entrySet方法二
for(Map.Entry<Person, String> en:hm.entrySet()) {
System.out.println();
System.out.println("人员:"+en.getKey().toString()+"来自:"+en.getValue());
} Map<Person,String> tree1 = new TreeMap<Person, String>(new Comparator<Person>() { @Override
public int compare(Person arg0, Person arg1) {
int num=arg0.getAge()-arg1.getAge();
return num==0?arg0.getName().compareTo(arg1.getName()):num;
} });
tree1.put(new Person("fengqichanglin",18), "langyabang");
System.out.println();
Iterator<?> it=tree1.entrySet().iterator();
while(it.hasNext()) {
Entry<Person,String> entry=(Entry<Person, String>) it.next();
System.out.println(entry.getKey().toString()+entry.getValue());
} } }

package sortmap_demo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap; public class CollectionsSort { public static void main(String[] args) { Map<Integer,User> map=new TreeMap<Integer,User>();
map.put(1,new User("张三",28));
map.put(2,new User("李四",29));
map.put(3,new User("王五",30));
map.put(4,new User("赵六",31));
Set<Entry<Integer, User>> set = map.entrySet();
List<Entry<Integer, User>> list = new ArrayList<Entry<Integer, User>>(set);
Collections.sort(list,new Comparator<Entry<Integer, User>>() { @Override
public int compare(Entry<Integer, User> o1, Entry<Integer, User> o2) {
return o2.getValue().getAge()-o1.getValue().getAge();
}
});
for (Entry<Integer, User> u : list) {
System.out.println(u.getKey()+"--"+u.getValue());
}
}
}

不颠倒key和value的位置,用list集合api排序的方法

TreeMap根据key的Comparator对象排序方法

package sortmap_demo;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap; public class HashMapDemo { public static void main(String[] args) { Map<User,Integer> map=new TreeMap<User,Integer>(new Comparator<User>() { @Override
public int compare(User o1, User o2) {
if(o1.getAge()>o2.getAge()){
return -1;
}else if(o1.getAge()==o2.getAge()){
return 0;
}else{
return 1;
}
}
});
map.put(new User("张三",28),1);
map.put(new User("李四",29),2);
map.put(new User("王五",30),3);
map.put(new User("赵六",31),4);
for (User u : map.keySet()) {
System.out.println(u+"--"+map.get(u));
}
}
}

TreeMap 底层是红黑树 排序是根据key值进行的 添加元素时异常 Comparable异常 Comparator比较自定义对象放在键的位置的更多相关文章

  1. TreeMap:是基于红黑树的Map接口的实现

    > TreeMap:是基于红黑树的Map接口的实现. 红黑树:平衡二叉树 取出时,可以有三种方式:前序遍历,中序遍历,后序遍历 >排序: A 自然排序  --TreeMap无参构造 Tre ...

  2. stl map底层之红黑树插入步骤详解与代码实现

    转载注明出处:http://blog.csdn.net/mxway/article/details/29216199 本篇文章并没有详细的讲解红黑树各方面的知识,只是以图形的方式对红黑树插入节点需要进 ...

  3. 红黑树之 原理和算法详细介绍(阿里面试-treemap使用了红黑树) 红黑树的时间复杂度是O(lgn) 高度<=2log(n+1)1、X节点左旋-将X右边的子节点变成 父节点 2、X节点右旋-将X左边的子节点变成父节点

    红黑树插入删除 具体参考:红黑树原理以及插入.删除算法 附图例说明   (阿里的高德一直追着问) 或者插入的情况参考:红黑树原理以及插入.删除算法 附图例说明 红黑树与AVL树 红黑树 的时间复杂度 ...

  4. 【算法】通过TreeMap理解红黑树

    本文以Java TreeMap为例,从源代码层面,结合详细的图解,剥茧抽丝地讲解红黑树(Red-Black tree)的插入,删除以及由此产生的调整过程. 总体介绍 Java TreeMap实现了So ...

  5. TreeMap红黑树

    Java TreeMap实现了SortedMap接口,也就是说会按照key的大小顺序对Map中的元素进行排序,key大小的评判可以通过其本身的自然顺序(natural ordering),也可以通过构 ...

  6. 红黑树规则,TreeSet原理,HashSet特点,什么是哈希值,HashSet底层原理,Map集合特点,Map集合遍历方法

    ==学习目标== 1.能够了解红黑树 2.能够掌握HashSet集合的特点以及使用(特点以及使用,哈希表数据结构) 3.能够掌握Map集合的特点以及使用(特点,常见方法,Map集合的遍历) 4.能够掌 ...

  7. 【深入理解Java集合框架】红黑树讲解(上)

    来源:史上最清晰的红黑树讲解(上) - CarpenterLee 作者:CarpenterLee(转载已获得作者许可,如需转载请与原作者联系) 文中所有图片点击之后均可查看大图! 史上最清晰的红黑树讲 ...

  8. 通过分析 JDK 源代码研究 TreeMap 红黑树算法实现

    本文转载自http://www.ibm.com/developerworks/cn/java/j-lo-tree/ 目录: TreeSet 和 TreeMap 的关系 TreeMap 的添加节点 Tr ...

  9. java中treemap和treeset实现(红黑树)

    java中treemap和treeset实现(红黑树)   TreeMap 的实现就是红黑树数据结构,也就说是一棵自平衡的排序二叉树,这样就可以保证当需要快速检索指定节点. TreeSet 和 Tre ...

随机推荐

  1. codeforces704D Captain America【上下界最大流】

    分别给行和列hash建两排点,对(x,y)坐标连x行y列的点 设红色价格低,那么就要尽量多选红色 设一个点出度为s,要求最小的最大差值为d,又,假设有流量表示选红没流量表示选蓝,那么要求就变成了这个点 ...

  2. 9.Python初窥门径(函数初识)

    Python(函数初识) 一.初识函数 面向过程缺点 : 代码重复,代码描述性不高 函数 : 函数是以功能为导向,一个函数封装一个功能. 函数的优点 : 减少代码重复性,增强了代码的可读性 二.函数的 ...

  3. P1291-添加括号(区间dp)

    题目背景 给定一个正整数序列a(1),a(2),...,a(n),(1<=n<=20) 不改变序列中每个元素在序列中的位置,把它们相加,并用括号记每次加法所得的和,称为中间和. 例如: 给 ...

  4. Mac环境下制作ubantu安装盘

    前言:ubantu为Linux发行版之一,此方法亦可制作其他Linux发行版 1.在磁盘工具中将准备好的u盘格式化为Mac OS扩展(日志型),并确保分区的模式是GUID分区 2.官网自行下载uban ...

  5. 15 Puzzle LightOJ - 1121

    https://cn.vjudge.net/problem/LightOJ-1121 #include<cstdio> #include<algorithm> #include ...

  6. 命令行下载工具 wget

    wget 是一个简单而强大的跨平台命令行下载工具,包括 Windows 也有对应的版本.全称 GNU Wget,属于 GNU 计划的一部分,自由软件.支持 HTTP.HTTPS 和 FTP 协议,可在 ...

  7. nodejs学习8:windows连接mongodb出现的错误解决办法

    今天遇到了在windows下连接mongodb错误的情况,因为之前安装是正常的,而重启的电脑之后就再也连接不上.于是在群里求助了下,无果,查阅了官网的英文文档,终于有些眉目了,故此一记. 先吐槽下命令 ...

  8. Android RecyclerView使用GridLayoutManager间距设置

    使用RecyclerView设置间距,需要重写RecyclerView.ItemDecoration这个类.有如下的效果图需要实现,间距只有中间的格子和底部的格式之间有.   Paste_Image. ...

  9. 使用PowerShell 获取azure image publisher offer sku 信息

    使用azure powershell 获取指定区域的可用镜像 publisher offer sku信息 param ( [parameter(Mandatory = $false)] $Locati ...

  10. (一)我的Javascript系列:Javascript的面向对象旅程(上)

    今宵酒醒何处,杨柳岸,晓风残月 导引 我的JavaScript系列文章是我自己对JavaScript语言的感悟所撰写的系列文章.现在还没有写完.目前一共出了下面的系列: (三)我的JavaScript ...