java-stream

How do I deal with checked exceptions in lambda? [duplicate]

允我心安 提交于 2021-02-16 15:36:08
问题 This question already has answers here : Java 8 Lambda function that throws exception? (26 answers) Closed 4 years ago . I have the following code snippet. package web_xtra_klasa.utils; import java.util.Arrays; import java.util.Properties; import java.util.function.Function; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class

Convert List<Person> to Map<Integer, List<Integer>> using Lambdas

可紊 提交于 2021-02-16 14:39:25
问题 I wanted to convert list to Map as below. Here is the example. I have student list something like below code snippet. Get a Hasmap out of it with Key as Integer which is Age and value as List. From the below input feed, My response should be something like below. How do I achieve this ? Map[[10, {1}], [20, {2,3,4}], [30,{5}]. [40,{6}]]; private static List<Person> getPersonTestData() { List<Person> personList = new ArrayList<>(); personList.add(Person.of(1, "First1", "Last1", 10)); personList

Combine keys and values of a HashMap to a Set

牧云@^-^@ 提交于 2021-02-16 14:19:27
问题 I have a HashMap<Integer, Integer> , There can be duplicate values for the unique keys. Is there a way to convert the HashMap to a Set<Integer> which contains unique Integers of both the keys and values. This can definitely be done in two loops by iterating over the keySet() and .values(). I'm would like to know whether this is possible in java 8 streams. 回答1: You can use the stream function to combine both the values and the keys: Map<Integer, Integer> map = ... Set<Integer> total = Stream

Java: Consumer interface in a stream doesn't work as expected [duplicate]

牧云@^-^@ 提交于 2021-02-16 14:13:31
问题 This question already has answers here : Java 8 Streams peek api (4 answers) How to use Streams api peek() function and make it work? (2 answers) Closed 2 years ago . I've got 2 statements, I expected that they should "print" same result: Arrays.stream("abc".split("")).forEach(System.out::println);//first Arrays.stream("abc".split("")).peek(new Consumer<String>() {//second @Override public void accept(String s) { System.out.println(s);//breakpoint } }); In fact, the first statement will print

Java: Consumer interface in a stream doesn't work as expected [duplicate]

荒凉一梦 提交于 2021-02-16 14:11:51
问题 This question already has answers here : Java 8 Streams peek api (4 answers) How to use Streams api peek() function and make it work? (2 answers) Closed 2 years ago . I've got 2 statements, I expected that they should "print" same result: Arrays.stream("abc".split("")).forEach(System.out::println);//first Arrays.stream("abc".split("")).peek(new Consumer<String>() {//second @Override public void accept(String s) { System.out.println(s);//breakpoint } }); In fact, the first statement will print

Transform a List<Object> to a Map<String, Integer> such that the String is not a duplicate value using Java 8 Streams

假如想象 提交于 2021-02-16 13:08:23
问题 We have a Student class as follows: class Student { private int marks; private String studentName; public int getMarks() { return marks; } public void setMarks(int marks) { this.marks = marks; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public Student(String studentName, int marks) { this.marks = marks; this.studentName = studentName; } } We have a LIST of Students as follows : List<Student>

Transform a List<Object> to a Map<String, Integer> such that the String is not a duplicate value using Java 8 Streams

别说谁变了你拦得住时间么 提交于 2021-02-16 13:04:10
问题 We have a Student class as follows: class Student { private int marks; private String studentName; public int getMarks() { return marks; } public void setMarks(int marks) { this.marks = marks; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public Student(String studentName, int marks) { this.marks = marks; this.studentName = studentName; } } We have a LIST of Students as follows : List<Student>

Transform a List<Object> to a Map<String, Integer> such that the String is not a duplicate value using Java 8 Streams

坚强是说给别人听的谎言 提交于 2021-02-16 13:03:10
问题 We have a Student class as follows: class Student { private int marks; private String studentName; public int getMarks() { return marks; } public void setMarks(int marks) { this.marks = marks; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public Student(String studentName, int marks) { this.marks = marks; this.studentName = studentName; } } We have a LIST of Students as follows : List<Student>

Unhandled IOException Inside Java List.stream.forEach Lambda Expression

谁都会走 提交于 2021-02-16 09:10:17
问题 I am using the Stream API introduced in Java 8 to run a method for each string in a list. public boolean initFile() throws IOException { if (this.outFile.exists()) { this.outFile.delete(); } return this.outFile.createNewFile(); } public void writeStringToFile(String str, boolean addNewLine) throws IOException { if (this.mode != FileMode.READ) { if (this.initFile()) { FileWriter fileWriter; if (this.mode == FileMode.APPEND) fileWriter = new FileWriter(outFile.getAbsolutePath(), true); else

Unhandled IOException Inside Java List.stream.forEach Lambda Expression

回眸只為那壹抹淺笑 提交于 2021-02-16 09:01:20
问题 I am using the Stream API introduced in Java 8 to run a method for each string in a list. public boolean initFile() throws IOException { if (this.outFile.exists()) { this.outFile.delete(); } return this.outFile.createNewFile(); } public void writeStringToFile(String str, boolean addNewLine) throws IOException { if (this.mode != FileMode.READ) { if (this.initFile()) { FileWriter fileWriter; if (this.mode == FileMode.APPEND) fileWriter = new FileWriter(outFile.getAbsolutePath(), true); else