Joining 2 streams from same object in java - java

I have a list of objects of class A defined as below:
class A {
private Set<String> sOne;
private Set<String> sTwo;
// Constructor, getters and setters
}
Now I would like to create a stream which contains elements of both sOne and stwo. Is there a way to do it in Java 8?

You can combine them using:
List<A> aList = ...;
Stream<String> stream = aList.stream()
.flatMap(a -> Stream.concat(
a.getsOne().stream(),
a.getsTwo().stream())
);

Stream.concat(sOne.stream(), sTwo.stream())
You should just be aware that this drops some characteristics IIRC in some cases.

An alternative to already mentioned Stream::concat is the Stream::of:
Stream.of(sOne.stream(), sTwo.stream())
.flatMap(Function.identity())
...
This requires to flatten the structure unless you wish to work with Stream<Stream<T>> or a stream of any collection Stream<Collection<T>>.

Related

Best way to replace nested loop concatenate string using java stream

I have a list of names and a list of versions. I want to get all permutations which are constructed by concatenating the string from two lists. I am using two for loop to do this but I want to switch to a more functional style approach. Here is my solution:
List<String> names = new ArrayList<>();
List<String> versions = new ArrayList<>();
List<String> result = new ArrayList<>();
names.forEach(name -> versions.stream().map(version -> result.add(name.concat(version))));
Is there a better way to do it?
You are looking for the "Cartesian Product" of names and versions — basically the return set/list from the aforementioned sets/lists.
final Stream<List<String>> result = names.stream()
.flatMap(s1 -> versions.stream().flatMap(s2 -> Stream.of(Arrays.asList(s1, s2))));
result.forEach(System.out::println);
Keep in mind that operation is super expensive. Google's Guava have this implemented also under com.google.common.collect.Sets.cartesianProduct(s1, s2).
You should look forward to use flatMap while streaming over names and then performing map operation further correctly as:
List<String> result = names.stream() // for each name
.flatMap(name -> versions.stream() // for each version
.map(version -> name.concat(version))) // concat version to the name
.collect(Collectors.toList()); // collect all such names
Or a bit tidier:
final List<String> result = names.stream() // Stream the Names...
.flatMap(name -> versions.stream() // ...together with Versions.
.map (version -> name.concat(version))) // Combine Name+Version
.collect(Collectors.toList()); // & collect in List.

Nested collections lambda iteration

Suppose I have an object containing a collection, each elements on the said collection contains a collection, and each collection contains a collection.
And I want to iterate on the deepest objects and apply the same code to it.
The imperative way is trivial, but is there a way to lambda-fy this all?
Here is how the code looks today:
My object o;
SecretType computedThingy = 78;
for (FirstLevelOfCollection coll : o.getList()) {
for (SecondLevelOfCollection colColl : coll.getSet()) {
for (MyCoolTinyObjects mcto : colColl.getFoo()) {
mcto.setSecretValue(computedThingy);
}
}
}
I can see how to make a lambda out of the deepest loop:
colColl.getFoo().stream().forEach(x -> x.setSecretValue(computedThingy)
But can I do more?
flatMap is available for such a purpose. What you get here is iteration over all elements of the various deepest collections as if they were a single collection:
o.getList().stream()
.flatMap(c1 -> c1.getSet().stream())
.flatMap(c2 -> c2.getFoo().stream())
.forEach(x -> x.setSecretValue(computedThingy));
flatMap to the rescue, simple example with a nested collection of String
See also:
Java 8 Streams FlatMap method example
Turn a List of Lists into a List Using Lambdas
Set<List<List<String>>> outerMostSet = new HashSet<>();
List<List<String>> middleList = new ArrayList<>();
List<String> innerMostList = new ArrayList<>();
innerMostList.add("foo");
innerMostList.add("bar");
middleList.add(innerMostList);
List<String> anotherInnerMostList = new ArrayList<>();
anotherInnerMostList.add("another foo");
middleList.add(anotherInnerMostList);
outerMostSet.add(middleList);
outerMostSet.stream()
.flatMap(mid -> mid.stream())
.flatMap(inner -> inner.stream())
.forEach(System.out::println);
Produces
foo
bar
another foo

Stream: Filter on children, return the parent

Assume a class MyClass:
public class MyClass {
private final Integer myId;
private final String myCSVListOfThings;
public MyClass(Integer myId, String myCSVListOfThings) {
this.myId = myId;
this.myCSVListOfThings = myCSVListOfThings;
}
// Getters, Setters, etc
}
And this Stream:
final Stream<MyClass> streamOfObjects = Stream.of(
new MyClass(1, "thing1;thing2;thing3"),
new MyClass(2, "thing2;thing3;thing4"),
new MyClass(3, "thingX;thingY;thingZ"));
I want to return every instance of MyClass that contains an entry "thing2" in myCSVListOfThings.
If I wanted a List<String> containing myCSVListOfThings this could be done easily:
List<String> filteredThings = streamOfObjects
.flatMap(o -> Arrays.stream(o.getMyCSVListOfThings().split(";")))
.filter("thing2"::equals)
.collect(Collectors.toList());
But what I really need is a List<MyClass>.
This is what I have right now:
List<MyClass> filteredClasses = streamOfObjects.filter(o -> {
Stream<String> things = Arrays.stream(o.getMyCSVListOfThings().split(";"));
return things.anyMatch(s -> s.equals("thing2"));
}).collect(Collectors.toList());
But somehow it does not feel right. Any cleaner solution than opening a new Stream inside of a Predicate?
Firstly, I recommend you to add extra method to MyClass public boolean containsThing(String str), so you can transform you code like this:
List<MyClass> filteredClasses = streamOfObjects
.filter(o -> o.containsThing("thing2"))
.collect(Collectors.toList());
Now you can implement this method as you want depends on input data: splitting into Stream, splitting into Set, even searching of substring (if it's possible and has sense), caching result if you need.
You know much more about usage of this class so you can make right choice.
One solution is to use a pattern matching that avoids the split-and-stream operation:
Pattern p=Pattern.compile("(^|;)thing2($|;)");
List<MyClass> filteredClasses = streamOfObjects
.filter(o -> p.matcher(o.getMyCSVListOfThings()).find())
.collect(Collectors.toList());
Since the argument to String.split is defined as regex pattern, the pattern above has the same semantic as looking for a match within the result of split; you are looking for the word thing2 between two boundaries, the first is either, the beginning of the line or a semicolon, the second is either, the end of the line or a semicolon.
Besides that, there is nothing wrong with using another Stream operation within a predicate. But there are some ways to improve it. The lambda expression gets more concise if you omit the obsolete local variable holding the Stream. Generally, you should avoid holding Stream instances in local variables as chaining the operations directly will reduce the risk of trying to use a Stream more than one time. Second, you can use the Pattern class to stream over the resulting elements of a split operation without collecting them all into an array first:
Pattern p=Pattern.compile(";");
List<MyClass> filteredClasses = streamOfObjects
.filter(o -> p.splitAsStream(o.getMyCSVListOfThings()).anyMatch("thing2"::equals))
.collect(Collectors.toList());
or
Pattern p=Pattern.compile(";");
List<MyClass> filteredClasses = streamOfObjects
.filter(o -> p.splitAsStream(o.getMyCSVListOfThings()).anyMatch(s->s.equals("thing2")))
.collect(Collectors.toList());
Note that you could also rewrite your original code to
List<MyClass> filteredClasses = listOfObjects.stream()
.filter(o -> Arrays.asList(o.getMyCSVListOfThings().split(";")).contains("thing2"))
.collect(Collectors.toList());
Now, the operation within the predicate is not a Stream but a Collection operation, but this doesn’t change the semantic nor the correctness of the code…
As I see it you have three options.
1) look for particular entry in the String without spliting it - still looks messy
List<MyClass> filteredClasses = streamOfObjects
.filter(o -> o.getMyCSVListOfThings().contains(";thing2;"))
.collect(Collectors.toList());
2) map twice - still messy
List<MyClass> filteredClasses = streamOfObjects
.map(o -> Pair<MyClass, List<String>>.of(o, toList(o.getMyCSVListOfThings()))
.filter(pair -> pair.getRight().contains("thing2"))
.map(pair -> pair.getLeft())
.collect(Collectors.toList());
where toList is a method that will convert String to List
3) create additional field - method I'd suggest
Extend class MyClass - add field to the class
List<String> values;
And initialize it in the constructor:
public MyClass(Integer myId, String myCSVListOfThings) {
this.myId = myId;
this.myCSVListOfThings = myCSVListOfThings;
this.values = toList(myCSVListOfThings);
}
And then in the stream simply:
List<MyClass> filteredClasses = streamOfObjects
.filter(o -> o.getValues().contains("thing2"))
.collect(Collectors.toList());
Of course field values can be initialized in LAZY mode during first getValues method call if you want.
This is similar to the issue, Getting only required objects from a list using Java 8 Streams, posted a year earlier. I think the solution I left there is applicable here.
There's a library called com.coopstools.cachemonads. It extends the java stream (and Optional) classes to allow caching of entities for later use.
The solution can be found with:
List<Parent> goodParents = CacheStream.of(parents)
.cache()
.map(Parent::getChildren)
.flatMap(Collection::stream)
.map(Child::getAttrib1)
.filter(att -> att > 10)
.load()
.distinct()
.collect(Collectors.toList());
where, parents is an array or stream.
For clarity, the cache method is what stores the parents; and the load method is what pulls the parents back out. And If a parent does not have children, a filter will be needed after the first map to remove the null lists.
More specifically, for your issue:
List<Parent> goodParents = CacheStream.of(streamOfObjects)
.cache()
.map(o -> o.getMyCSVListOfThings().split(";"))
.flatMap(Collection::stream)
.filter("thing2"::equals)
.load()
.collect(Collectors.toList())
This library can be used in any situation where operations need to be performed on children, including map/sort/filter/etc, but where an older entity is still needed. There may be more lines than some of the other answers, but each line is very clean and straight forward.
Please let me know if this answer is helpful.
The code can be found at https://github.com/coopstools/cachemonads or can be downloaded from maven:
<dependency>
<groupId>com.coopstools</groupId>
<artifactId>cachemonads</artifactId>
<version>0.2.0</version>
</dependency>
(or, gradle, com.coopstools:cachemonads:0.2.0)

Intersection of two collections of different objects types java 8

I have two lists of objects:
List<SampleClassOne> listOne;
List<SampleClassTwo> listTwo;
SampleClassOne:
public class SampleClassOne{
private String myFirstProperty;
//ommiting getters-setters
}
SampleClassTwo:
public class SampleClassTwo{
private String myOtherProperty;
//ommiting getters-setters
}
RootSampleClass:
public class RootSampleClass{
private SampleClassOne classOne;
private SampleClassTwo classTwo;
//ommiting getters-setters
}
Now I would like to merge two lists into new list of type RootSampleClass based on condition:
if(classOneObject.getMyFirstProperty().equals(classTwoObject.getMyOtherProperty()){
//create new RootSampleClass based on classOneObject and classTwoObject and add it to another collection
}
Pseudo code:
foreach(one: collectionOne){
foreach(two: collectionTwo){
if(one.getMyFirstProperty().equals(two.getMyOtherProperty()){
collectionThree.add(new RootSampleClass(one, two));
}
}
}
I am interested in java 8. I would like to have the best performance here that's why I am asking for existing solution without writing custom foreach.
A direct equivalent to the nested loops is
List<RootSampleClass> result = listOne.stream()
.flatMap(one -> listTwo.stream()
.filter(two -> one.getMyFirstProperty().equals(two.getMyOtherProperty()))
.map(two -> new RootSampleClass(one, two)))
.collect(Collectors.toList());
with an emphasis on direct equivalent, which includes the bad performance of doing n×m operations.
A better solution is to convert one of the lists into a data structure supporting an efficient lookup, e.g. a hash map. This consideration is independent of the question which API you use. Since you asked for the Stream API, you can do it like this:
Map<String,List<SampleClassOne>> tmp=listOne.stream()
.collect(Collectors.groupingBy(SampleClassOne::getMyFirstProperty));
List<RootSampleClass> result = listTwo.stream()
.flatMap(two -> tmp.getOrDefault(two.getMyOtherProperty(), Collections.emptyList())
.stream().map(one -> new RootSampleClass(one, two)))
.collect(Collectors.toList());
Note that both solutions will create all possible pairings in case, a property value occurs multiple times within either or both lists. If the property values are unique within each list, like IDs, you can use the following solution:
Map<String, SampleClassOne> tmp=listOne.stream()
.collect(Collectors.toMap(SampleClassOne::getMyFirstProperty, Function.identity()));
List<RootSampleClass> result = listTwo.stream()
.flatMap(two -> Optional.ofNullable(tmp.get(two.getMyOtherProperty()))
.map(one -> Stream.of(new RootSampleClass(one, two))).orElse(null))
.collect(Collectors.toList());
If you don’t mind potentially performing double lookups, you could replace the last solution with the following more readable code:
Map<String, SampleClassOne> tmp=listOne.stream()
.collect(Collectors.toMap(SampleClassOne::getMyFirstProperty, Function.identity()));
List<RootSampleClass> result = listTwo.stream()
.filter(two -> tmp.containsKey(two.getMyOtherProperty()))
.map(two -> new RootSampleClass(tmp.get(two.getMyOtherProperty()), two))
.collect(Collectors.toList());

how to get concatenation of nested List<Long> with java collector

I'd like to retrieve the distinct list of Longs given this nested structure:
Class A
public List<B> getBs()
Class B
public List<Long> getIds()
List<A> list = ...
// how do I now get all of longs as a distinct list
I realise I can do 2 for loops but given Java8's new abilities which I'm only just getting used to I'm sure there is a better way.
To clarify, I require List<long> (not List<A>)
Thanks
Something like the code below should work - you go down the nested structure with map, then you "flatMap" the list of ids into one concatenated stream of Longs on which you can apply the distinct intermediate operation.
List<A> list = ...;
List<Long> uniqueIds = list.stream() //Stream<A>
.map(A::getBs) //Stream<List<B>>
.flatMap(List::stream) //Stream<B>
.map(B::getIds) //Stream<List<Long>>
.flatMap(List::stream) //Stream<Long>
.distinct()
.collect(toList());

Categories