A simple azure storage Entity Resolver interface and it's implemented method:
EntityResolver<String> orderNumberResolver = new EntityResolver<String>() {
@Override
public String resolve(String partitionKey, String rowKey, Date timeStamp,
HashMap<String, EntityProperty> properties, String etag) {
return properties.get("SomeColumnName").getValueAsString();
}
};
Lambda of the above method will be :
EntityResolver<String> orderNumberResolver = (
partitionKey, rowKey, timeStamp, properties, etag
) -> properties.get("SomeColumnName").getValueAsString();
It's clear from above example that lambda's are smart enough to handle the type of method parameters according to their anonymous inner class thus it makes the implementation of overridden method easy. Hope this will be helpful.
Answer from Trilok Singh Devda on Stack OverflowA simple azure storage Entity Resolver interface and it's implemented method:
EntityResolver<String> orderNumberResolver = new EntityResolver<String>() {
@Override
public String resolve(String partitionKey, String rowKey, Date timeStamp,
HashMap<String, EntityProperty> properties, String etag) {
return properties.get("SomeColumnName").getValueAsString();
}
};
Lambda of the above method will be :
EntityResolver<String> orderNumberResolver = (
partitionKey, rowKey, timeStamp, properties, etag
) -> properties.get("SomeColumnName").getValueAsString();
It's clear from above example that lambda's are smart enough to handle the type of method parameters according to their anonymous inner class thus it makes the implementation of overridden method easy. Hope this will be helpful.
Try this (removing extra semi colon)
List roles = ldapTemplate.search(
baseDn, replaceFilter, sc,
(Attributes a) -> { return a.get("cn").get(); }
);
Collections.sort(commits, Comparator.comparing(GitCommit::getDate));
or even
commits.sort(Comparator.comparing(GitCommit::getDate));
In the first case, the method map requires Function. I assume it's wrong because of the Mapper. The code remains invalid as long as Mapper doesn't extend Function. This is a way to go:
interface Mapper<T, R> extends Function<T, R> {
default R map(T t) {
return apply(t);
}
}
myCollection.stream().map(new Mapper<String,String>() {
public String apply(String input) {
return new StringBuilder(input).reverse().toString();
}
});
The second case is correct and I assume your goal is to pack the anonymous function to:
A lambda expression:
Collections.sort(commits, (c1, c2) -> c1.getDate().compareTo(c2.getDate()));Or to a method reference:
Collections.sort(commits, Comparator.comparing(GitCommit::getDate));
Comparator#compareTo returns an int; while getTime is obviously long.
It would be nicer written like this:
.sort(Comparator.comparingLong(Message::getTime))
Lambda
The lambda can be seen as the shorthand of somewhat cumbersome anonymous class:
Java8 version:
Collections.sort(list, (o1, o2) -> o1.getTime() - o2.getTime());
Pre-Java8 version:
Collections.sort(list, new Comparator<Message>() {
@Override
public int compare(Message o1, Message o2) {
return o1.getTime() - o2.getTime();
}
});
So, every time you are confused how to write a right lambda, you may try to write a pre-lambda version, and see how it is wrong.
Application
In your specific problem, you can see the compare returns int, where your getTime returns long, which is the source of error.
You may use either method as other answer method, like:
Long.compare(o1.getTime(),o2.getTime())
Notice
- You should avoid using
-inComparator, which may causes overflow, in some cases, and crash your program.