How can I handle null objects in array which binds from an inputs?
I have
input name=example value=3,4
input name=example value=""
List example = new ArrayList()
the bind result is a list with 3 elements = null ,3, 4 is there an attribute that I can put on the list to ignore this null?
#JsonInclude(Include.NON_NULL) isn't working.
You can try to send your list to Apache ListUtils:
List result = ListUtils.predicatedList(example, PredicateUtils.notNullPredicate());
You can use classic Java 8 Stream API, it will look like this:
list = list.stream()
.filter(Objects::nonNull)
.collect(Collectors.toList());
Related
Having two lists of custom objects Entity(id, name) - mainEntityList, otherEntityList and I want to iterate through them to replace the items in mainEntityList with that of otherEntityList where the ids match.
Ex
mainEntityList = [{1, "abc"},{2, "xyz"}]
otherEntityList = [{2, "value"}]
Then after replacing I should have
mainEntityList = [{1, "abc"},{2, "value"}]
It is working with the conventional loop method but what would be the best solution using java stream? Thanks!
Create a map from your otherEntityList mapping each Id to the Entity object and then use List.replaceAll cheking if the map keyset contains the ids in your mainEntityList:
List<Entity> mainEntityList = new ArrayList<>();
mainEntityList.add(new Entity(1, "abc"));
mainEntityList.add(new Entity(2, "xyz"));
List<Entity> otherEntityList = new ArrayList<>();
otherEntityList.add(new Entity(2, "value"));
Map<Integer,Entity> map = otherEntityList.stream().collect(Collectors.toMap(Entity::getId,Function.identity()));
mainEntityList.replaceAll(entity -> map.keySet().contains(entity.getId()) ? map.get(entity.getId()): entity);
System.out.println(mainEntityList);
List<EPosition> has S and P role.
For some employee, P role exists but now S does not exists.
So Source will only provide P but not S row in list.
In Database, WE have both P as well as S.
We want to make all S row as NULL (do not delete). So how to compare two lists
List<EPosition> src; //Fetch from Soap
List<EPosition> db;//Fetch from DB
for (EPosition d: db){
for (Eposition s: src){
if (s.ID = d.ID){
//Make it null
}
}
}
merge later
Problem statement: From the problem statement, what I understood is you have two lists (say A and B), where A contains some objects which are also present in B and you want to set this value to null in A using java streams.
Solution: So to do that you need to use the map on stream where each object is mapped to null if you find the object in B else object itself. To find whether the object is present in B there are two ways :
a. use contains method of the list (this will use equals method of the object in your case Eposition)
dbs = dbs.stream()
.map(db -> return src.contains(db) ? null :db)
.collect(Collectors.toList());
b. simply iterate over second list using stream and find a match of the id using anyMatch function.
dbs = dbs.stream()
.map(db -> return src.stream().anyMatch(sid -> sid.ID.equals(did.ID) ? null :db)
.collect(Collectors.toList());
I am learning Java Streams and want to replace the below code with java 8 features.
i was able to use stream.filter() and stream.map features , but i could not replace the below code with java 8 features.
List<Subject> subjects= null;
Set<SubjectData> subjectData= new SubjectData();
for (String name: studentNames)
{
//subjects = student.getSubjects(name);
// consider instead of above line , which returns a collection of <Subject>
for (Subject subject : subjects)
{
subjectData.add(new SubjectData(subject.syllabus(), subject.code()));
}
}
any pointers would be appreciated
I imagine something like this is what you intend:
Set<SubjectData> subjectData = studentNames.stream()
.flatMap(name -> student.getSubjects(name).stream())
.map(subject -> new SubjectData(subject.syllabus(), subject.code()))
.collect(Collectors.toSet());
This streams the student names, maps them to their subjects while concatenating those streams, and then creates SubjectData objects for each. Lastly, those objects are collected into a set.
I've a a stream which I want to partition into smaller parts based on matching Id and then apply some proccessing logic on each of the part/element.
class BigRequest{
String bId;
List<Parts> parts;
//getters and setter here
}
Class Parts{
String pId;
String partId;
//getters and setter here
}
I want to segregate and create a list of Parts of size 10 when the partId of different parts are same.
How to use the filter or reduce or groupingBy function to compare the two elements and put them to a list?
I've tried filter like below, doesn't take p1 variable:
big.stream().filter( p -> p.getPartId() == p1.getPartId()) //error
Tried groupingBy like this
big.stream().collect(Collectors.groupingBy(Parts::getPartId) //error
I want to iterate over the filtered/reduced list a another and call another function called abc(). How can I do it using Java Streams?
pseudo:
big.getParts().stream.
//dividing logic logic
for(i < parts.size)
abc(p)
Thanks
You might use something like this:
Map<String,List<Parts>> commonId = big.getParts().
stream().
collect(
Collectors.groupingBy(
Parts::getPartId,
Collectors.mapping(
Function.identity(),
Collectors.toList()
)
)
);
and after it, you will just need to iterate over the map and apply your function.
commonId.entrySet().stream().map(entry -> apply(entry))...
Updated
We can omit Collectors.mapping(Function.identity(),Collectors.toList()) part, since it is a default behaviour of groupingBy
Map<String,List<Parts>> commonId = big.getParts().
stream().
collect(
Collectors.groupingBy(
Parts::getPartId
)
);
I am trying to extract a filtered list on top of the original list based on some condition. I am using backport version of Java 8 and am not pretty sure how to do this.I get the Set from ccarReport.getCcarReportWorkflowInstances() call. I need to iterate and filter this set based on a condition match( I am comparing the date attribute in each object with the request date being passed. Below is the code
Set<CcarReportWorkflowInstance> ccarReportWorkflowInstanceSet = ccarReport.getCcarReportWorkflowInstances();
List<CcarReportWorkflowInstance> ccarReportWorkflowInstances = StreamSupport.stream(ccarReportWorkflowInstanceSet).filter(ccarReportWorkflowInstance -> DateUtils.isSameDay(cobDate, ccarReportWorkflowInstance.getCobDate()));
The routine which is doing the job
public List<CcarRepWfInstDTO> fetchReportInstances(Long reportId, Date cobDate) {
List<CcarRepWfInstDTO> ccarRepWfInstDTOs = null;
CcarReport ccarReport = validateInstanceSearchParams(reportId, cobDate);
Set<CcarReportWorkflowInstance> ccarReportWorkflowInstanceSet = ccarReport.getCcarReportWorkflowInstances();
List<CcarReportWorkflowInstance> ccarReportWorkflowInstances = StreamSupport.stream(ccarReportWorkflowInstanceSet).filter(ccarReportWorkflowInstance -> DateUtils.isSameDay(cobDate, ccarReportWorkflowInstance.getCobDate()));
ccarRepWfInstDTOs = ccarRepWfInstMapper.ccarRepWfInstsToCcarRepWfInstDTOs(ccarReportWorkflowInstances);
return ccarRepWfInstDTOs;
}
Error I get when I tried to use streams.
Assuming I understood what you are trying to do, you can replace your method body with a single line :
return
validateInstanceSearchParams(reportId, cobDate).getCcarReportWorkflowInstances()
.stream()
.filter(c -> DateUtils.isSameDay(cobDate, c.getCobDate()))
.collect(Collectors.toList());
You can obtain a Stream from the Set by using the stream() method. No need for StreamSupport.stream().
After filtering the Stream, you should collect it into the output List.
I'd use shorter variable and method names. Your code is painful to read.