java-stream

Java 8 stream for-loop

让人想犯罪 __ 提交于 2019-12-05 06:36:47
Im new to Java 8 Streams and would like to convert following code-block to Java 8's Stream way of doing the same thing. Edit : Updates the class-names to be less confusing. (Removed Foo, Bar, Baz...) ArrayList<PriceList> priceLists = new ArrayList<PriceList>(); // I'm casting to a type-safe List from getObjects() // -which is a function I dont have access to. Is there a nice // solution to embed this in the stream-syntax? List<PriceListGroup> distObjects = (List<PriceListGroup>) objects.get(1).getObjects(); for(PriceListGroup group : distObjects) { Set<Affiliate> affiliates = group

Java Streams - Get a “symmetric difference list” from two other lists

牧云@^-^@ 提交于 2019-12-05 06:21:10
Im trying to use Java 8 streams to combine lists. How can I get a "symmetric difference list" (all object that only exist in one list) from two existing lists. I know how to get an intersect list and also how to get a union list. In the code below I want the disjoint Cars from the two lists of cars (bigCarList,smallCarList). I expect the result to be a list with the 2 cars ("Toyota Corolla" and "Ford Focus") Example code: public void testDisjointLists() { List<Car> bigCarList = get5DefaultCars(); List<Car> smallCarList = get3DefaultCars(); //Get cars that exists in both lists List<Car>

LinkedHashMap entrySet's order not being preserved in a stream (Android)

隐身守侯 提交于 2019-12-05 05:38:21
I'm creating a very simple form validation utility for a sign up screen, and I'm running into some unexpected behavior concerning LinkedHashMap and a stream created from its entrySet . I'm storing validation results in a LinkedHashMap , with the following ordering of statements: Map<ValidationResult.SignUpField, Boolean> fieldStatuses = new LinkedHashMap<>(); fieldStatuses.put(EMAIL, isValidEmail(emailAddress)); fieldStatuses.put(USERNAME, isValidUsername(username)); fieldStatuses.put(BIRTHDAY, isValidBirthday(birthday)); fieldStatuses.put(PASSWORD, isValidPassword(password)); fieldStatuses

Use of double colons - difference between static and non-static method references [duplicate]

浪子不回头ぞ 提交于 2019-12-05 05:37:52
This question already has answers here : :: (double colon) operator in Java 8 (17 answers) Closed 3 years ago . Edit: My question here was answered. To summarize, I was confused about the usage of non-static method references. There the functional interface and referenced method have a different number of parameters. What answered my question is the comment and the accepted answer. I am currently reading the Java Tutorial about Stream reduction methods ( https://docs.oracle.com/javase/tutorial/collections/streams/reduction.html ). There I found a piece of code that I thought was wrong, so I

Map to a running sum in Java 8

僤鯓⒐⒋嵵緔 提交于 2019-12-05 05:22:40
If I have a collection: List<Long> numbers = asList(2, 2, 4, 5); How can I map/process these to build up a running total. To produce something like: List<Long> runningTotals = asList(2, 4, 8, 13); Even better, how can I build a list of something (like a tuple) so I can preserve the orignals: ((2 -> 2), (2 -> 4), (4 -> 8), (5 -> 13)); Update : As Holger pointed out in the comments using Stream.reduce() for this purpose is not correct. See Reduction and Mutable Reduction or Java 8 Streams - collect vs reduce for more information. You can use Java Stream.collect() instead to generate your list

Naming java methods that return streams

*爱你&永不变心* 提交于 2019-12-05 05:13:14
Is there naming convention for methods that return Stream? The only mention I found is this answer on S.O (last paragraph), but I don't see what is it based on. Since I wrote that paragraph I feel compelled to answer. :-) Suppose you have a class that represents an aggregation of things of a single type, and you want to return a Stream of them to the caller. If it's totally unambiguous as to what you're returning, you might just as well call the method stream() . There are a lot of methods in the JDK named stream() that return a stream of the obvious type. Sometimes what you're returning is

How to store enum to map using Java 8 stream API

我们两清 提交于 2019-12-05 05:09:50
I have an enum with another enum as a parameter public enum MyEntity{ Entity1(EntityType.type1, .... MyEntity(EntityType type){ this.entityType = entityType; } } I want to create a method that return the enum by type public MyEntity getEntityTypeInfo(EntityType entityType) { return lookup.get(entityType); } usually I would have written private static final Map<EntityType, EntityTypeInfo> lookup = new HashMap<>(); static { for (MyEntity d : MyEntity.values()){ lookup.put(d.getEntityType(), d); } } What is the best practice to write it with java stream? Alexis C. I guess there are some typos in

Encounter order preservation in java stream

南笙酒味 提交于 2019-12-05 04:48:40
问题 I have gone through related questions like How to ensure order of processing in java8 streams?, still ordering of output element is not completely clear to me. Therefore please clarify my following doubt. Integer[] intArray = {1, 2, 3, 4, 5, 6, 7, 8 }; List<Integer> listOfIntegers = new ArrayList<>(Arrays.asList(intArray)); listOfIntegers .parallelStream() .unordered() .forEachOrdered(e -> System.out.print(e + " ")); I think at least theoretically(or according to java specification) it can

Java Lambda - check if an ArrayList to Stream is empty

不羁岁月 提交于 2019-12-05 04:29:25
I have the following lambda expression and if works fine when bonusScheduleDurationContainers is not empty. If it is empty, I get a NoSuchElementException . How do I check this in the lambda expression? final List<ScheduleDurationContainer> bonusScheduleDurationContainers = scheduleDurationContainersOfWeek.stream() .filter(s -> s.getContainerType() == ScheduleIntervalContainerTypeEnum.BONUS) .collect(Collectors.toList()); final ScheduleDurationContainer bonusScheduleDurationContainer = bonusScheduleDurationContainers.stream() .filter(s -> s.getDayOfWeekStartingWithZero() == dayOfWeekTmp)

Replacing traditional newForLoop with Java 8 Streams

不羁岁月 提交于 2019-12-05 04:13:48
So, finally making a relatively large jump from Java 6 to Java 8, I've read up a fair amount of Java 8 Streams API. Unfortunately, almost all the examples that have been asked are almost close to what I'm trying to figure out how to do, but not close enough. What I have is final List<Function<? super Double, Double>> myList = generateList(); final double myVal = calculate(10); private double calculate(double val) { for (Function<? super Double, Double> function : this.myList) { val += function.apply(val); } return val; } Now, I've come to understand I could do something similar with .stream()