java筆記


//信件
<span id="todocount">${totals }</span>
$(function(){
window.parent.$("#todocount").html(${totals});

});

==========================


//集合對比,選中
<s:iterator value="annexList" id="an">
                <s:if test ="annexIds.contains(#an.id)">
                <input id="attachment" name="attachment" checked="checked" type="checkbox" value="${an.id}"> ${an.annexName }
                </s:if>
                <s:else>
                <input id="attachment" name="attachment" type="checkbox" value="${an.id}"> ${an.annexName }
                </s:else>
</s:iterator>       

=========================


初始化一個map

Map<String, String> map = new HashMap<String, String>();
map.put("1", "hell");
map.put("2", "hello");
map.put("3", "hel");
map.put("4", "hello");

1、第一種方式,普遍使用

Set<String> keySet = map.keySet();
for (String key : keySet) { 
    System.out.println("key= " + key + " and value= " + map.get(key));
}

2、第二種方式, 容量大時 推薦使用

Set<Map.Entry<String,String>> entySet =  map.entrySet();
for (Map.Entry<String, String> entry : entySet) {
	System.out.println("key= " + entry.getKey() + " and value= "
			+ entry.getValue());
}

實驗發現輸出的順序是亂的,排個序吧

1、按照key值排序

首先寫個排序類

private static class KeyComparator implements
        Comparator<Map.Entry<String, String>> {
    public int compare(Map.Entry<String, String> m,
            Map.Entry<String, String> n) {
        return m.getKey().compareTo(n.getKey());
    }
}

把數據放在list裏邊纔可以使用

List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>();
list.addAll(map.entrySet());

KeyComparator kc = new KeyComparator();
Collections.sort(list, kc);
for (Iterator<Map.Entry<String, String>> it = list.iterator(); it
        .hasNext();) {
    System.out.println(it.next());
}

2、按照Value值排序

private static class ValueComparator implements
        Comparator<Map.Entry<String, String>> {
    public int compare(Map.Entry<String, String> m,
            Map.Entry<String, String> n) {
        return m.getValue().compareTo(n.getValue());
    }
}

排序輸出

list.clear();
list.addAll(map.entrySet());
ValueComparator vc = new ValueComparator();
Collections.sort(list, vc);
for (Iterator<Map.Entry<String, String>> it = list.iterator(); 
    it.hasNext();) {
    System.out.println(it.next());
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章