java-stream

How to collect result of a stream into an array of custom object in Java 8 [duplicate]

泄露秘密 提交于 2020-01-06 08:38:10
问题 This question already has answers here : How to convert a Java 8 Stream to an Array? (9 answers) Java stream toArray() convert to a specific type of array (2 answers) Closed 8 months ago . I have an List<TestBuilder> testBuilders; Test has a function build of type Test I did testBuilders.stream().map(Test::build()).collect() I want to collect above in array of Test i.e Test[] I am not sure what would go in collect function 回答1: Use the terminal operation Stream::toArray which packs the

For loop to add custom objects to arraylist for n times - Java8

本小妞迷上赌 提交于 2020-01-06 08:12:41
问题 We have an old-style for loop to add custom objects to ArrayList. public List<Object> generateList() { List<Object> list = new ArrayList<Object>(); for (int i = 0; i < 10; i++) { list.add(new Manager(50000, 10, "Finance Manager")); list.add(new Employee(30000, 5, "Accounts")); } return list; } Is there any way to do this by using java8? I tried to use Stream.generate(MyClass::new).limit(10); but, I am not getting the right way in java8 to achieve the above functionality. Any suggestions,

For loop to add custom objects to arraylist for n times - Java8

风流意气都作罢 提交于 2020-01-06 08:12:26
问题 We have an old-style for loop to add custom objects to ArrayList. public List<Object> generateList() { List<Object> list = new ArrayList<Object>(); for (int i = 0; i < 10; i++) { list.add(new Manager(50000, 10, "Finance Manager")); list.add(new Employee(30000, 5, "Accounts")); } return list; } Is there any way to do this by using java8? I tried to use Stream.generate(MyClass::new).limit(10); but, I am not getting the right way in java8 to achieve the above functionality. Any suggestions,

Recursively populate JTree, using list of list

僤鯓⒐⒋嵵緔 提交于 2020-01-06 07:11:56
问题 this question has the reverse process in get nested List of CustomObject from JTree, including leaf I want to implement a method optimization of process, I reduced this process simple (multiplication and division) like a Tree shown. The Bottom and Bold Rectangles (the online tool don't let me change the color) represent the total multiplication between N Numerators and D Denominators of each list. Now, I was creating list of list in Java, in order to calculate the total cost of each list

How to use Java 8 Streams groupingBy to map multiple database records to a single object with a List<String> property for one column

半世苍凉 提交于 2020-01-06 01:53:56
问题 My problem essentially comes down to this simplified example. I have data coming back from a DB which has some duplicate information in the rows. In this example I have a list of TeamRow objects that come back from the DB. I can easily group these using Collectors.groupingBy : public class TeamRow { private int id; private String name; private String player; public TeamRow(int id, String name, String player) { this.id = id; this.name = name; this.player = player; } public int getId() {return

Extracting db column data with filter and map in Java8

ⅰ亾dé卋堺 提交于 2020-01-05 04:12:12
问题 A table about employees has following data: employee Id, employee name, their manager's Id and their team members' Id. Each employee would have multiple entries based on their team size. Say an employee with 5 other members in their team would have five rows with the employee and manager id repeated each with one team member's id. Sample table: employeeId | employeeName | managerId | teamEmployeeId | ------------------------------------------------------- 1000 | Alex | 4000 | 1101 | 1200 |

Transform Java Stream and return reduced values both

此生再无相见时 提交于 2020-01-04 07:35:08
问题 Assuming I consume a Stream of entities from a source which I do not want to materialize, and I want to both transform the elements, and return some globally reduced value, what is the idiomatic way with java(8)? This is essentially trying to perform both a reduce() and a collect() . Example: class Person { public String firstname, public String lastname, public int age; } class TeamSummary { public List<String> fullnames, // firstname and lastname of all public Person oldest } public

guava's Streams::findLast implementation

孤街醉人 提交于 2020-01-03 19:58:51
问题 I am looking into the implementation of Streams::findLast from guava and while trying to understand it, there were a couple of things that simply I could not grasp. Here is it's implementation: public static <T> java.util.Optional<T> findLast(Stream<T> stream) { class OptionalState { boolean set = false; T value = null; void set(@Nullable T value) { set = true; this.value = value; } T get() { checkState(set); return value; } } OptionalState state = new OptionalState(); Deque<Spliterator<T>>

How to interconnect non-parallel stream with parallel stream(one producer multiple consumers)

 ̄綄美尐妖づ 提交于 2020-01-03 19:31:55
问题 I am trying to create one producer multiple consumers model with streams in Java8. I am reading and processing data from DB resource and I want to process them in streaming fashion way(can not read the whole resource into memory). The reading of the source has to be single threaded (the cursors is not thread safe) and reading is fast, than the processing of each data chunks which is heavy operation can run in parallel. I haven't found out how can I join (interconnect) non-parallel stream with

How to interconnect non-parallel stream with parallel stream(one producer multiple consumers)

流过昼夜 提交于 2020-01-03 19:31:34
问题 I am trying to create one producer multiple consumers model with streams in Java8. I am reading and processing data from DB resource and I want to process them in streaming fashion way(can not read the whole resource into memory). The reading of the source has to be single threaded (the cursors is not thread safe) and reading is fast, than the processing of each data chunks which is heavy operation can run in parallel. I haven't found out how can I join (interconnect) non-parallel stream with