JAVA中集合的排序方法

2021-06-01 05:56:04 字數 610 閱讀 3148

1.專案中一些物件都是存在hashmap裡,hashmap本身是無序的,現在想對hashmap根據key排序後輸出value,可以利用collections類來快速完成有序輸出,下面是**片段:

hashmap compmap = new hashmap();

compmap.put("ooooo", null);

compmap.put("pppp", null);

compmap.put("aaaa", null);

compmap.put("ssss", null);

compmap.put("ssbb", null);

compmap.put("bbbb", null);

arraylistcomps = new arraylist(arrays.aslist(compmap.keyset().toarray()));

collections.sort(comps);

for(string key:comps){

system.out.print(key);

輸出結果為:

aaaa

bbbb

ooooo

pppp

ssbb

ssss

java中集合詳解

一 集合 是一種儲存資料的手段,一次可以儲存多個值 二 補充 短期儲存 rom 一旦計算機關閉,儲存的資料就會消失如 物件,變數,陣列,集合 長期儲存 ram 直接儲存到硬碟上,可以長久的儲存,不會隨著計算機的關閉而消失,如 各種檔案,txt,doc,png,mp4 三 學習集合 1.陣列 可以儲存...

C 中集合的排序

arraylist,list,dictionary等c 中常用到的集合都有乙個預設的sort方法,這個方法可以進行預設排序。但是如果這些集合中的元素是自定義的類,那麼這個預設的sort可能就不準確了。文章中使用到的命名空間是system,介面是 icomparable 和 icomparer。sor...

oracle中集合的方法

除了建構函式外,集合還有很多內建函式,這些函式稱為方法。呼叫方法的語法如下 collection.method 下表中列出oracle中集合的方法 方法 描述 使用限制 count 返回集合中元素的個數 delete 刪除集合中所有元素 delete 刪除元素下標為x的元素,如果x為null,則集合...