java-stream

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

我是研究僧i 提交于 2019-12-22 09:46:25
问题 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

In Java 8, can I use streams to filter a partial string?

妖精的绣舞 提交于 2019-12-22 09:33:24
问题 In Java 8, can I use streams to filter a partial string? Let us assume I have a list of animals like: Brown Bear Black Bear Black Crow Red Herring Owl Sparrow Blackbacked Flocking Crow Let us assume all of the Animals names are in a list of Animals Objects public class Animal{ public name; public animalType; } Is there some way to find all of the animals that have Black regardless of the case somewhere in the name. Something like the following... List<Animal> filtList = employeeList.stream()

Java 8 streams: conditional Collector

杀马特。学长 韩版系。学妹 提交于 2019-12-22 09:33:01
问题 I want to use Java 8 streams to convert a List of String values to a single String. A List of values like "A", "B" should return a String like "Values: 'A', 'B' added". This works fine, however I want to change the Pre- and Postfix depending on the amount of values. For example, if I have a List of only "A" I want the resulting String to be "Value 'A' added". import java.util.stream.Collectors; import java.util.ArrayList; import java.util.List; public class HelloWorld { public static void

Java 8 Stream - Find largest nested list

半腔热情 提交于 2019-12-22 08:55:15
问题 I have a Collection<List<SomeObject>> values How can I find the collection with the largest list using Streams? I have tried something like this, but it doesn't quite work values.stream().max(e -> e.stream().max(List::size).get()).get() But I get compilation error. Any ideas? 回答1: I think you want values.stream().max(Comparator.comparingInt(List::size)).get() If you need duplicates, the best solution I can think of would be something like values.stream() .collect(Collector.of( ArrayList::new,

Retrieve fixed number of entries around an entry in a map sorted by values

笑着哭i 提交于 2019-12-22 08:30:25
问题 The POJO viz. Entry.java represents an entry in the leaderboard. Position is the position in the leaderboard, 1 being the user with the highest score public class Entry { private String uid; private int score; private int position; @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + score; result = prime * result + ((uid == null) ? 0 : uid.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if

How to get n first values from an Iterator in Java 8?

主宰稳场 提交于 2019-12-22 08:24:03
问题 I have sorted a HashMap using Sort a Map<Key, Value> by values (Java) to that I have a LinkedHashMap , i.e. an Iterable which garantees iteration order. Now, I'd like to retrieve a java.util.List of the first n entries of the map with a one-liner, if possible with a Java 8 Collection Stream API-technique. I found how can i get two consecutive values from Iterator which explains that there's a possibility to do that with an array, but that's not elegant, differs from my intention to get a List

Change data in an immutable way with Java stream

让人想犯罪 __ 提交于 2019-12-22 08:16:05
问题 Consider this code: Function<BigDecimal,BigDecimal> func1 = x -> x;//This could be anything Function<BigDecimal,BigDecimal> func2 = y -> y;//This could be anything Map<Integer,BigDecimal> data = new HashMap<>(); Map<Integer,BigDecimal> newData = data.entrySet().stream(). collect(Collectors.toMap(Entry::getKey,i -> func1.apply(i.getValue()))); List<BigDecimal> list = newData.entrySet().stream().map(i -> func2.apply(i.getValue())).collect(Collectors.toList()); Basically what I'm doing is

How do I map this in Java 8 using the stream API?

好久不见. 提交于 2019-12-22 08:06:13
问题 Ok, so I have a List<Person> . and each Person has a List<String> that is a list of phone numbers that that person owns. So this is the basic structure: public class Person { private String name; private List<String> phoneNumbers; // constructors and accessor methods } I would like to create a Map<String, Person> where the key is each phone number that the person owns, and the value is the actual person. So to explain better. If I had this List<Person> : Person bob = new Person("Bob"); bob

Why can’t I observe the same performance improvement on both JVMs 32 and 64bits?

假如想象 提交于 2019-12-22 07:57:10
问题 I was testing two different approaches ( primes() and primesOpt() ) to collect the first N prime numbers using the Java 8 IntStream . I took these examples from chapter 6 of Java 8 in Action. You can get the source code from this gist Primes.java and this pom.xml to build it with Maven and JMH integration. (you can copy pom.xml to project folder and Primes.java to src\main\java\primes and build it with the command: mvn clean install . After that you can run the benchmark with: java -jar

Map<S,S> to List<S>

a 夏天 提交于 2019-12-22 07:36:08
问题 I'm trying to convert a Map<String, String> to a List<String> using lambdas. Essentially I'd like to concatenate the key and value with an '=' in between. This seems trivial but I can't find how to do it. E.g. Map<String, String> map = new HashMap<>(); map.put("a1","b1"); map.put("a2","b2"); map.put("a3","b3"); // Lambda // Result contains ["a1=b1", "a2=b2", "a3=b3"] List<String> result; 回答1: For java 7, you can do it in one line too, starting from Map#toString() : List<String> list = Arrays