cari

Rumah  >  Soal Jawab  >  teks badan

java中map可以按插入顺序倒序输出么?

public class Test {
    public static void main(String[] args) {
        LinkedHashMap<String,String>  a=new LinkedHashMap<String,String>();
        a.put("a", "1");
        a.put("b", "2");
        a.put("c", "3");
        for(String b:a.keySet()){
            System.out.println(b+"值为:"+a.get(b));
        }
        
    }
}

输出为
a值为:1
b值为:2
c值为:3
想要倒序输出怎么实现

大家讲道理大家讲道理2802 hari yang lalu840

membalas semua(1)saya akan balas

  • 迷茫

    迷茫2017-04-18 10:52:17

    Ujian kelas awam {

    public static void main(String[] args) {
        LinkedHashMap<String,String>  linkedhashmap = new LinkedHashMap<String,String>();
        linkedhashmap .put("a", "1");
        linkedhashmap .put("b", "2");
        linkedhashmap .put("c", "3");
        ListIterator<Map.Entry<String,String>> i = new ArrayList<Map.Entry<String,String>>(linkedhashmap.entrySet()).listIterator(linkedhashmap.size());
        
        while(i.hasPrevious()) {  
            Map.Entry<String, String> entry=i.previous();  
            System.out.println(entry.getKey()+":"+entry.getValue());  
        }
        
    }

    }

    balas
    0
  • Batalbalas