Sort de MAP?

olá,

estou mostrando em um pagina jsp o map

abaixo mas está mostrando o codigo 2 e depois o 1

tem como fazer um sort e colocar na ordem…

abs



Map maps = new HashMap()

maps.put(new Long(01),"Relatorio 1");
maps.put(new Long(02),"Relatorio ((((((");

Em vez de um hashmap, use um TreeMap. Ele será ordenado pela chave.

Cara usa o SortedMap que ele ja coloca em ondem você nao precisa fazer nada
é so criar assim

   SortedMap<Long, String> sortedMap = new TreeMap<Long, String>();
      sortedMap.put(50L, "AAAAAA");
      sortedMap.put(10L, "BBBBBB");
      sortedMap.put(80L, "XXXXXX");
      sortedMap.put(1L,  "OOOOOO");
		
      for(Map.Entry<Long, String> ordenar : sortedMap.entrySet()){
			
	System.out.println( ordenar.getKey() + " : " + ordenar.getValue());
			
      }    

Abraço

Se, por ventura, precisar usaro HashMap antes:

Map maps = new HashMap()  
   
maps.put(new Long(01),"Relatorio 1");  
maps.put(new Long(02),"Relatorio ((((((");

TreeMap tmap = new TreeMap(maps);

Set<Long> chaves = tmap.keySet();

for (long chave : chaves){
   System.out.println(chave + " - " + tmap.get(chave));
}

Senão, faça como o colega falou. Use o TreeMap diretamente:

TreeMap tmap = new TreeMap();
		
tmap.put(new Long(01),"Relatorio 1");    
tmap.put(new Long(02),"Relatorio ((((((");

Set<Long>chaves = tmap.keySet();

for (long chave : chaves){
	System.out.println(chave + " " + tmap.get(chave));			
}

:wink:

ThreeMap ordena pela ordem Natural. Se quiser outra ordem, passe no construtor um comparator.

Fernando