java-stream

Getting indexes of maximum number in array

心不动则不痛 提交于 2019-12-04 06:39:29
I have an array containing numbers which are ranks. Something like this : 0 4 2 0 1 0 4 2 0 4 0 2 Here 0 corresponds to the lowest rank and max number corresponds to highest rank. There may be multiple indexes containing highest rank. I want to find index of all those highest rank in array. I have achieved with following code: import java.util.*; class Index{ public static void main(String[] args){ int[] data = {0,4,2,0,1,0,4,2,0,4,0,2}; int max = Arrays.stream(data).max().getAsInt(); ArrayList<Integer> indexes = new ArrayList<Integer>(); for(int i=0;i<12;i++){ if(data[i]==max){ indexes.add(i)

How can I reverse one single string in Java 8 using Lambda and Streams?

六月ゝ 毕业季﹏ 提交于 2019-12-04 06:36:01
I have one string say "Aniruddh" and I want to reverse it using lambdas and streams in Java 8. How can I do it? Given a string like String str = "Aniruddh"; the canonical solution is String reversed = new StringBuilder(str).reverse().toString(); If, perhaps for educational purposes, you want to solve this by streaming over the string’s characters, you can do it like String reversed = str.chars() .mapToObj(c -> (char)c) .reduce("", (s,c) -> c+s, (s1,s2) -> s2+s1); This is not only much more complicated, it also has lots of performance drawbacks. The following solution eliminates boxing related

How do I lazily concatenate streams?

穿精又带淫゛_ 提交于 2019-12-04 06:21:49
I'm trying to implement a stream that uses another instance of itself in its implementation. The stream has a few constant elements prepended (with IntStream.concat) to it, so this should work as long as the concatenated stream creates the non-constant part lazily. I think using the StreamSupport.intStream overload taking a Supplier with IntStream.concat (which "creates a lazily concatenated stream" ) should be lazy enough to only create the second spliterator when elements are demanded from it, but even creating the stream (not evaluating it) overflows the stack. How can I lazily concatenate

Collect all objects from a Set of Sets with Java Stream

安稳与你 提交于 2019-12-04 06:20:59
I'm trying to learn Java Streams and trying to get a HashSet<Person> from a HashSet<SortedSet<Person>> . HashSet<Person> students = getAllStudents(); HashSet<SortedSet<Person>> teachersForStudents = students.stream().map(Person::getTeachers).collect(Collectors.toCollection(HashSet::new)); HashSet<Person> = //combine teachers and students in one HashSet What I really want it to combine all teachers and all students in one HashSet<Person> . I guess I'm doing something wrong when I'm collecting my stream? You can flatMap each student into a stream formed by the student along with their teachers:

Stream of cartesian product of other streams, each element as a List?

↘锁芯ラ 提交于 2019-12-04 05:46:29
How can I implement a function using Java 8 to take some number of streams, and produce a stream in which each element is a list consisting of one member of the Cartesian product of the streams? I've looked at this question -- that question uses an aggregator that is a BinaryOperator (taking two items of like type and producing an item of the same type). I'd like the items in the end result to be List s rather than the types of the elements in the input streams. Concretely, supposing my desired function is called product , the following: Stream<List<String>> result = product( Stream.of("A", "B

Java stream operation order of execution at terminal point [duplicate]

本秂侑毒 提交于 2019-12-04 05:37:24
This question already has an answer here: Stream intermediate operations ordering 2 answers I have been trying to find clear contract from official Java documentation as to in which order Java streams, once a terminal operation is called, process the elements and call intermediate operations. For example lets look at these examples that use both Java stream version and plain iteration version (both producing same outcome) . Example1: List<Integer> ints = Arrays.asList(1, 2, 3, 4, 5); Function<Integer, Integer> map1 = i -> i; Predicate<Integer> f1 = i -> i > 2; public int findFirstUsingStreams

Java 8 Stream - Why is filter method not executing? [duplicate]

[亡魂溺海] 提交于 2019-12-04 05:18:36
This question already has an answer here: Why does Java8 Stream generate nothing? 3 answers I am learning filtering using java stream. But the stream after filtering is not printing anything. I think the filter method is not getting executed. My filtering code is as follows: Stream.of("d2", "a2", "b1", "b3", "c") .filter(s -> { s.startsWith("b"); System.out.println("filter: " + s); return true; }); There is no compilation error and no exception also. Any suggestion? filter is an intermediate operation, which will be executed only if the Stream pipeline ends in a terminal operation. For example

How Can I use Java Stream To Build a List<Integer> And The Integer Between a and b

♀尐吖头ヾ 提交于 2019-12-04 05:08:27
问题 Say I have two variables : a = 5 , b = 8 , And I want : Arrays.asList(5, 6, 7, 8) How Can I Use Java stream To get this? 回答1: you can use IntStream.rangeClosed to generate the numbers and collect into a list. List<Integer> result = IntStream.rangeClosed(a, b) .boxed() .collect(Collectors.toList()); 来源: https://stackoverflow.com/questions/47975249/how-can-i-use-java-stream-to-build-a-listinteger-and-the-integer-between-a-and

How can I make an IntStream from a byte array?

随声附和 提交于 2019-12-04 05:05:45
I already know there are only IntStream and LongStream . How can I make an IntStream from a byte array? Currently I'm planning to do like this. static int[] bytesToInts(final byte[] bytes) { final int[] ints = new int[bytes.length]; for (int i = 0; i < ints.length; i++) { ints[i] = bytes[i] & 0xFF; } return ints; } static IntStream bytesToIntStream(final byte[] bytes) { return IntStream.of(bytesToInt(bytes)); } Is there any easier or faster way to do this? A variant of Radiodef's answer: static IntStream bytesToIntStream(byte[] bytes) { return IntStream.range(0, bytes.length) .map(i -> bytes[i

How to check if Collection is not empty using java Stream

旧城冷巷雨未停 提交于 2019-12-04 04:39:00
I am new to Java 8. I am not able to understand what is wrong in the following piece of code. The idea is to sent Collection<User> if its not empty. But if the collection is empty than sent HttpStatus.NOT_FOUND Entity response. @RequestMapping(value = "/find/pks", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<Collection<User>> getUsers(@RequestBody final Collection<String> pks) { return StreamSupport.stream(userRepository.findAll(pks).spliterator(), false) .map(list -> new ResponseEntity<>(list , HttpStatus.OK)) .orElse(new ResponseEntity<>