java-8

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>

Reducing a list of UnaryOperators in Java 8

旧巷老猫 提交于 2021-01-21 18:49:20
问题 What is the preferred way of reducing a list of UnaryOperators in Java 8 till they represent one UnaryOperator that I can call apply on? For example I have the following interface MyFilter extends UnaryOperator<MyObject>{}; public MyObject filterIt(List<MyFilter> filters,MyObject obj){ Optional<MyFilter> mf = filters .stream() .reduce( (f1,f2)->(MyFilter)f1.andThen(f2)); return mf.map(f->f.apply(obj)).orElse(obj); } But this code throws a ClassCastException at (MyFilter)f1.andThen(f2) . I

Reducing a list of UnaryOperators in Java 8

守給你的承諾、 提交于 2021-01-21 18:44:01
问题 What is the preferred way of reducing a list of UnaryOperators in Java 8 till they represent one UnaryOperator that I can call apply on? For example I have the following interface MyFilter extends UnaryOperator<MyObject>{}; public MyObject filterIt(List<MyFilter> filters,MyObject obj){ Optional<MyFilter> mf = filters .stream() .reduce( (f1,f2)->(MyFilter)f1.andThen(f2)); return mf.map(f->f.apply(obj)).orElse(obj); } But this code throws a ClassCastException at (MyFilter)f1.andThen(f2) . I

Reducing a list of UnaryOperators in Java 8

ⅰ亾dé卋堺 提交于 2021-01-21 18:43:17
问题 What is the preferred way of reducing a list of UnaryOperators in Java 8 till they represent one UnaryOperator that I can call apply on? For example I have the following interface MyFilter extends UnaryOperator<MyObject>{}; public MyObject filterIt(List<MyFilter> filters,MyObject obj){ Optional<MyFilter> mf = filters .stream() .reduce( (f1,f2)->(MyFilter)f1.andThen(f2)); return mf.map(f->f.apply(obj)).orElse(obj); } But this code throws a ClassCastException at (MyFilter)f1.andThen(f2) . I

Convert Java 8 Lambda Function to Java 7

好久不见. 提交于 2021-01-21 10:27:32
问题 Hey I'm new to coding and I've kind of gotten a hold of Java 8's Lambda functions but I'm trying to convert some of the code I wrote to Java 7 for a project in school and I can't wrap my head around how to make this piece of code identical in functionality but in java 7. Sorry if this is a dumb questions but I can't seem to figure it out. Do I write a custom method and then apply it to the my PriorityQueue. open = new PriorityQueue<>((Object o1, Object o2) -> { Cell c1 = (Cell)o1; Cell c2 =