[英]How to check if a value is present in the list of map using Java8 Stream
[英]Sort list of map objects using multiple boolean keys in Java8
我正在嘗試對看起來像這樣的Map對象列表進行排序。
"list" : [
{
"up" : false,
more fields ....
},
{
"randomField" : "ABC",
"someMoreRandomField" : "Retirement"
more fields ....
},
{
"up" : false,
more fields ....
},
{
"last" : true,
"up" : false
more fields ....
},
{
"last" : true,
"up" : true
more fields ....
},
{
"up" : true
more fields ....
},
{
"up" : true
more fields ....
},
{
"up" : true
more fields ....
}
]
我希望根據這些條件對它們進行排序。 如果對象的鍵“ last”標記為true,則它們應在列表中位於最后。 如果鍵“ up”為真,則它們將首先出現在列表中。 如果鍵“ last”標記為true,則優先級高,即使同一對象的“ up”標記為true,它也應出現在列表的底部。
排序列表:
"list" : [
{
"up" : true
more fields ....
},
{
"up" : true
more fields ....
},
{
"up" : true
more fields ....
},
{
"up" : false,
more fields ....
},
{
"randomField" : "ABC",
"someMoreRandomField" : "Retirement"
more fields ....
},
{
"up" : false,
more fields ....
},
{
"last" : true,
"up" : false
more fields ....
},
{
"last" : true,
"up" : true
more fields ....
}
]
我能夠使用Vanilla JS來實現這一點,嘗試在Java 8中做同樣的事情。已經花了很長時間,偶然發現了幾個答案: https : //stackoverflow.com/a/46717991/2114024
但尚未找到解決方案。
JS代碼段:
list.sort(function(a,b) {
if(a.last === undefined) {a.last = false};
if(b.last === undefined) {b.last = false};
if(a.up === undefined) {a.up = false};
if(b.up === undefined) {b.up = false};
return Number(a.last) - Number(b.last) || Number(b.up) - Number(a.up);
});
任何建議表示贊賞。
我嘗試了與您申請Java腳本類似的邏輯。 您可以在Java 8中嘗試以下代碼:
public class ListMapSort {
public static void main(String[] args) {
List<Map<String, Object>> data = getData();
data.sort(Comparator.comparing(m -> {
int lastKey = (m.containsKey("last") && (boolean)m.get("last")) ? 1 : 0;
int upKey = (m.containsKey("up") && (boolean)m.get("up")) ? 0 : 1;
return lastKey*10 + upKey;
}));
System.out.println("Sorted data: " + data);
}
private static List<Map<String, Object>> getData() {
List<Map<String, Object>> data = new ArrayList<>();
Map<String, Object> map = new HashMap<>();
map.put("up", false);
map.put("id", 1);
data.add(map);
map = new HashMap<>();
map.put("up", false);
map.put("id", 2);
data.add(map);
map = new HashMap<>();
map.put("last", false);
map.put("up", false);
map.put("id", 3);
data.add(map);
map = new HashMap<>();
map.put("last", true);
map.put("up", true);
map.put("id", 4);
data.add(map);
map = new HashMap<>();
map.put("up", true);
map.put("id", 5);
data.add(map);
map = new HashMap<>();
map.put("last", false);
map.put("id", 6);
data.add(map);
map = new HashMap<>();
map.put("last", true);
map.put("id", 7);
data.add(map);
map = new HashMap<>();
map.put("last", false);
map.put("up", true);
map.put("id", 7);
data.add(map);
return data;
}
}
就像您提到的另一篇文章中所說的那樣,也可以使用這種方式解決該問題,
data.sort(Comparator.comparing(
(Map<String, Object> m) -> (boolean)m.getOrDefault("last", false))
.thenComparing(m -> !(boolean)m.getOrDefault("up", false)));
我可以使用@swapan的答案對它進行排序,但必須撤消否定。
data.sort(Comparator.comparing(
(Map<String, Object> m) -> !(boolean)m.getOrDefault("last", false))
.thenComparing(m -> (boolean)m.getOrDefault("up", false)));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.