How to get value from an optional object in another optional? - java

Basically,I need to get a size of optional list in an optional object. Something like:
private int getCount(#NonNull Optional<myObject> aaa) {
if(aaa.isPresent() && aaa.get().getMyList().isPresent()) {
return aaa.get().getMyList().get().size();
}
return 0;
}
The code doesn't look nice. What's the elegant way to get it? With ifPresent().orElse()?
Thanks in advance!

Consecutive map (or flatMap, in case something returns an Optional) operations, and a final orElse:
private int getCount(#NonNull Optional<myObject> cvm) {
return cvm
.flatMap(x -> x.getMyList())
.map(list -> list.size()) // or List::size
.orElse(0);
}

Related

How to optimise this filtering of list in Java

I have the following code:
private List<String> validate(StartValue start, List<String> colors, Entity entity) {
if (!CollectionUtils.isEmpty(colors)) {
return colors.stream()
.filter(color -> ValidationUtil.getColorfulValues(start, color.getRGBValue()).isEmpty() ||
(!ValidationUtil.getColorfulValues(start, color.getRGBValue()).isEmpty() &&
ValidationUtil.getColorfulValues(start, color.getRGBValue()).contains(entity.getColor())))
.collect(Collectors.toList());
}
return colors;
}
Here ValidationUtil.getColorfulValues is getting called thrice for each value in the list. Is there a way to optimize the filter so that we can save the value of the call?
If you would have not represented the code as a lambda expression, but as a block statement you would have been able to simplify it further.
.filter(color -> {
List<String> colourFulValues = ValidationUtil.getColorfulValues(start,
color.getRGBValue());
return colourFulValues.isEmpty() || colourFulValues.contains(entity.getColor())
}
Of course, you could abstract the block as a Predicate of its own depending on its usage. The type of the Predicate would be the type you have chosen to represent the color. Currently, in your question, for example, if it's a java.lang.String, where is the method color.getRGBValue() associated from?
You can use a method reference:
private List<String> validate(StartValue start, List<String> colors, Entity entity) {
if (!CollectionUtils.isEmpty(colors)) {
return colors.stream()
.filter(this::filter)
.collect(Collectors.toList());
}
return colors;
}
private boolean filter(String color) {
var rgbVal = color.getRGBValue();
var cv = ValidationUtil.getColorfulValues(start, rgbVal);
boolean empty = cv.isEmpty();
return empty || (!empty && cv.contains(entity.getColor()));
}
Note that I use var since it is not clear to me what color.getRGBValue() returns and also what ValidationUtil.getColorfulValues() returns.
Guessing that CollectionUtils.isEmpty considers null elements non-existing.
For the filter condition evaluating ValidationUtil.getColorfulValues(start, color.getRGBValue())) only once requires mapping to it. But one needs to conserve the color too, to collect it in a list. So I introduced a record ColorWithValues. The record class is intended for such kind of uses.
|| is a short-circuit operator and does not need a negation of its first argument.
So:
private List<String> validate(StartValue start, List<String> colors, Entity entity) {
record ColorWithValues(String color, Set<String> values) {}
return colors.stream()
.filter(Objects::nonNull)
.map(c ->
new ColorWithValues(c,
ValidationUtil.getColorfulValues(start, c.getRGBValue())))
.filter(cv -> cv.values.isEmpty() ||
cv.values.contains(entity.getColor()))
.map(cv -> cv.color)
.collect(Collectors.toList());
}

Convert one Optional<List<Object>> to another Optional<List<Object>> in Java

How can I convert Optional List object from one type to another, for an example
Optional<List<ProductMultipleOptionViewModel>> productOptionType1 // One type
Optional<List<ProductMultipleOption>> productOptionType2 // Other type
ProductMultipleOptionViewModel
Type 1
#Introspected
public record ProductMultipleOptionViewModel(
ProductOptionViewModel productOption,
String optionName) {
}
Type 2
#Introspected
public record ProductMultipleOption(
ProductOptionViewModel productOption,
String optionName) {
}
I want to convert from Optional<List<ProductMultipleOption>>to other Optional<List<ProductMultipleOptionViewModel>>. I tried the below code
Optional<List<ProductMultipleOptionViewModel>> conveertItem = Optional.ofNullable(product.getProductMultipleOption())
.orElseGet(null)
.stream()
.map(option -> {
return new ProductMultipleOptionViewModel(
ProductOptionViewModel.valueOf(//Access the option value//), //access the option value//
);
})
.collect(Collectors.toList());
With the above code, I am not able to access the option value inside map method
If product.getProductMultipleOption() is null return null or empty list.
You should rarely use Optional and Collections (like List or Set) together. Instead you should work with empty Collections. Also keep in mind that Optionals should not really be used for conditional logic, but rather as return values.
Either using a normal if statement:
List<ProductMultipleOptionViewModel> conveertItem = new ArrayList<>();
if (product.getProductMultipleOption() != null) {
for(ProductMultipleOption option : product.getProductMultipleOption()) {
conveertItem.add(new ProductMultipleOptionViewModel(
ProductOptionViewModel.valueOf(option)
));
}
}
Another variant:
List<ProductMultipleOption> list = product.getProductMultipleOption();
if (list == null) {
list = Collections.emptyList();
}
List<ProductMultipleOptionViewModel> conveertItem = new ArrayList<>();
for(ProductMultipleOption option : list) {
conveertItem.add(new ProductMultipleOptionViewModel(
ProductOptionViewModel.valueOf(option)
));
}
Or if you really want to use Streams and Optionals (matter of taste):
List<ProductMultipleOptionViewModel> conveertItem = Optional.ofNullable(product.getProductMultipleOption())
.map(List::stream)
.orElseGet(Stream::empty)
.map(option -> new ProductMultipleOptionViewModel(
ProductOptionViewModel.valueOf(option)
))
.collect(Collectors.toList());
Now that's a lot of code for simply converting a nullable List. So why not return an empty List in the first place? Change product.getProductMultipleOption() to something like this:
public List<ProductMultipleOption> getProductMultipleOption() {
List<ProductMultipleOption> list = ...; // your current logic for getting the list
return list == null ? Collections.emptyList() : list;
}
That way you never have to worry about null checks. Because you're simply working with an empty collection wherever you're calling getProductMultipleOption().
It helps to think about dealing with nulls/empty optionals separately from dealing with the list. The code below deals with nulls using the Optional.map() method, which returns an empty optional (of the appropriate return type) if the given argument is empty; otherwise, it applies the mapping function on the list.
Optional<List<ProductMultipleOptionViewModel>> convertedItem =
Optional.ofNullable(product.getProductMultipleOption())
.map(list -> list.stream()
.map(option -> new ProductMultipleOptionViewModel(
option.productOption(),
option.optionName()))
.collect(Collectors.toList()));
Might not be the best way to do whatever you're doing... but to answer your question if you're trying to work with what you've got and keep it minimal:
private List<ProductMultipleOption> getProductOptionViewModelList() {
/* simulating return of a list that could be null. */
return null;
}
private Optional<List<ProductMultipleOption>> getProductMultipleOptionNull() {
/* simulating return of an optional list. */
return Optional.empty();
}
private static class ProductOptionViewModel { }
public record ProductMultipleOptionViewModel(
ProductOptionViewModel productOption,
String optionName) {
}
public record ProductMultipleOption(
ProductOptionViewModel productOption,
String optionName) {
}
/*
Create your own methods to convert the models.
Replace the variables with whichever method is available to get the name:
(inputOption.productOption, inputOption.optionName)
(inputOption.productOption(), inputOption.optionName())
. (inputOption.getProductOption(), inputOption.getOptionName())
*/
private ProductMultipleOptionViewModel convertToMultipleOptionViewModel(
ProductMultipleOption inputOption) {
return new ProductMultipleOptionViewModel(
inputOption.productOption,
inputOption.optionName);
}
private ProductMultipleOption convertToMultipleOption(
ProductMultipleOptionViewModel inputOption) {
return new ProductMultipleOption(
inputOption.productOption,
inputOption.optionName);
}
/*
If the list you're getting is Optional<List<ProductOptionViewModel>>
and you want List<ProductMultipleOptionViewModel>
*/
List<ProductMultipleOptionViewModel> convertedFromOptionalList =
getProductMultipleOptionNull()
.stream()
.flatMap(Collection::stream)
.map(this::convertToMultipleOptionViewModel)
.toList();
/*
If the list you're getting is List<ProductOptionViewModel>
and you want List<ProductMultipleOptionViewModel>
*/
List<ProductMultipleOptionViewModel> convertedFromNullableList = Optional
.ofNullable(getProductOptionViewModelList())
.stream()
.flatMap(Collection::stream)
.map(this::convertToMultipleOptionViewModel)
.toList();
/*
If for some reason you're trying to get the list as
Optional<List<ProductOptionViewModel>> you can wrap
them with Optional.of() :
*/
Optional<List<ProductMultipleOptionViewModel>> convertedFromOptionalList = Optional
.of(Optional.ofNullable(getProductOptionViewModelList())
.stream()
.flatMap(Collection::stream)
.map(this::convertToMultipleOptionViewModel)
.toList());
Optional<List<ProductMultipleOptionViewModel>> convertedFromNullableList = Optional
.of(getProductMultipleOptionNull()
.stream()
.flatMap(Collection::stream)
.map(this::convertToMultipleOptionViewModel)
.toList());

Sort list by multiple fields(not then compare) in java

Now I have an object:
public class Room{
private long roomId;
private long roomGroupId;
private String roomName;
... getter
... setter
}
I want sort list of rooms by 'roomId', but in the meantime while room objects has 'roomGroupId' greator than zero and has same value then make them close to each other.
Let me give you some example:
input:
[{"roomId":3,"roomGroupId":0},
{"roomId":6,"roomGroupId":0},
{"roomId":1,"roomGroupId":1},
{"roomId":2,"roomGroupId":0},
{"roomId":4,"roomGroupId":1}]
output:
[{"roomId":6,"roomGroupId":0},
{"roomId":4,"roomGroupId":1},
{"roomId":1,"roomGroupId":1},
{"roomId":3,"roomGroupId":0},
{"roomId":2,"roomGroupId":0}]
As shown above, the list sort by 'roomId', but 'roomId 4' and 'roomId 1' are close together, because they has the same roomGroupId.
This does not have easy nice solution (maybe I am wrong).
You can do this like this
TreeMap<Long, List<Room>> roomMap = new TreeMap<>();
rooms.stream()
.collect(Collectors.groupingBy(Room::getRoomGroupId))
.forEach((key, value) -> {
if (key.equals(0L)) {
value.forEach(room -> roomMap.put(room.getRoomId(), Arrays.asList(room)));
} else {
roomMap.put(
Collections.max(value, Comparator.comparing(Room::getRoomId))
.getRoomId(),
value
.stream()
.sorted(Comparator.comparing(Room::getRoomId)
.reversed())
.collect(Collectors.toList())
);
}
});
List<Room> result = roomMap.descendingMap()
.entrySet()
.stream()
.flatMap(entry -> entry.getValue()
.stream())
.collect(Collectors.toList());
If you're in Java 8, you can use code like this
Collections.sort(roomList, Comparator.comparing(Room::getRoomGroupId)
.thenComparing(Room::getRoomId));
If not, you should use a comparator
class SortRoom implements Comparator<Room>
{
public int compare(Room a, Room b)
{
if (a.getRoomGroupId().compareTo(b.getRoomGroupId()) == 0) {
return a.getRoomId().compareTo(b.getRoomId());
}
return a.getRoomGroupId().compareTo(b.getRoomGroupId();
}
}
and then use it like this
Collections.sort(roomList, new SortRoom());

Filter values from a list based on priority

I have a list of valid values for a type:
Set<String> validTypes = ImmutableSet.of("TypeA", "TypeB", "TypeC");
From a given list I want to extract the first value which has a valid type. In this scenario I would write something of this sort:
public class A{
private String type;
private String member;
}
List<A> classAList;
classAList.stream()
.filter(a -> validTypes.contains(a.getType()))
.findFirst();
However I would like to give preference to TypeA, i.e. if classAList has TypeA and TypeB, I want the object which has typeA. To do this one approach I've is:
Set<String> preferredValidTypes = ImmutableSet.of("TypeA");
classAList.stream()
.filter(a -> preferredValidTypes.contains(a.getType()))
.findFirst()
.orElseGet(() -> {
return classAList.stream()
.filter(a -> validTypes.contains(a.getType()))
.findFirst();
}
Is there a better approach?
filter list by type, order by type, collect to list, then just get first element
List<A> collect = classAList.stream()
.filter(a -> validTypes.contains(a.getType()))
.sorted(Comparator.comparing(A::getType))
.collect(Collectors.toList());
System.out.println(collect.get(0));
You can use a custom comparator like:
Comparator<A> comparator = (o1, o2) -> {
if (preferredValidTypes.contains(o1.getType()) && !preferredValidTypes.contains(o2.getType())) {
return 1;
} else if (!preferredValidTypes.contains(o1.getType()) && preferredValidTypes.contains(o2.getType())) {
return -1;
} else {
return 0;
}
};
to sort the list and then findFirst from that list with your conditiion.
i don't like the answers already given which use Comparator. Sorting is an expensive operation. You can do it with one walk through the list. Once you find a preferred value, you can break out, otherwise you continue to the end to find a valid.
In this case anyMatch can provide the possibility to break out from the stream processing:
MyVerifier verifier=new MyVerifier(validTypes,preferredValidTypes);
classAList.stream()
.anyMatch(verifier);
System.out.println("Preferred found:"+verifier.preferred);
System.out.println("Valid found:"+verifier.valid);
public static class MyVerifier implements Predicate<A> {
private Set<String> validTypes;
private Set<String> preferredValidTypes;
A preferred=null;
A valid=null;
public MyVerifier(Set<String> validTypes, Set<String> preferredValidTypes) {
super();
this.validTypes = validTypes;
this.preferredValidTypes = preferredValidTypes;
}
#Override
public boolean test(A a) {
if(preferred==null && preferredValidTypes.contains(a.getType())) {
preferred=a;
// we can stop because we found the first preferred
return true;
} else if(valid==null && validTypes.contains(a.getType())) {
valid=a;
}
return false;
}
}
One can, of course, define two lists, one with all valid types, and one with the preferred types.
However, here is another approach. Define one list, or actually, a Map, with the keys being the valid types, and the boolean values being whether the type is preferred.
Map<String, Boolean> validTypes = ImmutableMap.of(
"TypeA", false,
"TypeB", false,
"TypeC", true
);
Using AtomicReference
One option is the following:
AtomicReference<A> ref = new AtomicReference<>();
listOfAs.stream()
.filter(t -> validTypes.containsKey(t.getType()))
.anyMatch(t -> validTypes.get(ref.updateAndGet(u -> t).getType()));
AtomicReference now contains a preferred A if available, or another valid A, or if the stream is empty, then it contains null. This stream operation short-circuits if an A with a preferred type is found.
The drawback of this option is that it creates side-effects, which is discouraged.
Using distinct()
Another suggestion would be the following. It uses the same map structure, using a boolean to indicate which values are preferred. However, it does not create side effects.
Map<Boolean, A> map = listOfAs.stream()
.filter(t -> validTypes.containsKey(t.getType()))
.map(t -> new Carrier<>(validTypes.get(t.getType()), t))
.distinct()
.limit(2)
.collect(Collectors.toMap(Carrier::getKey, Carrier::getValue));
It works as follows.
filter discards any element that is not a valid type.
Then, each element is mapped to a Carrier<Boolean, A> instance. A Carrier is a Map.Entry<K, V> which implements its equals and hashCode methods regarding only the key; the value does not matter. This is necessary for the following step,
distinct(), which discards any duplicate element. This way, only one preferred type and only one valid type is found.
We limit the stream to have 2 elements, one for each boolean. This is because the stream, which is lazy, stops evaluating after both booleans are found.
At last, we collect the Carrier elements into a Map.
The map contains now the following elements:
Boolean.TRUE => A with a preferred type
Boolean.FALSE => A with a valid type
Retrieve the appropriate element using
A a = map.getOrDefault(true, map.get(false)); // null if not found
Well you have to take care into account that sorting is stable, that is equal elements will appear in the same order as the initial source - and you need that to correctly get the first element from that List<A> that will satisfy your requirement, thus:
String priorityType = "TypeB";
Stream.of(new A("TypeA", "A"),
new A("TypeB", "B"),
new A("TypeC", "C"))
.sorted(Comparator.comparing(A::getType, Comparator.comparing(priorityType::equals)).reversed())
.filter(x -> validTypes.contains(priorityType))
.findFirst()
.orElseThrow(RuntimeException::new);
In Java8 you can use streams:
public static Carnet findByCodeIsIn(Collection<Carnet> listCarnet, String codeIsIn) {
return listCarnet.stream().filter(carnet -> codeIsIn.equals(carnet.getCodeIsin())).findFirst().orElse(null);
}
Additionally, in case you have many different objects (not only Carnet) or you want to find it by different properties (not only by cideIsin), you could build an utility class, to ecapsulate this logic in it:
public final class FindUtils {
public static <T> T findByProperty(Collection<T> col, Predicate<T> filter) {
return col.stream().filter(filter).findFirst().orElse(null);
}
}
public final class CarnetUtils {
public static Carnet findByCodeTitre(Collection<Carnet> listCarnet, String codeTitre) {
return FindUtils.findByProperty(listCarnet, carnet -> codeTitre.equals(carnet.getCodeTitre()));
}
public static Carnet findByNomTitre(Collection<Carnet> listCarnet, String nomTitre) {
return FindUtils.findByProperty(listCarnet, carnet -> nomTitre.equals(carnet.getNomTitre()));
}
public static Carnet findByCodeIsIn(Collection<Carnet> listCarnet, String codeIsin) {
return FindUtils.findByProperty(listCarnet, carnet -> codeIsin.equals(carnet.getCodeIsin()));
}
}
If you have preferred valid types in other collection so you can follow this code.
Map<String,A> groupByType = classAList
.stream()
/* additional filter to grouping by valid types.*/
//.filter(a->validTypes.contains(a.getType()))
.collect(Collectors.toMap(A::getType, Function.identity(),(v1, v2)->v1));
then use:
A result = preferredValidTypes
.stream()
.map(groupByType::get)
.findFirst()
.orElseThrow(RuntimeException::new);
or just group by preferred valid types
A result2 = classAList
.stream()
.filter(a -> preferredValidTypes.contains(a.getType()))
.collect(Collectors.toMap(A::getType, Function.identity(), (v1, v2) -> v1))
.entrySet()
.stream()
.findFirst()
.map(Map.Entry::getValue)
.orElseThrow(RuntimeException::new);

Converting to streams

I'd like to convert the following code, which breaks from the outer loop, into Java 8 Streams.
private CPBTuple getTuple(Collection<ConsignmentAlert> alertsOnCpdDay)
{
CPBTuple cpbTuple=null;
OUTER:
for (ConsignmentAlert consignmentAlert : alertsOnCpdDay) {
List<AlertAction> alertActions = consignmentAlert.getAlertActions();
for (AlertAction alertAction : alertActions) {
cpbTuple = handleAlertAction(reportDTO, consignmentId, alertAction);
if (cpbTuple.isPresent()) {
break OUTER;
}
}
}
return cpbTuple;
}
Every answer here uses flatMap, which until java-10 is not lazy. In your case that would mean that alertActions is traversed entirely, while in the for loop example - not. Here is a simplified example:
static class User {
private final List<String> nickNames;
public User(List<String> nickNames) {
this.nickNames = nickNames;
}
public List<String> getNickNames() {
return nickNames;
}
}
And some usage:
public static void main(String[] args) {
Arrays.asList(new User(Arrays.asList("one", "uno")))
.stream()
.flatMap(x -> x.getNickNames().stream())
.peek(System.out::println)
.filter(x -> x.equalsIgnoreCase("one"))
.findFirst()
.get();
}
In java-8 this will print both one and uno, since flatMap is not lazy.
On the other hand in java-10 this will print one - and this is what you care about if you want to have your example translated to stream-based 1 to 1.
Something along the lines of this should suffice:
return alertsOnCpdDay.stream()
.flatMap(s-> s.getAlertActions().stream())
.map(s-> handleAlertAction(reportDTO, consignmentId, s))
.filter(s-> s.isPresent())
.findFirst().orElse(null);
That said, a better option would be to change the method return type to Optional<CPBTuple> and then simply return the result of findFirst(). e.g.
private Optional<CPBTuple> getTuple(Collection<ConsignmentAlert> alertsOnCpdDay) {
return alertsOnCpdDay.stream()
.flatMap(s-> s.getAlertActions().stream())
.map(s-> handleAlertAction(reportDTO, consignmentId, s))
.filter(s-> s.isPresent())
.findFirst();
}
This is better because it better documents the method and helps prevent the issues that arise when dealing with nullity.
Since you break out of the loops upon the first match, you can eliminate the loops with a Stream with flatMap, which returns the first available match:
private CPBTuple getTuple(Collection<ConsignmentAlert> alertsOnCpdDay) {
return alertsOnCpdDay.stream()
.flatMap(ca -> ca.getAlertActions().stream())
.map(aa -> handleAlertAction(reportDTO, consignmentId, aa))
.filter(CPBTuple::isPresent)
.findFirst()
.orElse(null);
}
Try this out,
alertsOnCpdDay.stream()
.map(ConsignmentAlert::getAlertActions)
.flatMap(List::stream)
.map(alertAction -> handleAlertAction(reportDTO, consignmentId, alertAction))
.filter(CPBTuple::isPresent)
.findFirst().orElse(null);

Categories