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()就可以 实现排序了。

• 本文已收录于以下专栏：

举报原因： 您举报文章：map 排序问题 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)