[英]sorting a List of Map<String, String>
我有一個像這樣創建的列表變量:
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
在我的 Android 應用程序中,這個列表被填充。
只是一個例子:
Map<String, String> map1 = new HashMap<String, String>();
map.put("name", "Josh");
...
Map<String, String> map2 = new HashMap<String, String>();
map.put("name", "Anna");
...
Map<String, String> map3 = new HashMap<String, String>();
map.put("name", "Bernie");
...
list.add(map1);
list.add(map2);
list.add(map3);
我使用list
通過擴展BaseAdapter
並實現各種方法在ListView
顯示結果。
我的問題:我需要根據地圖的鍵名按字母順序對list
進行排序
問題:根據地圖的鍵名按字母順序對list
進行排序的簡單方法是什么?
我似乎無法解決這個問題。 我已將每個Map
中的每個名稱提取到一個String
數組中,並對其進行排序( Arrays.sort(strArray);
)。 但這不會保留每個Map
的其他數據,所以我不太確定如何保留其他映射值
以下代碼完美運行
public Comparator<Map<String, String>> mapComparator = new Comparator<Map<String, String>>() {
public int compare(Map<String, String> m1, Map<String, String> m2) {
return m1.get("name").compareTo(m2.get("name"));
}
}
Collections.sort(list, mapComparator);
但是您的地圖可能應該是特定類的實例。
您應該實現一個Comparator<Map<String, String>>
,它基本上從它傳遞的兩個地圖中提取“名稱”值,並比較它們。
然后使用Collections.sort(list, comparator)
。
你確定Map<String, String>
真的是你列表中最好的元素類型嗎? 也許你應該有另一個包含Map<String, String>
但也有getName()
方法的類?
@Test
public void testSortedMaps() {
Map<String, String> map1 = new HashMap<String, String>();
map1.put("name", "Josh");
Map<String, String> map2 = new HashMap<String, String>();
map2.put("name", "Anna");
Map<String, String> map3 = new HashMap<String, String>();
map3.put("name", "Bernie");
List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
mapList.add(map1);
mapList.add(map2);
mapList.add(map3);
Collections.sort(mapList, new Comparator<Map<String, String>>() {
public int compare(final Map<String, String> o1, final Map<String, String> o2) {
return o1.get("name").compareTo(o2.get("name"));
}
});
Assert.assertEquals("Anna", mapList.get(0).get("name"));
Assert.assertEquals("Bernie", mapList.get(1).get("name"));
Assert.assertEquals("Josh", mapList.get(2).get("name"));
}
如果你想使用 lamdas 並讓它更容易閱讀
List<Map<String,String>> results;
Comparator<Map<String,String>> sortByName = Comparator.comparing(x -> x.get("Name"));
public void doSomething(){
results.sort(sortByName)
}
您需要創建一個比較器。 我不確定為什么每個值都需要自己的映射,但這里是比較器的樣子:
class ListMapComparator implements Comparator {
public int compare(Object obj1, Object obj2) {
Map<String, String> test1 = (Map<String, String>) obj1;
Map<String, String> test2 = (Map<String, String>) obj2;
return test1.get("name").compareTo(test2.get("name"));
}
}
你可以看到它與你上面的例子一起工作:
public class MapSort {
public List<Map<String, String>> testMap() {
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String, String> myMap1 = new HashMap<String, String>();
myMap1.put("name", "Josh");
Map<String, String> myMap2 = new HashMap<String, String>();
myMap2.put("name", "Anna");
Map<String, String> myMap3 = new HashMap<String, String>();
myMap3.put("name", "Bernie");
list.add(myMap1);
list.add(myMap2);
list.add(myMap3);
return list;
}
public static void main(String[] args) {
MapSort ms = new MapSort();
List<Map<String, String>> testMap = ms.testMap();
System.out.println("Before Sort: " + testMap);
Collections.sort(testMap, new ListMapComparator());
System.out.println("After Sort: " + testMap);
}
}
您將收到一些類型安全的警告,因為我並不擔心這些。 希望有幫助。
不是你的問題的真正答案,但我有一個要求按其值的屬性對地圖列表進行排序,這也適用於你的情況:
List<Map<String, Object>> sortedListOfMaps = someListOfMaps.sorted(Comparator.comparing(map -> ((String) map.get("someKey")))).collect(Collectors.toList()))
try {
java.util.Collections.sort(data,
new Comparator<Map<String, String>>() {
SimpleDateFormat sdf = new SimpleDateFormat(
"MM/dd/yyyy");
public int compare(final Map<String, String> map1,
final Map<String, String> map2) {
Date date1 = null, date2 = null;
try {
date1 = sdf.parse(map1.get("Date"));
date2 = sdf.parse(map2.get("Date"));
} catch (ParseException e) {
e.printStackTrace();
}
if (date1.compareTo(date2) > 0) {
return +1;
} else if (date1.compareTo(date2) == 0) {
return 0;
} else {
return -1;
}
}
});
} catch (Exception e) {
}
有點跑題
這是一個用於觀看共享首選項的小工具
基於上層答案
可能對某人有幫助
@SuppressWarnings("unused")
public void printAll() {
Map<String, ?> prefAll = PreferenceManager
.getDefaultSharedPreferences(context).getAll();
if (prefAll == null) {
return;
}
List<Map.Entry<String, ?>> list = new ArrayList<>();
list.addAll(prefAll.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, ?>>() {
public int compare(final Map.Entry<String, ?> entry1, final Map.Entry<String, ?> entry2) {
return entry1.getKey().compareTo(entry2.getKey());
}
});
Timber.i("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
Timber.i("Printing all sharedPreferences");
for(Map.Entry<String, ?> entry : list) {
Timber.i("%s: %s", entry.getKey(), entry.getValue());
}
Timber.i("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
有很多方法可以解決相同的問題。 下面給出了使用 Java 8 解決問題的最簡單方法之一:
根據您的要求,根據地圖的鍵名稱按字母順序排序
第一種方式:
list = list.stream()
.sorted((a,b)-> (a.get("name")).compareTo(b.get("name")))
.collect(Collectors.toList());
或者,
list = list.stream()
.sorted(Comparator.comparing(map->map.get("name")))
.collect(Collectors.toList());
第二種方式:
Collections.sort(list, Comparator.comparing(map -> map.get("name")));
第三種方式:
list.sort(Comparator.comparing(map-> map.get("name")));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.