java-stream

Printing certain values using Collections.frequency()

北城以北 提交于 2020-01-23 06:34:39
问题 I have an array as follows: int[] array = {11, 14, 17, 11, 48, 33, 29, 11, 17, 22, 11, 48, 18}; What I wanted to do was to find the duplicate values, and print them. So my way of doing this was to convert to ArrayList , then to Set and use a stream on the Set . ArrayList<Integer> list = new ArrayList<>(array.length); for (int i = 0; i < array.length; i++) { list.add(array[i]); } Set<Integer> dup = new HashSet<>(list); I then used a stream to loop through it and print the values using

Printing certain values using Collections.frequency()

[亡魂溺海] 提交于 2020-01-23 06:34:09
问题 I have an array as follows: int[] array = {11, 14, 17, 11, 48, 33, 29, 11, 17, 22, 11, 48, 18}; What I wanted to do was to find the duplicate values, and print them. So my way of doing this was to convert to ArrayList , then to Set and use a stream on the Set . ArrayList<Integer> list = new ArrayList<>(array.length); for (int i = 0; i < array.length; i++) { list.add(array[i]); } Set<Integer> dup = new HashSet<>(list); I then used a stream to loop through it and print the values using

Java, in which thread are sequential streams executed?

ε祈祈猫儿з 提交于 2020-01-23 05:22:40
问题 While reading the documentation about streams, I came across the following sentences: ... attempting to access mutable state from behavioral parameters presents you with a bad choice ... if you do not synchronize access to that state, you have a data race and therefore your code is broken ... [1] If the behavioral parameters do have side-effects ... [there are no] guarantees that different operations on the "same" element within the same stream pipeline are executed in the same thread. [2]

Why does stream parallel() not use all available threads?

早过忘川 提交于 2020-01-23 02:52:27
问题 I tried to run 100 Sleep tasks in parallel using Java8(1.8.0_172) stream.parallel() submitted inside a custom ForkJoinPool with 100+ threads available. Each task would sleep for 1s. I expected the whole work would finish after ~1s, given the 100 sleeps could be done in parallel. However I observe a runtime of 7s. @Test public void testParallelStream() throws Exception { final int REQUESTS = 100; ForkJoinPool forkJoinPool = null; try { // new ForkJoinPool(256): same results for all tried

Java 8 Split String and Create Map inside Map

耗尽温柔 提交于 2020-01-22 19:51:08
问题 I have a String like 101-1-5,101-2-4,102-1-5,102-2-5,102-3-5,103-1-4 . I want to add this in to a Map<String, Map<String, String>> . Like: {101={1=5, 2=4}, 102={1=5, 2=5, 3=5}, 103={1=4}} How to do this using Java 8 stream? I tried using normal Java. And it works fine. public class Util { public static void main(String[] args) { String samp = "101-1-5,101-2-4,102-1-5,102-2-5,102-3-5,103-1-4"; Map<String, Map<String, String>> m1 = new HashMap<>(); Map<String, String> m2 = null; String[] items

Stream ordered/unordered problems

夙愿已清 提交于 2020-01-22 14:15:30
问题 I have the following code: Set<Integer> l = new TreeSet<>(); l.add(1); l.add(10); l.add(3); l.add(-3); l.add(-4); and I want to unorder the collection with: l.stream().unordered().forEach(System.out::println); but the forEach returns always the collection ordered! Then I have also another doubt about this sentence: For sequential streams, the presence or absence of an encounter order does not affect performance, only determinism. If a stream is ordered, repeated execution of identical stream

Stream ordered/unordered problems

我是研究僧i 提交于 2020-01-22 14:15:12
问题 I have the following code: Set<Integer> l = new TreeSet<>(); l.add(1); l.add(10); l.add(3); l.add(-3); l.add(-4); and I want to unorder the collection with: l.stream().unordered().forEach(System.out::println); but the forEach returns always the collection ordered! Then I have also another doubt about this sentence: For sequential streams, the presence or absence of an encounter order does not affect performance, only determinism. If a stream is ordered, repeated execution of identical stream

Stream groupingBy a list of enum types

不问归期 提交于 2020-01-21 17:59:44
问题 I have a Product class: class Product { String name; List<Group> group; //more fields, getters, setters public Product(String name, Group... group) { this.name = name; this.group = Arrays.asList(group); } } where Group is an enum public enum Group { LEISURE, SPORT, FORMALATTIRE, BABY, MATERNITY //... } From a list of products I want to create a Map<Group,List<Product>> Example input: List<Product> productList = new ArrayList<>(); productList.add(new Product("A", Group.BABY, Group.MATERNITY));

Stream groupingBy a list of enum types

你说的曾经没有我的故事 提交于 2020-01-21 17:53:07
问题 I have a Product class: class Product { String name; List<Group> group; //more fields, getters, setters public Product(String name, Group... group) { this.name = name; this.group = Arrays.asList(group); } } where Group is an enum public enum Group { LEISURE, SPORT, FORMALATTIRE, BABY, MATERNITY //... } From a list of products I want to create a Map<Group,List<Product>> Example input: List<Product> productList = new ArrayList<>(); productList.add(new Product("A", Group.BABY, Group.MATERNITY));

Stream groupingBy a list of enum types

依然范特西╮ 提交于 2020-01-21 17:52:12
问题 I have a Product class: class Product { String name; List<Group> group; //more fields, getters, setters public Product(String name, Group... group) { this.name = name; this.group = Arrays.asList(group); } } where Group is an enum public enum Group { LEISURE, SPORT, FORMALATTIRE, BABY, MATERNITY //... } From a list of products I want to create a Map<Group,List<Product>> Example input: List<Product> productList = new ArrayList<>(); productList.add(new Product("A", Group.BABY, Group.MATERNITY));