java-8

Java 8 Stream to find element in list

依然范特西╮ 提交于 2020-12-29 05:51:09
问题 I have the following class: public class Item { int id; String name; // few other fields, contructor, getters and setters } I have a list of Items. I want to iterate through the list and find the instance which has a particular id. I'm trying to do it through streams. public void foobar() { List<Item> items = getItemList(); List<Integer> ids = getIdsToLookup(); int id, i = ids.size() - 1; while (i >= 0) { id = ids.get(i); Optional<Item> item = items .stream() .filter(a -> a.getId() == id)

In Java, what is the simplest way to create an SSLContext with just a PEM file?

落花浮王杯 提交于 2020-12-29 05:30:32
问题 I used LetsEncrypt's CertBot to generate PEM files for free. In other languages it is easy to start an HTTPS server using just a couple lines of code and the PEM/key files. The solutions I have found so far in java are overly complex and I'm looking for something simpler. I do not want to use java's command-line "keytool". I just want to drag and drop my PEM/key files into my eclipse, and programatically start up an HTTPS server using an SSLContext. I do not want to include massive external

In Java, what is the simplest way to create an SSLContext with just a PEM file?

我的未来我决定 提交于 2020-12-29 05:26:01
问题 I used LetsEncrypt's CertBot to generate PEM files for free. In other languages it is easy to start an HTTPS server using just a couple lines of code and the PEM/key files. The solutions I have found so far in java are overly complex and I'm looking for something simpler. I do not want to use java's command-line "keytool". I just want to drag and drop my PEM/key files into my eclipse, and programatically start up an HTTPS server using an SSLContext. I do not want to include massive external

Equivalent of Scala's foldLeft in Java 8

北城以北 提交于 2020-12-28 06:42:56
问题 What is the equivalent of of Scala's great foldLeft in Java 8? I was tempted to think it was reduce , but reduce has to return something of identical type to what it reduces on. Example: import java.util.List; public class Foo { // this method works pretty well public int sum(List<Integer> numbers) { return numbers.stream() .reduce(0, (acc, n) -> (acc + n)); } // this method makes the file not compile public String concatenate(List<Character> chars) { return chars.stream() .reduce(new

Equivalent of Scala's foldLeft in Java 8

江枫思渺然 提交于 2020-12-28 06:41:10
问题 What is the equivalent of of Scala's great foldLeft in Java 8? I was tempted to think it was reduce , but reduce has to return something of identical type to what it reduces on. Example: import java.util.List; public class Foo { // this method works pretty well public int sum(List<Integer> numbers) { return numbers.stream() .reduce(0, (acc, n) -> (acc + n)); } // this method makes the file not compile public String concatenate(List<Character> chars) { return chars.stream() .reduce(new

Equivalent of Scala's foldLeft in Java 8

旧时模样 提交于 2020-12-28 06:40:19
问题 What is the equivalent of of Scala's great foldLeft in Java 8? I was tempted to think it was reduce , but reduce has to return something of identical type to what it reduces on. Example: import java.util.List; public class Foo { // this method works pretty well public int sum(List<Integer> numbers) { return numbers.stream() .reduce(0, (acc, n) -> (acc + n)); } // this method makes the file not compile public String concatenate(List<Character> chars) { return chars.stream() .reduce(new

Equivalent of Scala's foldLeft in Java 8

我们两清 提交于 2020-12-28 06:39:53
问题 What is the equivalent of of Scala's great foldLeft in Java 8? I was tempted to think it was reduce , but reduce has to return something of identical type to what it reduces on. Example: import java.util.List; public class Foo { // this method works pretty well public int sum(List<Integer> numbers) { return numbers.stream() .reduce(0, (acc, n) -> (acc + n)); } // this method makes the file not compile public String concatenate(List<Character> chars) { return chars.stream() .reduce(new

Equivalent of Scala's foldLeft in Java 8

回眸只為那壹抹淺笑 提交于 2020-12-28 06:38:07
问题 What is the equivalent of of Scala's great foldLeft in Java 8? I was tempted to think it was reduce , but reduce has to return something of identical type to what it reduces on. Example: import java.util.List; public class Foo { // this method works pretty well public int sum(List<Integer> numbers) { return numbers.stream() .reduce(0, (acc, n) -> (acc + n)); } // this method makes the file not compile public String concatenate(List<Character> chars) { return chars.stream() .reduce(new

Scala Futures and java 8 CompletableFuture

岁酱吖の 提交于 2020-12-27 17:07:47
问题 The introduction of CompletableFutures in Java 8 brought to the language features available in the scala.concurrent.Future such as monadic transformations. What are the differences, and why a Scala developer should prefer Scala Futures over java 8 CompletableFuture ? Are there still good reasons to use the scala.concurrent.Future in Java through akka.dispatch bridge? 回答1: What are the differences, and why a Scala developer should prefer Scala Futures over java 8 CompletableFuture ? Rephrasing

Java how to use stream map to return boolean [duplicate]

强颜欢笑 提交于 2020-12-26 06:45:06
问题 This question already has an answer here : Java8 Effectively Final compile time error on non final variable (1 answer) Closed 3 years ago . I am trying to return a boolean for the result. public boolean status(List<String> myArray) { boolean statusOk = false; myArray.stream().forEach(item -> { helpFunction(item).map ( x -> { statusOk = x.status(); // x.status() returns a boolean if (x.status()) { return true; } return false; }); }); } It's complaining variable used in lambda expression should