import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class HelloWorld {
public static void main(String[] args) {
List<Map<String, String>> msgList = new ArrayList<>();
Map<String, String> msgMap = new HashMap<>();
msgMap.put("1", "test[warning_726]");
msgList.add(msgMap);
msgMap = new HashMap<>();
msgMap.put("1", "test3333[warning_727]");
msgList.add(msgMap);
msgMap = new HashMap<>();
msgMap.put("1","test2[warning_736]");
msgList.add(msgMap);
msgMap = new HashMap<>();
msgMap.put("1","test21[914_warning]");
msgList.add(msgMap);
msgMap = new HashMap<>();
msgMap.put("1","test255[warning_737]");
msgList.add(msgMap);
msgMap = new HashMap<>();
msgMap.put("2","error_728");
msgList.add(msgMap);
// msgList: [{1=test[warning_726]}, {1=test3333[warning_727]}, {1=test2[warning_736]}, {1=test21[914_warning]}, {1=test255[warning_737]}, {2=error_728}]
System.out.println("msgList: " + msgList);
/*
* test3333 の値を含む物の数を取得
*/
var a = msgList.stream()
.flatMap(msg -> msg.entrySet().stream())
.filter(entry -> entry.getValue().contains("test3333"))
.count();
// a : 1
System.out.println("a: " + a);
/*
* test3333 の値を含む物を取得
*/
var b = msgList.stream()
.flatMap(msg -> msg.entrySet().stream())
.filter(entry -> entry.getValue().contains("test3333"))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
// b: {1=test3333[warning_727]}
System.out.println("b: " + b);
/*
* warning_726 の値が含まれない物を全て取得
*/
msgList = msgList.stream()
.filter(map -> map.entrySet().stream()
.noneMatch(entry -> entry.getValue().contains("warning_726")))
.collect(Collectors.toList());
/*
* warning_727 の値が含まれない物を全て取得
*/
msgList = msgList.stream()
.filter(map -> map.entrySet().stream()
.noneMatch(entry -> entry.getValue().contains("warning_727")))
.collect(Collectors.toList());
// msgList: [{1=test2[warning_736]}, {1=test21[914_warning]}, {1=test255[warning_737]}, {2=error_728}]
System.out.println("msgList: " + msgList);
}
}
More than 1 year has passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme