java-stream

Comparing two collections using Stream - anyMatch

孤者浪人 提交于 2021-01-26 21:16:23
问题 I want to compare if any of the objects in a list2 is present in a list1 . I could iterate over both lists and compare all elements using .contains() but I am wondering if there is not a more efficient way. I found this and I am trying to implement the suggested method: List<Item> list1; List<Item> list2; boolean anyMatch = list1.stream().anyMatch(x -> x.equals(list2.stream())); System.out.println(anyMatch); When I do this I constantly get false , even when I'd expect a true . How come? 回答1:

Java 8 stream to collect a Map of List of items

非 Y 不嫁゛ 提交于 2021-01-26 18:15:30
问题 I have a List of Maps which stores the roles and the names of people. For ex: List<Map<String, String>> listOfData 1) Role: Batsman Name: Player1 2)Role: Batsman Name: Player2 3)Role: Bowler Name: Player3 Role and Name are the Keys of the map. I want to convert this into a Map<String, List<String>> result , which will give me a list of names for each role, i.e k1: Batsman v1: [Player1, Player2] k2: Bowler v2: [Player3] listOfData .stream() .map(entry -> new AbstractMap.SimpleEntry<>(entry.get

Java 8 stream to collect a Map of List of items

╄→尐↘猪︶ㄣ 提交于 2021-01-26 18:12:23
问题 I have a List of Maps which stores the roles and the names of people. For ex: List<Map<String, String>> listOfData 1) Role: Batsman Name: Player1 2)Role: Batsman Name: Player2 3)Role: Bowler Name: Player3 Role and Name are the Keys of the map. I want to convert this into a Map<String, List<String>> result , which will give me a list of names for each role, i.e k1: Batsman v1: [Player1, Player2] k2: Bowler v2: [Player3] listOfData .stream() .map(entry -> new AbstractMap.SimpleEntry<>(entry.get

Java 8 stream to collect a Map of List of items

[亡魂溺海] 提交于 2021-01-26 18:10:23
问题 I have a List of Maps which stores the roles and the names of people. For ex: List<Map<String, String>> listOfData 1) Role: Batsman Name: Player1 2)Role: Batsman Name: Player2 3)Role: Bowler Name: Player3 Role and Name are the Keys of the map. I want to convert this into a Map<String, List<String>> result , which will give me a list of names for each role, i.e k1: Batsman v1: [Player1, Player2] k2: Bowler v2: [Player3] listOfData .stream() .map(entry -> new AbstractMap.SimpleEntry<>(entry.get

Java 8 stream to collect a Map of List of items

。_饼干妹妹 提交于 2021-01-26 18:08:16
问题 I have a List of Maps which stores the roles and the names of people. For ex: List<Map<String, String>> listOfData 1) Role: Batsman Name: Player1 2)Role: Batsman Name: Player2 3)Role: Bowler Name: Player3 Role and Name are the Keys of the map. I want to convert this into a Map<String, List<String>> result , which will give me a list of names for each role, i.e k1: Batsman v1: [Player1, Player2] k2: Bowler v2: [Player3] listOfData .stream() .map(entry -> new AbstractMap.SimpleEntry<>(entry.get

Java 8 stream to collect a Map of List of items

心不动则不痛 提交于 2021-01-26 18:08:12
问题 I have a List of Maps which stores the roles and the names of people. For ex: List<Map<String, String>> listOfData 1) Role: Batsman Name: Player1 2)Role: Batsman Name: Player2 3)Role: Bowler Name: Player3 Role and Name are the Keys of the map. I want to convert this into a Map<String, List<String>> result , which will give me a list of names for each role, i.e k1: Batsman v1: [Player1, Player2] k2: Bowler v2: [Player3] listOfData .stream() .map(entry -> new AbstractMap.SimpleEntry<>(entry.get

Java 8 stream to collect a Map of List of items

血红的双手。 提交于 2021-01-26 18:08:08
问题 I have a List of Maps which stores the roles and the names of people. For ex: List<Map<String, String>> listOfData 1) Role: Batsman Name: Player1 2)Role: Batsman Name: Player2 3)Role: Bowler Name: Player3 Role and Name are the Keys of the map. I want to convert this into a Map<String, List<String>> result , which will give me a list of names for each role, i.e k1: Batsman v1: [Player1, Player2] k2: Bowler v2: [Player3] listOfData .stream() .map(entry -> new AbstractMap.SimpleEntry<>(entry.get

Java 8 stream to collect a Map of List of items

我的未来我决定 提交于 2021-01-26 18:07:29
问题 I have a List of Maps which stores the roles and the names of people. For ex: List<Map<String, String>> listOfData 1) Role: Batsman Name: Player1 2)Role: Batsman Name: Player2 3)Role: Bowler Name: Player3 Role and Name are the Keys of the map. I want to convert this into a Map<String, List<String>> result , which will give me a list of names for each role, i.e k1: Batsman v1: [Player1, Player2] k2: Bowler v2: [Player3] listOfData .stream() .map(entry -> new AbstractMap.SimpleEntry<>(entry.get

Java 8 extract all keys from matching values in a Map

懵懂的女人 提交于 2021-01-26 03:15:20
问题 I'm relatively new to Java8 and I have a scenario where I need to retrieve all the keys from the Map which matched with the objects. Wanted to know if there is a way to get all keys without iterating them from the list again. Person.java private String firstName; private String lastName; //setters and getters & constructor MAIN Class. String inputCriteriaFirstName = "john"; Map<String, Person> inputMap = new HashMap<>(); Collection<Person> personCollection = inputMap.values(); List<Person>

Java 8 extract all keys from matching values in a Map

Deadly 提交于 2021-01-26 03:13:24
问题 I'm relatively new to Java8 and I have a scenario where I need to retrieve all the keys from the Map which matched with the objects. Wanted to know if there is a way to get all keys without iterating them from the list again. Person.java private String firstName; private String lastName; //setters and getters & constructor MAIN Class. String inputCriteriaFirstName = "john"; Map<String, Person> inputMap = new HashMap<>(); Collection<Person> personCollection = inputMap.values(); List<Person>