java-stream

How can I make an IntStream from a byte array?

北城余情 提交于 2019-12-06 01:14:27
问题 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? 回答1: A variant of Radiodef's answer:

Java 8: Extracting a pair of arrays out of a Stream<Pair>

旧巷老猫 提交于 2019-12-06 00:37:03
So I have some code using Java 8 streams, and it works. It does exactly what I need it to do, and it's legible (a rarity for functional programming). Towards the end of a subroutine, the code runs over a List of a custom pair type: // All names Hungarian-Notation-ized for SO reading class AFooAndABarWalkIntoABar { public int foo_int; public BarClass bar_object; .... } List<AFooAndABarWalkIntoABar> results = ....; The data here must be passed into other parts of the program as arrays, so they get copied out: // extract either a foo or a bar from each "foo-and-bar" (fab) int[] foo_array =

Is there something like Java Stream's “peek” operation in Scala?

折月煮酒 提交于 2019-12-06 00:02:05
问题 In Java you can call peek(x -> println(x)) on a Stream and it will perform the action for each element and return the original stream, unlike foreach which is Unit. Is there something similar in Scala, ideally something which works on all Monady types, allowing you to "pass through" the original Monad while performing a side effect action? (Logging, e.g.) It is of course easily implemented: def tap[A, U](a: A)(action: (A) => U): A = { action(a) a } but I'm hoping for something a bit more

Collect all objects from a Set of Sets with Java Stream

◇◆丶佛笑我妖孽 提交于 2019-12-05 23:37:20
问题 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

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

对着背影说爱祢 提交于 2019-12-05 23:21:17
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. You require only a "simple" map here: List<List<String>> listOfListValues; public List<String> getAsFlattenedList(

Java 8 Collection and stream/forEach

白昼怎懂夜的黑 提交于 2019-12-05 23:12:07
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 foo.forEach(); // Goes through every item in foo foo.stream().forEach(); // Does stream make a difference here It is useless unless you need stream operations like map or filter. foo.parallelStream().forEach(); This spawns a new thread for every logical core of

Filtering keys from Map to List attribute in Java 8

自作多情 提交于 2019-12-05 22:20:43
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",null), new student("3", "Ravi",null), new student("2", "Ram",null)); Map<String,String> newmap = new

Java 8 java.util.stream.Streams

纵然是瞬间 提交于 2019-12-05 21:56:13
问题 I see many blog posts mention a Streams class and I see it was once part of the lambda branch API. It appears to be non-public API now and it does not match the previous implementation. Is there a different way to do Streams.concat() or to append multiple values to a stream? 回答1: The Streams class got split and some of its methods were moved to StreamSupport, which does not contain a concat method in the latest build. The rationale for the split is explained here. The specific case of concat

How to build a Map that replicates a Function in Java's Lambda API

我只是一个虾纸丫 提交于 2019-12-05 21:23:56
From a java.util.function.BiFunction that maps a pair of Enum s into a value, I want to build a EnumMap that reflects that mapping. For instance, let E1 and E2 be enum types and T any given type: BiFunction<E1,E2, T> theBiFunction = //...anything EnumMap<E1,EnumMap<E2,T>> theMap = buildTheMap( // <-- this is where the magic happens E1.values(), E2.values(), theBiFunction); Given any pair of values of type E1 and E2 E1 e1 = //any valid value... E2 e2 = //any valid value.... both values below should be equal: T valueFromTheMaps = theMap.get(e1).get(e2); T valueFromTheFunction = theBiFunction

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

流过昼夜 提交于 2019-12-05 20:47:25
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 A method reference is telling Java "treat this method as the implementation of a single-method interface"--that is, the method reference should have the signature int foo(String,String) and thus