# map 排序问题

Map<Integer, Player> playerList = Collections
.synchronizedMap(new HashMap<Integer, Player>());
ConcurrentHashMap<Integer,Player> gamePlayer=
new ConcurrentHashMap<Integer, Player>();

ConcurrentHashMap<Integer,Integer> map =new ConcurrentHashMap<Integer, Integer>();
map.put(2, 2);
map.put(1, 1);
map.put(3, 3);
Iterator<Integer> it = map.keySet().iterator();
while (it.hasNext()) {
System.out.println(map.get(it.next()));
}

int [] arr = new int[3];
for (int i = 0; i < arr.length; i++) {
for (int j = i+1; j < arr.length; j++) {
...
}

}

public static void main(String[] args) {

P p1 = new P(2);
P p2 = new P(1);
P p3 = new P(3);

List<P> list = new ArrayList<P>();
for (int i = 0; i < 10; i++) {
}

Object arr  []  = list.toArray();

for (int i = 0; i < arr.length; i++) {
P max = arr[i];
for (int j = i+1; j < arr.length; j++) {
P com = arr[j];
if(max.getId()<com.getId()){
P tmp = max;
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
}

class P{
private int id;
public int getId() {
return id;
}
public P(int id) {
this.id = id;
}
}

for (int i = 0; i < list.size(); i++) {
P max = list.get(i);
for (int j = i+1; j < list.size(); j++) {
P com = list.get(j);
if(max.getId()<com.getId()){
P tmp = max;
list.remove(i);
list.remove(j);
continue;
}
}
}

Collections.sort(list, new Comparator(){
public int compare(Object a, Object b) {
int a1 = ((P)a).getId();
int b1 =  ((P)b).getId();
if(a1>b1){
return 1;
}else if(a1<b1){
return -1;
}
return 0;
}
});

//return (new Integer(((P)b).getId()).compareTo(((P)a).getId()));

map也同样可以用上面的排序法 ，因为 map 也属于集合，将 list 换成 map.keySet()就可以 实现排序了。

#### 史上最详细的ConcurrentHashMap详解--源码分析

2016-03-31 19:20:35

#### Java中的面试题 [1] --- Java基础、集合、排序、算法、ConcurrentHashMap等

2017-05-15 09:37:01

#### ConcurrentHashMap 浅层次总结

2015-11-01 11:09:51

#### 【Java并发编程】深入分析ConcurrentHashMap(九)

2016-02-22 16:35:31

#### HashMap调优和ConcurrentHashMap分析

2017-03-28 20:31:13

#### Java中HashMap排序和遍历

2011-03-12 21:43:00

#### Java 对Map进行排序的实现方法

2016-03-26 11:47:31

#### java如何对map进行排序详解

2016-06-25 22:49:35

#### map集合排序

2017-08-04 12:15:13

#### Java 8 – 怎样对Map排序

2017-11-05 11:19:15