java-stream

Stuck with lambda expression and Map

a 夏天 提交于 2020-12-05 05:28:10
问题 I have the Person class: import java.util.*; public class Person { private String name; Map<String,Integer> Skills=new HashMap<>(); // skill name(String) and level(int) public String getName(){ return this.name; } public Map<String,Integer> getSkills(){ return this.Skills; } } And the App class: import java.util.*; import java.util.Map.Entry; import static java.util.stream.Collectors.*; import static java.util.Comparator.*; public class App { private List<Person> people=new ArrayList<>(); //

How to remove multiple elements from Set/Map AND knowing which ones were removed?

故事扮演 提交于 2020-12-02 03:32:43
问题 I have a method that has to remove any element listed in a (small) Set<K> keysToRemove from some (potentially large) Map<K,V> from . But removeAll() doesn't do, as I need to return all keys that were actually removed, since the map might or might not contain keys that require removal. Old-school code is straight forward: public Set<K> removeEntries(Map<K, V> from) { Set<K> fromKeys = from.keySet(); Set<K> removedKeys = new HashSet<>(); for (K keyToRemove : keysToRemove) { if (fromKeys

How to remove multiple elements from Set/Map AND knowing which ones were removed?

﹥>﹥吖頭↗ 提交于 2020-12-02 03:31:01
问题 I have a method that has to remove any element listed in a (small) Set<K> keysToRemove from some (potentially large) Map<K,V> from . But removeAll() doesn't do, as I need to return all keys that were actually removed, since the map might or might not contain keys that require removal. Old-school code is straight forward: public Set<K> removeEntries(Map<K, V> from) { Set<K> fromKeys = from.keySet(); Set<K> removedKeys = new HashSet<>(); for (K keyToRemove : keysToRemove) { if (fromKeys

How to remove multiple elements from Set/Map AND knowing which ones were removed?

淺唱寂寞╮ 提交于 2020-12-02 03:30:54
问题 I have a method that has to remove any element listed in a (small) Set<K> keysToRemove from some (potentially large) Map<K,V> from . But removeAll() doesn't do, as I need to return all keys that were actually removed, since the map might or might not contain keys that require removal. Old-school code is straight forward: public Set<K> removeEntries(Map<K, V> from) { Set<K> fromKeys = from.keySet(); Set<K> removedKeys = new HashSet<>(); for (K keyToRemove : keysToRemove) { if (fromKeys

How to remove multiple elements from Set/Map AND knowing which ones were removed?

这一生的挚爱 提交于 2020-12-02 03:28:23
问题 I have a method that has to remove any element listed in a (small) Set<K> keysToRemove from some (potentially large) Map<K,V> from . But removeAll() doesn't do, as I need to return all keys that were actually removed, since the map might or might not contain keys that require removal. Old-school code is straight forward: public Set<K> removeEntries(Map<K, V> from) { Set<K> fromKeys = from.keySet(); Set<K> removedKeys = new HashSet<>(); for (K keyToRemove : keysToRemove) { if (fromKeys

How to log filtered values in Java Streams

泪湿孤枕 提交于 2020-12-01 09:13:00
问题 I have a requirement to log/sysout the filtered values in Java Streams. I am able to log/sysout the non-filtered value using peek() method. However, can someone please let me know how to log filtered values? For example, let's say I have a list of Person objects like this: List<Person> persons = Arrays.asList(new Person("John"), new Person("Paul")); I want to filter out those persons who are not "John" as follows: persons.stream().filter(p -> !"John".equals(p.getName())).collect(Collectors

Java 8 extract first key from matching value in a Map

别说谁变了你拦得住时间么 提交于 2020-11-30 04:36:13
问题 Suppose I have a map of given name, surname pairs and I want to find the given name of the first entry in that map that has the surname matching a certain value. How would we do this in a java 8 fashion. In my test case example below I put two ways that would do it. However the first one (looking for the given name of the first person with a surname of "Donkey") will throw java.util.NoSuchElementException: No value present so it is not safe. The second one works but it is not only harder to

Sorting collection of maps based on values [closed]

旧城冷巷雨未停 提交于 2020-11-30 02:05:30
问题 Closed. This question needs debugging details. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 days ago . Improve this question I'm trying to sort list of maps based on specific key lets say "tax" with int values in all the maps. However other keys like "id" and "price" have string and double type values respectively as shown below. List<Map<String, Object>> taxes = Arrays.asList(Map.of("id", "3", "tax",

Sorting collection of maps based on values [closed]

不打扰是莪最后的温柔 提交于 2020-11-30 01:58:04
问题 Closed. This question needs debugging details. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 days ago . Improve this question I'm trying to sort list of maps based on specific key lets say "tax" with int values in all the maps. However other keys like "id" and "price" have string and double type values respectively as shown below. List<Map<String, Object>> taxes = Arrays.asList(Map.of("id", "3", "tax",

Sorting collection of maps based on values [closed]

二次信任 提交于 2020-11-30 01:57:35
问题 Closed. This question needs debugging details. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 days ago . Improve this question I'm trying to sort list of maps based on specific key lets say "tax" with int values in all the maps. However other keys like "id" and "price" have string and double type values respectively as shown below. List<Map<String, Object>> taxes = Arrays.asList(Map.of("id", "3", "tax",