lambda

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

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

Python lambda with if but without else

和自甴很熟 提交于 2020-11-30 02:43:06
问题 I was writing some lambda functions and couldn't figure this out. Is there a way to have something like lambda x: x if (x<3) in python? As lambda a,b: a if (a > b) else b works ok. So far lambda x: x < 3 and x or None seems to be the closest i have found. 回答1: A lambda, like any function, must have a return value. lambda x: x if (x<3) does not work because it does not specify what to return if not x<3 . By default functions return None , so you could do lambda x: x if (x<3) else None But

Cannot use ref or out parameter inside an anonymous method [duplicate]

。_饼干妹妹 提交于 2020-11-29 08:21:05
问题 This question already has answers here : C# Cannot use ref or out parameter inside an anonymous method body (3 answers) Closed 5 years ago . I have a problem with my code in c# if someone could help resolve my problem. In a function I am parsing a Xml file and saving it to a struct. Then I try to retreive some info from said struct with a specific node id and my code fails with "Cannot use ref or out parameter 'c' inside an anonymous method, lambda expression, or query expression" Here is my

Cannot use ref or out parameter inside an anonymous method [duplicate]

岁酱吖の 提交于 2020-11-29 08:18:53
问题 This question already has answers here : C# Cannot use ref or out parameter inside an anonymous method body (3 answers) Closed 5 years ago . I have a problem with my code in c# if someone could help resolve my problem. In a function I am parsing a Xml file and saving it to a struct. Then I try to retreive some info from said struct with a specific node id and my code fails with "Cannot use ref or out parameter 'c' inside an anonymous method, lambda expression, or query expression" Here is my

Cannot use ref or out parameter inside an anonymous method [duplicate]

房东的猫 提交于 2020-11-29 08:18:35
问题 This question already has answers here : C# Cannot use ref or out parameter inside an anonymous method body (3 answers) Closed 5 years ago . I have a problem with my code in c# if someone could help resolve my problem. In a function I am parsing a Xml file and saving it to a struct. Then I try to retreive some info from said struct with a specific node id and my code fails with "Cannot use ref or out parameter 'c' inside an anonymous method, lambda expression, or query expression" Here is my

Why lambda inside map is not running?

*爱你&永不变心* 提交于 2020-11-28 08:57:47
问题 I am trying to learn concurrency and lambdas in java 8. But my code is not entering lambda block inside map. List<Book> bookList = new ArrayList<Book>(); isbnList .stream() .map(isbn -> (CompletableFuture.supplyAsync( () -> { try { List<String> pageContents = getUrlContents(webLink + isbn); return new Book( parseBookTitle(pageContents), isbn, parseRank(pageContents) ); } catch (IOException ex) { return null; } })).thenApply(a -> bookList.add(a)) ); While debugging, code is exiting at .map

Why lambda inside map is not running?

≡放荡痞女 提交于 2020-11-28 08:57:25
问题 I am trying to learn concurrency and lambdas in java 8. But my code is not entering lambda block inside map. List<Book> bookList = new ArrayList<Book>(); isbnList .stream() .map(isbn -> (CompletableFuture.supplyAsync( () -> { try { List<String> pageContents = getUrlContents(webLink + isbn); return new Book( parseBookTitle(pageContents), isbn, parseRank(pageContents) ); } catch (IOException ex) { return null; } })).thenApply(a -> bookList.add(a)) ); While debugging, code is exiting at .map

Mapping a list to Map Java 8 stream and groupingBy

不羁岁月 提交于 2020-11-27 04:56:35
问题 I have this simple Bean class: public class Book { public Book(Map<String, String> attribute) { super(); this.attribute = attribute; } //key is isbn, val is author private Map<String, String> attribute; public Map<String, String> getAttribute() { return attribute; } public void setAttribute(Map<String, String> attribute) { this.attribute = attribute; } } In my main class, I have added some information to the List: Map<String, String> book1Details = new HashMap<String, String>(); book1Details

Mapping a list to Map Java 8 stream and groupingBy

岁酱吖の 提交于 2020-11-27 04:56:31
问题 I have this simple Bean class: public class Book { public Book(Map<String, String> attribute) { super(); this.attribute = attribute; } //key is isbn, val is author private Map<String, String> attribute; public Map<String, String> getAttribute() { return attribute; } public void setAttribute(Map<String, String> attribute) { this.attribute = attribute; } } In my main class, I have added some information to the List: Map<String, String> book1Details = new HashMap<String, String>(); book1Details