boolean isSuccess = aMap.entrySet().stream().anyMatch(entry -> {
AKey aKey= entry.getKey();
BValue bValue = bMap.get(aKey);
if (bValue == null) {
return false;
}
AValue aValue = entry.getValue();
if (compareDetail(aValue, bValue)) {
return false;
}
return true;
}
);
this code always only loop one time, how can i loop all elements then return true when two if blocks false?
It seems you need to try allMatch instead.
Related
Spring is not caching my function when i am using default key such as -
#PostMapping("getDashboardDataNew")
#Cacheable(value="myDash")
public DashboardDto getHomeDashboardDataNew(#RequestBody DashboardRequest dashboardRequest) {
LOGGER.info(" Get All the Dashboard Information : ");
//code
return dashboardDto;
}
But when I am providing custom key using sPEL its caching the response eg.
#PostMapping("getDashboardDataNew")
#Cacheable(value="myDash", key="#dashboardRequest.level")
public DashboardDto getHomeDashboardDataNew(#RequestBody DashboardRequest dashboardRequest) {
LOGGER.info(" Get All the Dashboard Information : ");
//code
return dashboardDto;
}
The request payload is always-
{"fromDate":null,"toDate":null,"theme":null,"activity":null,"level":1,"levelValue":null,"state":null,"district":null}
Even after auto generating equals and hashcode using eclipse the spring is not caching the value. Below are the auto generated codes
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((activity == null) ? 0 : activity.hashCode());
result = prime * result + ((fromDate == null) ? 0 : fromDate.hashCode());
result = prime * result + ((level == null) ? 0 : level.hashCode());
result = prime * result + ((levelValue == null) ? 0 : levelValue.hashCode());
result = prime * result + ((organizer == null) ? 0 : organizer.hashCode());
result = prime * result + ((theme == null) ? 0 : theme.hashCode());
result = prime * result + ((toDate == null) ? 0 : toDate.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
DashboardRequest other = (DashboardRequest) obj;
if (activity == null) {
if (other.activity != null)
return false;
} else if (!activity.equals(other.activity))
return false;
if (fromDate == null) {
if (other.fromDate != null)
return false;
} else if (!fromDate.equals(other.fromDate))
return false;
if (level == null) {
if (other.level != null)
return false;
} else if (!level.equals(other.level))
return false;
if (levelValue == null) {
if (other.levelValue != null)
return false;
} else if (!levelValue.equals(other.levelValue))
return false;
if (organizer == null) {
if (other.organizer != null)
return false;
} else if (!organizer.equals(other.organizer))
return false;
if (theme == null) {
if (other.theme != null)
return false;
} else if (!theme.equals(other.theme))
return false;
if (toDate == null) {
if (other.toDate != null)
return false;
} else if (!toDate.equals(other.toDate))
return false;
return true;
}
I am not changing the request payload.
By default when no key is supplied, Spring cache relies on SimpleKeyGenerator which relies on hashcode of parameters to generate the key. You can check this link.
I figured out what went wrong here.
I was changing one of the property of the request payload somewhere inside the function eg.
dashboardRequest.setLevel(dashboardRequest.getLevel() + 1);
And as spring cache AOP puts the value in cache after the method execution is was using the modified object instead of value provided in param effectively making my key different from the key that would have generated by request payload. Hope this helps someone.
Is there a way to rewrite this using Optional and lambdas in a more succinct and clear way?
private boolean pricingIndicator(AvgBuySellPriceTerm avgBuySellPriceTerm){
if(avgBuySellPriceTerm == null){
return false;
}else{
if(avgBuySellPriceTerm.getIndicator()!= null && ! avgBuySellPriceTerm.getIndicator().isEmpty()){
return true;
}else{
return false;
}
}
}
Here's a suggestion with Optional:
private boolean pricingIndicator(AvgBuySellPriceTerm avgBuySellPriceTerm){
return Optional.ofNullable(avgBuySellPriceTerm)
.map(AvgBuySellPriceTerm::getIndicator)
.map(i -> !i.isEmpty()) // return true if getIndicator
// is not empty
.orElse(false);
}
The following should do it, using an Optional::ofNullable and the classic map, filter and isPresent methods
private boolean pricingIndicator(AvgBuySellPriceTerm avgBuySellPriceTerm){
return Optional.ofNullable(avgBuySellPriceTerm)
.map(AvgBuySellPriceTerm::getIndicator)
.filter(ind -> !ind.isEmpty())
.isPresent();
}
!Optional.ofNullable(t)
.map(AvgBuySellPriceTerm::getIndicator)
.map(List::isEmpty)
.orElse(true);
Not sure this is more readable though.
Again, not using lambdas here but keeping it readable. The first if statement can be omitted, so it can all boil down to:
private boolean pricingIndicator(AvgBuySellPriceTerm avgBuySellPriceTerm){
if(avgBuySellPriceTerm != null && avgBuySellPriceTerm.getIndicator()!= null && !avgBuySellPriceTerm.getIndicator().isEmpty()){
return true;
}
return false;
}
I have a Map in Java 8.
I need to check if the list that comprises the map value is empty or null and return the result .
I have tried likewise with no luck
public boolean mapValuesEmpty() {
boolean result = true;
for (Entry<Integer, List<SomeObjectName>> entry : eventLogsMap.entrySet()) {
if (entry.getValue() != null) {
result = false;
break;
}
}
return result;
}
Many thanks
boolean result = eventLogsMap
.values()
.stream()
.anyMatch(list -> list != null && !list.isEmpty())
I have created an ArrayList and I am looking to use a boolean method to add an element to the ArrayList.
private static Arraylist <Bicycle> bikelist = new Arraylist<Bicycle>();
public boolean add(Bicycle bicycle)
{
if( bikelist.size() != -1)
return true;
bikelist.add(bicycle);
}
return false;
Would this be plausible? I'm not quite sure how boolean methods work? Do they have to contain an if statement to return true or false?
Using: public boolean add(Object obj): Adds an element obj to the Arraylist. If the element is added successfully, this method returns true
Depends on what you want to return the boolean value for.
For whether the add operation succeeded: the only reason why adding an Object to an ArrayList would fail is due to an OutOfMemoryException AFAIK. Then you would do:
public boolean add(Object obj) {
try {
list.add(obj);
} catch (OutOfMemoryException e) {
return false;
}
return true;
}
If you want to return false if the list already contains the object, then:
public boolean add(Object obj) {
if (list.contains(obj)) {
return false;
}
list.add(obj);
return true;
}
Note that ArrayList<T>.contains(T) checks for object equality using Object.equals(Object). Thus you need to override equals() for your custom class.
#Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Bicycle other = (Bicycle) obj;
if (other.owner != this.owner) {
return false;
}
if (this.model != other.model) {
return false;
}
return true;
}
Can someone explain this solution to me? A friend helped me, but he just wrote it all out and didn't explain it. Now, I'm really confused :(
_.indexOf = function(array, target){
var result = -1;
_.each(array, function(item, index) {
if (item === target && result === -1) {
result = index;
}
});
return result;
};
return result;
};
The function traverses through all the elements of the array and returns index of the first element that is equal to target. The code could also look like this:
_.indexOf = function(array, target){
var result = -1;
_.each(array, function(item, index) {
if (item === target) {
result = index;
return false;
}
});
return result;
}