java-stream

Filtering keys from Map to List attribute in Java 8

谁说胖子不能爱 提交于 2019-12-07 17:58:24
问题 I have a List and a Map as below: public class student { private String name; private String age; private String id; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getId() { return id; } public void setId(String id) { this.id = id; } student(String id,String name,String age) { } } List<student> stulist = Arrays.asList(new student("1", "vishwa

How to get the count of keys for values in a hash map using lambda

喜欢而已 提交于 2019-12-07 17:50:50
问题 I have a hash map Map<Integer, List<String>> Directmap = new HashMap<Integer, List<String>>() {{ put(0, Arrays.asList(a, b)); put(1, Arrays.asList(b, c)); put(2, Arrays.asList(d)); put(3, Arrays.asList(d, e)); put(4, Arrays.asList(e)); put(5, Arrays.asList()); }}; Directmap: {0=[a, b], 1=[b, c], 2=[d], 3=[d, e], 4=[e], 5=[]} I want to count the number of keys for each value. For example: "a" has one key, "b" has two keys, ..., "e" has two keys namely 3 and 4. I tried something like this: Map

How to use non-final variable inside Java8 streams/filters?

我只是一个虾纸丫 提交于 2019-12-07 17:32:19
问题 In my use case I would like to update the value of a variable and reference the same in next iteration in streams. But the java compiler is giving me error. Here is my code static String convertList( List<Map.Entry<String, String>> map, String delimiter, long maxLength ) { long currentLength = 0L; return map.stream() .map(e->e.getKey() + "=" + e.getValue()) .filter(p->{ long pLength = p.getBytes(StandardCharsets.UTF_8).length; currentLength = currentLength + pLength; if (currentLength <=

Divide LongStream into substreams with maximal length

依然范特西╮ 提交于 2019-12-07 16:53:53
问题 There are some SQL statements in my program that contain IN -clauses with given Ids. The problem is that in some cases there might be more than 1000 Ids which causes Oracle to crash with ORA-01795. Too many items. So I want to divide this list into multiple sub-lists. Example: I have 2403 Ids Result would be three lists: 0 - 999 1000 - 1999 2000 - 2402 I have written a piece of code that works, but looks terrible. Is there any better solution for this problem? Maybe something with Collectors

Why doesn't sorted(Comparator::reverseOrder) work?

故事扮演 提交于 2019-12-07 16:48:16
问题 The below Stream expression works perfectly fine: Stream<String> s = Stream.of("yellow","blue", "white"); s.sorted(Comparator.reverseOrder()) .forEach(System.out::print);` //yellowwhiteblue Why doesn't the equivalent one with method references compile? s.sorted(Comparator::reverseOrder).forEach(System.out::print); The type Comparator does not define reverseOrder(String, String) that is applicable here 回答1: A method reference is telling Java "treat this method as the implementation of a single

Why does IntStream.range(0, 100000).parallel.foreach take longer then normal for loop

拟墨画扇 提交于 2019-12-07 15:49:27
I am just starting to learn about the Streams and parallel in Java and I was wondering why a normal for loop takes less time than IntStream paralleled at adding items to an array. package parallel; import java.util.stream.IntStream; public class Parallel { public static void main(String[] args) { final int[] intArray = new int[100000]; long startTime = System.currentTimeMillis(); IntStream.range(0, 100000).parallel().forEach(i -> intArray[i]=i); long endTime = System.currentTimeMillis(); System.out.println("Parallel time: " + (endTime-startTime)); final int[] intArray2 = new int[100000]; try {

Is there a way to check whether a stream is finite in Java? [closed]

蹲街弑〆低调 提交于 2019-12-07 15:27:17
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed last year . I know there is infinite stream in Java. Is there a way to check whether the stream is finite or not? Something like this method isStreamFinite(Stream<T> stream) ? @Test public void testStreamFinity() { assertFalse(isStreamFinite(generateLong())); } private <T> boolean isStreamFinite(Stream<T> stream

Flattening a List of List to a List with Java 8 stream API

谁说我不能喝 提交于 2019-12-07 14:37:05
问题 I have the following code which could be much simpler using Java 8 stream API: List<List<String>> listOfListValues; public List<String> getAsFlattenedList() { List<String> listOfValues= new ArrayList<>(); for (List<String> value: listOfListValues) { listOfValues.add(String.valueOf(value)); } return listOfValues; } I searched for a solution on SO and found this: listOfListValues.stream() .flatMap(List::stream) .collect(Collectors.toList()); But this doesn't do the same what I want. 回答1: You

Java 8 Collection and stream/forEach

て烟熏妆下的殇ゞ 提交于 2019-12-07 13:30:35
问题 Is there any reason to specifically insert a stream/parallel stream before a forEach call when using a Collection? Example: Collection<Object> foo; foo.forEach(); // Goes through every item in foo foo.stream().forEach(); // Does stream make a difference here foo.parallelStream().forEach(); // Does this make a difference here? Thanks 回答1: foo.forEach(); // Goes through every item in foo foo.stream().forEach(); // Does stream make a difference here It is useless unless you need stream

Counting and order with Java 8 Stream API

梦想的初衷 提交于 2019-12-07 12:44:49
问题 I wonder how could this be ordered by COUNT then ASC. Stream<String> fruits = Stream.of("apple", "orange", "ananas"); Map<String, Long> letters = fruits.map(w -> w.split("")) .flatMap(Arrays::stream) .collect(groupingBy(identity(), counting())); Output: {p=2, a=5, r=1, s=1, e=2, g=1, l=1, n=3, o=1}` Desired output: {a=5, n=3, e=2, p=2, g=1, l=1, r=1, s=1, o=1} 回答1: It’s unavoidable to do it in two mapping steps as you need the counts first, before you can sort according to the counts: Map