java-stream

Initializing 2d array with streams in Java

风格不统一 提交于 2019-12-07 12:41:46
问题 I have the following class: public class MyClass{ //... public MyClass(int x, int y){ //... } } Now, I need to initialize 2d-array with the items int rows; int cols; //initializing rows and cols MyClass[][] arr = new MyClass[rows][cols]; //how to initialize arr[x][y] with //new MyClass(x, y) with streams API I looked at this example but it doesn't work in my case. They use a single IntStream QUESTION: Of course I can use nested for loops, but I think it's now old-style and is considering bad.

Java groupingBy: sum multiple fields

南笙酒味 提交于 2019-12-07 12:32:18
问题 This question is an extension to the post: Java 8 groupingby with returning multiple field. For the same problem, how do you return a list of Customer ? For example, it should return: Customer("A",4500,6500) Customer("B",3000,3500) Customer("C",4000,4500) 回答1: @Pankaj Singhal's post is the right idea if you want a Map<String, Customer> as the result set +1. However, I would extract the merging logic into its own function e.g. in the Customer class you would have a function as such: public

Java 8 Streams: How to call once the Collection.stream() method and retrieve an array of several aggregate values [duplicate]

一曲冷凌霜 提交于 2019-12-07 11:57:00
问题 This question already has answers here : How to get minimum and maximum value from List of Objects using Java 8 (2 answers) Closed 3 years ago . I'm starting with the Stream API in Java 8. Here is my Person object I use: public class Person { private String firstName; private String lastName; private int age; public Person(String firstName, String lastName, int age) { this.firstName = firstName; this.lastName = lastName; this.age = age; } public String getFirstName() { return firstName; }

Java Stream: get latest version of user records

ε祈祈猫儿з 提交于 2019-12-07 11:17:06
问题 I have a list of User objects, defined as follows: public class User { private String userId; // Unique identifier private String name; private String surname; private String otherPersonalInfo; private int versionNumber; } public User(String userId, String name, String surname, String otherPersonalInfo, int version) { super(); this.name = name; this.surname = surname; this.otherPersonalInfo = otherPersonalInfo; this.version = version; } } Example list: List<User> users = Arrays.asList( new

Java collect function gives cyclic inference error

人盡茶涼 提交于 2019-12-07 11:04:00
问题 When typing the following code I get a "cyclic inference" error on the argument for the groupingBy function: Map<String, User> mapByEmail = users.stream().collect(Collectors.groupingBy(User::getEmail)); I find this confusing because the following does not cause any problem: users.stream().collect(Collectors.groupingBy(User::getEmail)); and neither does this: List<User> listByEmail = users.stream().collect(Collectors.groupingBy(User::getEmail)).values().stream().reduce(null, (a,b)-> a=b); So

Can Stream#limit return fewer elements than expected?

萝らか妹 提交于 2019-12-07 11:00:36
问题 If the Stream s below has at least n elements, what are the situations where the stream sLimit may have less than n elements, if any? Stream sLimit = s.limit(n); Reason for the question: in this answer, I read that: Despite the appearances, using limit(10) doesn't necessarily result in a SIZED stream with exactly 10 elements -- it might have fewer. 回答1: I think Holger's and Sotirios' answers are accurate, but inasmuch as I'm the guy who made the statement, I guess I should explain myself. I'm

How to merge lists of Map with Lists values using Java Streams API?

你说的曾经没有我的故事 提交于 2019-12-07 09:56:49
问题 How can I reduce the Map<X, List<String>> grouping by the X.p and join all the list values at the same time, so that I have Map<Integer, List<String>> at the end? This is what I've tried so far: class X { int p; int q; public X(int p, int q) { this.p = p; this.q = q; } } Map<X, List<String>> x = new HashMap<>(); x.put(new X(123,5), Arrays.asList("A","B")); x.put(new X(123,6), Arrays.asList("C","D")); x.put(new X(124,7), Arrays.asList("E","F")); Map<Integer, List<String>> z = x.entrySet()

Concatenate the String values of all Maps in a List

纵饮孤独 提交于 2019-12-07 09:51:13
问题 I am trying to adapt Lambda features, however few struggles here and there. List<Map<String, String>> list = new LinkedList<>(); Map<String, String> map = new HashMap<>(); map.put("data1", "12345"); map.put("data2", "45678"); list.add(map); I just want to print the values in comma separated format like 12345,45678 So here goes my trial list.stream().map(Map::values).collect(Collectors.toList()) //Collectors.joining(",") and the output is [[12345,45678]] . It means, there's a list and inside

Java 8 map each field value of a List to another similar List

只愿长相守 提交于 2019-12-07 09:49:29
I have 2 List of objects: List<User> List<UserResource> For User class: public class User { private int id; private String name; private String address; **SETTERS & GETTERS** } For UserResource class: public class UserResource { private String name; private String address; **SETTERS & GETTERS** } My List<User> has data and i want to map value in name and address to UserResource 's name and address , using java 8 stream and lambda, but i not sure how to do it. Search online for quite sometime but not able to find similar question. Something like: List<User> users = **data inserted** List

Combine allMatch, noneMatch and anyMatch on a single stream

谁说我不能喝 提交于 2019-12-07 07:30:53
问题 I would like to have the following logic: (i know it doesn't work because it consumes the stream more than once). but i am not sure how to achieve it. Stream<ByteBuffer> buffers = super.getBuffers().stream(); if (buffers.allMatch(b -> b.position() > 0)) { return OutgoingMessageStatus.FULLY_SENT; } else if (buffers.noneMatch(b -> b.position() > 0)) { return OutgoingMessageStatus.WAS_NOT_SENT; } else { return OutgoingMessageStatus.PARTIALLY_SENT; } How can I do that? 回答1: Since the result of