Java code TIPS
Lambda knowledge
Main functional interface
Interface name |
Method |
Overview |
Function<T,R> |
R apply(T t) |
Argument T,Return value R |
Consumer<T> |
void accept(T t) |
Argument T,No return value |
Predicate<T> |
boolean test(T t) |
Argument T,Return value boolean |
Supplier<T> |
T get() |
No arguments,Return value T |
UnaryOperator<T> |
T apply(T t) |
Argument T,Return value T |
BinaryOperator<T> |
T apply(T t1, T t2) |
2 arguments T,Return value R |
BiFunction<T,U,R> |
R apply(T t, U u) |
Arguments T and U,Return value R |
Intermediate operation
Method name |
Overview |
filter |
Elements that match the conditions |
distinct |
Elements without duplication |
limit |
Specified number of elements |
skip |
Elements excluding the specified number |
map |
Elements converted by some processing |
flatmap |
|
sorted |
sort |
peek |
|
Termination operation
Return type |
Method name |
Overview |
Similar |
boolean |
anyMatch |
Did it match any of the elements |
allMatch |
R |
collect |
Result of variable reduction operation |
|
long |
count |
Number of elements |
|
Optional<T> |
findAny |
Either element |
findFirst |
void |
forEach |
Processing for elements that do not return a return value |
|
Optional<T> |
max |
Maximum element |
min |
T |
reduce |
Cumulative processing result |
|
List related
Sort and combine
list.stream().sorted(Comparator.reverseOrder()).reduce("", (all,s) -> all + "\r\n" + s);
list.sort(Comparator.reverseOrder());
String.join("\r\n", list);
Unique sort
List<String> list = new ArrayList<String>();
list.add("A2:100");
list.add("B:101");
list.add("C:100");
list.add("A1:100");
list.stream().sorted((s1, s2) -> {
int ret = s1.substring(s1.length() -3).compareTo(s2.substring(s2.length() -3));
if(ret == 0) {
return s1.compareTo(s2);
}
return ret * -1;
}).forEach(System.out::println);
--result
B:101
A1:100
A2:100
C:100
Output the contents of List
list.forEach(System.out::println);
Serial number creation