Can Java 8 Streams use multiple items from mapping pipeline - java

I have some data stored in a JPA Repository that I am trying to process. I would like to be able to use Java 8 Streams to do so, but can not figure out how to get the required information. This particular 'Entity' is actually only for recovery, so it holds items that would need to be processed after something like a power-fail/restart.
Using pre-Java 8 for-loops the code would look like:
List<MyEntity> deletes = myEntityJpaRepository.findByDeletes();
for (MyEntity item : deletes) {
String itemJson = item.getData();
// use a Jackson 'objectMapper' already setup to de-serialize
MyEventClass deleteEvent = objectMapper.readValue(itemJson, MyEventClass.class);
processDelete(deleteEvent, item.getId());
}
The problem arises from the two parameter method called at the very end. Using Streams, I believe I would do:
// deletes.stream()
// .map(i -> i.getData())
// .map(event -> objectMapper.readValue(event, MyEventClass.class))
// .forEach(??? can't get 'id' here to invoke 2 parameter method);
I have a solution (without Streams) that I can live with. However I would think this problem comes up a lot, thus my question is: IN GENERAL, is there a way using Streams to accomplish what I am trying to do?

Why not a Pair return on your map operation:
.map(i -> new Pair<>(i.getData(), i.getId()))
.map(pair -> new Pair<>(objectMapper.readValue(pair.getLeft(), MyEventClass.class), pair.getRight())
.forEach(p -> processDelete(pair.getLeft(), pair.getRight()))
I did not compile this, so there might be minor things to fix. But in general, you would need a Holder to pass your objects to the next stage in such a case. Either a Pair or some type or even a array.

Why not doing it simply this way?
deletes.forEach(item ->
processDelete(objectMapper.readValue(item.getData(), MyEventClass.class),
item.getId()));

This is a start at least, I guess it is dependent on why you want to use stream and how much you want to make it more functional
List<MyEntity> deletes = myEntityJpaRepository.findByDeletes();
deletes.stream().foreach(item -> {
String itemJson = item.getData();
// use a Jackson 'objectMapper' already setup to de-serialize
MyEventClass deleteEvent = objectMapper.readValue(itemJson, MyEventClass.class);
processDelete(deleteEvent, item.getId());
});

Related

Java streaming higher order function

I am trying to process an object that has nested lists 2 levels deep. For example my object can be broken down to something like this:
TopLevel: [
MidLevel: [
LowLevel,
LowLevel,
.
.
],
MidLevel: [
LowLevel,
LowLevel,
.
.
]
.
.
]
Essentially TopLevel contains a list of MidLevel objects, which in turn each contain a list of LowLevel objects. At the end of the processing I would like to build SomeObj for each of the LowLevel objects. However SomeObj requires information from TopLevel, MidLevel, and LowLevel.
I have been trying to write code in a more functional style over the last several months so my first thought was to create a higher order function that I could build up over each level in the object. The function looks like this:
Function<MidLevel, Function<LowLevel, SomeObj>> buildObjects(TopLevel topLevel) {
return midLevel ->
lowLevel -> {
return buildSomeObj(topLevel, midLevel, lowLevel);
};
}
I intend to use this function in some way like the following (assume I have utility functions that provide a stream of the lists):
Function<MidLevel, Function<LowLevel, SomeObj>> topBuilder = buildObjects(topLevel);
List<SomeObj> objs = topLevel.streamMid()
.map(topBuilder)
.streamLow()
.map(Function::apply)
.collect(/*collect to list*/);
However, this obviously does not work because once I apply the MidLevel objects to the topBuilder function my stream is now a stream of functions and not MidLevel objects, thus I do not have access to the list of LowLevel objects in the stream anymore.
Is there any solution to this or am I trying to solve this functionally when it's not well suited to it? Is there a way to both apply the function, and also have access to the original object that was applied to that function?
flatMap() and nesting are the way to go. Try this:
topLevelStream() //create a stream of top level elements
.flatMap( top -> top.streamMid() //create a stream of mid level elements
.flatMap( mid -> mid.streamLow() //create a stream of low level elements
.map(low -> "use top, mid and low here")
)
)
.collect( ... );
By nesting like this you still have access to the elements in the outer functions and the combination of flatMap() and map() exposes the stream that map() is called upon to collect().
You could simply use flatMap as:
List<SomeObj> objs = topLevel.getMidLevels().stream()
.flatMap(a -> a.getLowLevels().stream().map(b -> topBuilder.apply(a).apply(b)))
.collect(Collectors.toList());
with your entities analogous to:
class TopLevel {
List<MidLevel> midLevels;
}
class MidLevel {
List<LowLevel> lowLevels;
}
class LowLevel {
}
class SomeObj {
}

Populate additional/extra fields only in list from another collection (with lambdas)

I have an original collection, List<Reviewers>, and a new one List<ReviewPerson> where some fields from Reviewers will be copied to ReviewPerson.
The new list is constructed in a special way and not directly from reviewers.stream().map(...). But at the end, I need to copy 2 additional columns that exist in each bean, status and comments.
List<Reviewers> originalList = ... // from DAO
if (!originalList.isEmpty()) {
List<ReviewPerson> newList = new ArrayList<ReviewPerson>();
// this fills out some columns of the ReviewPerson, not all;
// must use this partial construction from service class
newList.addAll(service.initialPopulation());
// At the end, need to copy: (1) status, (2) comments
// ...
}
The problem is I can't do this,
originalList.stream()
.map(obj -> new ReviewPerson(obj.getField1(), obj.getField2(),
// ...
obj.getStatus(), obj.getComments()))
.collect(Collectors.toList());
because I'm not constructing new objects in the collection. What should I do?
One common solution is to stream the (presumably corresponding) indexes of the lists and use the same index to access both lists:
IntStream.range(0, originalList.size()).forEach(i -> {
newList.get(i).setFieldA(originalList.get(i).getFieldA();
newList.get(i).setFieldB(originalList.get(i).getFieldB();
// etc...
});
But to be honest, this may be streaming for the sake of streaming. Sometimes a good old-fashioned straight-forward for loop is just a better solution.
If you don't mind an extra dependency, and if Reviewers and ReviewPersons are indeed corresponding, I'd suggest using the jOOλ library and its Seq.zip() method (Seq is a subtype of Stream) together with a Tuple.consumer overload.
With the above, you can end up with such a concise piece of code:
Seq.zip(newList, originalList).forEach(Tuple.consumer((reviewPerson, reviewer) -> {
reviewPerson.setStatus(reviewer.getStatus());
reviewPerson.setComments(reviewer.getComments());
}));

Converting Linq queries to Java 8

Im traslating a old enterprise App who uses C# with Linq queries to Java 8. I have some of those queries who I'm not able to reproduce using Lambdas as I dont know how C# works with those.
For example, in this Linq:
from register in registers
group register by register.muleID into groups
select new Petition
{
Data = new PetitionData
{
UUID = groups.Key
},
Registers = groups.ToList<AuditRegister>()
}).ToList<Petition>()
I undestand this as a GroupingBy on Java 8 Lambda, but what's the "select new PetitionData" inside of the query? I don't know how to code it in Java.
I have this at this moment:
Map<String, List<AuditRegister>> groupByMuleId =
registers.stream().collect(Collectors.groupingBy(AuditRegister::getMuleID));
Thank you and regards!
The select LINQ operation is similar to the map method of Stream in Java. They both transform each element of the sequence into something else.
collect(Collectors.groupingBy(AuditRegister::getMuleID)) returns a Map<String, List<AuditRegister>> as you know. But the groups variable in the C# version is an IEnumerable<IGrouping<string, AuditRegister>>. They are quite different data structures.
What you need is the entrySet method of Map. It turns the map into a Set<Map.Entry<String, List<AuditRegister>>>. Now, this data structure is more similar to IEnumerable<IGrouping<string, AuditRegister>>. This means that you can create a stream from the return value of entry, call map, and transform each element into a Petition.
groups.Key is simply x.getKey(), groups.ToList() is simply x.getValue(). It should be easy.
I suggest you to create a separate method to pass into the map method:
// you can probably came up with a more meaningful name
public static Petition mapEntryToPetition(Map.Entry<String, List<AuditRegister>> entry) {
Petition petition = new Petition();
PetitionData data = new PetitionData();
data.setUUID(entry.getKey());
petition.setData(data);
petition.setRegisters(entry.getValue());
return petition;
}

Java 8 lambda expression is present

First I need to check if data is present in list then get else set default or empty value on a Java 8 stream.
Currently I am using below code without isPresent but I dont know how to use isPresent in java8.
I am trying something below which is not perfect:
String isScheme = (this.mapProgramApproaches.stream().findFirst().isPresent())? this.mapProgramApproaches.stream().findFirst().get().getIsScheme().toString() : "0";
Where as mapProgramApproaches this is set.
Don't use isPresent() (it makes no sense to run the Stream pipeline twice).
You can use map to map the value of the Optional to the required String, and then
use orElse() to return a default value when the Optional value is not present:
String isScheme = this.mapProgramApproaches.stream()
.findFirst()
.map(o->o.getIsScheme().toString())
.orElse("0");
Maybe you are looking for something like this:
String isScheme = this.mapProgramApproaches.stream()
.findFirst()
.map(p -> p.getIsScheme().toString())
.orElse("0");
I'm not sure about context in which you are doing this, but I suppose that you would like to check whether some object is scheme and then do something with that. In that case I would suggest implement it like this:
List<String> mapProgramApproaches = new ArrayList<>();
mapProgramApproaches.stream()
.filter(this::isScheme)
.findFirst()
.ifPresent(this::doYourCode)
.orElse(defaultValue);
It will make your code cleaner. And will help to avoid additional conditionals!

Java, search for an element, if not found, add it

I am pretty new to streams.
I would like to stream the geometries EC_Geometry arraylist and if the EC_Geometry element is not present (or better equals never returns true), then I add it.
public void init(GL3 gl3, EC_Mesh mesh) {
geometries.stream()
.filter(geometry -> mesh.getGeometry().equals(geometry))
.findAny()
.orElse(..?);
}
But I am stuck at the last line
How can I solve it using streams?
Please note that equals is a method I wrote checking if the geometry is the same (i.e: if the triangles correspond)
orElse will always run even if the value returned isn't used so it is preferable to use orElseGet here which will only run if nothing is found.
geometries.stream()
.filter(geometry -> mesh.getGeometry().equals(geometry))
.findAny()
.orElseGet(() -> {
geometries.add(mesh.getGeometry());
return mesh.getGeometry();
});
.findAny().orElse(..?);
is for Optional - if you would like to get first element found.
For what you would like to achieve the best approach would be just to:
meshG = mesh.getGeometry();
if (!geometries.contains(meshG)) {
geometries.add(meshG);
}
No need to overuse Stream API.

Categories

Resources