java-stream

Java: making List from primitive array using Stream API

心不动则不痛 提交于 2019-12-30 10:51:57
问题 I'm trying to make a List from a primitive array int[] values={4,5,2,3,42,60,20}; List<Integer> greaterThan4 = Arrays.stream(values) .filter(value -> value > 4) .collect(Collectors.toList()); But the last function collect gives me an error because it wants other arguments. It wants 3 arguments Supplier, ObjIntConsumer and BiConsumer. I don't understand why it wants 3 arguments when I have seen different examples that just use collect(Collectors.toList()); and get the list. What I'm doing

Java: making List from primitive array using Stream API

守給你的承諾、 提交于 2019-12-30 10:51:53
问题 I'm trying to make a List from a primitive array int[] values={4,5,2,3,42,60,20}; List<Integer> greaterThan4 = Arrays.stream(values) .filter(value -> value > 4) .collect(Collectors.toList()); But the last function collect gives me an error because it wants other arguments. It wants 3 arguments Supplier, ObjIntConsumer and BiConsumer. I don't understand why it wants 3 arguments when I have seen different examples that just use collect(Collectors.toList()); and get the list. What I'm doing

How to get all the distinct values by key inside stream java8

别等时光非礼了梦想. 提交于 2019-12-30 10:49:10
问题 I am currently learning a bit about streams. I have the following JSONArray, and I want to be able to retrieve all the distinct xvalues. datasets: { ds1: { xvalues: [ "(empty)", "x1", "x2" ] }, ds2: { xvalues: [ "(empty)", "x1", "x2", "x3" ] } } I am trying the following code but it doesn't seem quite right.... List<String> xvalues = arrayToStream(datasets) .map(JSONObject.class::cast) .map(dataset -> { try { return dataset.getJSONArray("xvalues"); } catch (JSONException ex) { } return; })

Merging lists under same objects in a list using Java streams

孤街醉人 提交于 2019-12-30 10:45:10
问题 I have two objects like following: public class A { private Integer id; private String name; private List<B> list; public A(Integer id, String name, List<B> list) { this.id = id; this.name = name; this.list = list; } //getters and setters } and public class B { private Integer id; private String name; public B(Integer id, String name) { this.id = id; this.name = name; } //getters and setters } So, A holds a list of B and there is a list of A populated as follows: List<A> list = new ArrayList<

How to sort an ArrayList with object using stream().sorted()

断了今生、忘了曾经 提交于 2019-12-30 09:56:47
问题 I am having real trouble with using stream and sorted to sort my ArrayList and hope someone can help out. The code uses Croatian words, but I don't think that will be a problem for someone who understands what I mean. This is the ArrayList ArrayList<Publikacija> listaPublikacija = new ArrayList<>(); listaPublikacija.add(prvaKnjiga); listaPublikacija.add(drugaKnjiga); listaPublikacija.add(prviCasopis); listaPublikacija.add(drugiCasopis); In my assignment I am supposed to sort these objects

Partition java streams in categories [duplicate]

假装没事ソ 提交于 2019-12-30 09:28:10
问题 This question already has answers here : Java 8 List<V> into Map<K, V> (21 answers) Closed 2 years ago . I have a stream<A> , where class A { String category(); // ... } I would like to get a map<String, list<A>> , where the original stream is partitioned into sublists based on the value of category(). It is pretty trivial to have it implemented using a for loop, but is it possible to get a more elegant solution harnessing java streams? EXAMPLE: Given {[a, xyz], [a, zyx], [b, abc]} , I would

Java Streams | groupingBy same elements

家住魔仙堡 提交于 2019-12-30 09:05:11
问题 I have a stream of words and I would like to sort them according to the occurrence of same elements (=words). e.g.: {hello, world, hello} to Map<String, List<String>> hello, {hello, hello} world, {world} What i have so far: Map<Object, List<String>> list = streamofWords.collect(Collectors.groupingBy(???)); Problem 1: The stream seems to lose the information that he is processing Strings, therefore the compiler forces me to change the type to Object, List Problem 2: I don't know what to put

what is the difference between a stateful and a stateless lambda expression?

余生长醉 提交于 2019-12-30 08:34:30
问题 According to the OCP book one must avoid stateful operations otherwise known as stateful lambda expression. The definition provided in the book is 'a stateful lambda expression is one whose result depends on any state that might change during the execution of a pipeline.' They provide an example where a parallel stream is used to add a fixed collection of numbers to a synchronized ArrayList using the .map() function. The order in the arraylist is completely random and this should make one see

How do I sort a List of TreeSets with java8 streams

≡放荡痞女 提交于 2019-12-30 08:28:39
问题 My list contains sets like [1,3,5][2,6,4] etc, all of the same size. I tried doing this but it doesn't seem to work. List<TreeSet<T>> block; for(TreeSet<T> t : block){ block.stream().sorted((n,m)->n.compareTo(m)).collect(Collectors.toSet()); } The end result I want is [1,2,3][4,5,6] . I could try to add all the elements in an ArrayList and sort that out then make a new List of TreeSet 's. But is there is some kind of one liner? UPDATE: List<T> list=new ArrayList<T>(); for(TreeSet<T> t : block

Do terminal operations on streams close the source? [duplicate]

吃可爱长大的小学妹 提交于 2019-12-30 08:03:15
问题 This question already has answers here : Do terminal operations close the stream? (2 answers) Closed 5 years ago . Consider the following code: Path directory = Paths.get(/* some directory */); Files.list(directory).forEach(System.out::println); Does a terminal operation (like forEach ) close the underlying file that has been opened? Refer to the relevant parts of the javadoc of Files.list: The returned stream encapsulates a DirectoryStream. If timely disposal of file system resources is