New Things in Java8
1. Functional Interface
2. Lambda
public int sum(int x, int y){
return x + y;
}
(int x, int y) -> { return x + y; }
java8 can guess and decide the type
(x, y) -> { return x + y;}
The magic part is the comparator class
Comparator comparator = (str1,str2) -> {return s1.compareToIgnoreCase(s2);}
3. Function as first-class object
Function<String, String> upperfier = String::toUpperCase;
System.out.println(upperfier.apply(“Hello"));
Predicate
Set<String> knowNames = new HashSet<>();
knowNames.add(“sillycat");
knowNames.contains(“sillycat");
Predicate<String> isKnowName = knowNames::contains;
isKnowName.test(“sillycat");
4. More Static Methods
Public static <T, U extends Comparable<? super U>> Comparator<T> comparing(Function<? super T, ? extends U> keyExtractor){}
Compare Name
Comparator nameComparator = Comparator.comparing(Emloyee::getName);
Compare Salary
Comparator salaryComparator = Comparator.comparing(Employee::getSalary);
5. Stream
map, filter, reduce
//list sum
int result = list.stream().reduce(0, (x,y) -> x+y);
//list filter
int result = list.stream().filter(x -> x > 5).reduce(0, (x,y) -> x+y);
//list map
int result = list.stream().filter(x->x>5).map(x->x*x).mapToInt(x->x).sum();
Really Nice
//salary sum
int totalSalary = employees.stream().map(e->e.getSalary()).reduce(0,(x,y)->x+y);
double averageSalary = employees.stream().mapToInt(e->e.getSalary()).average().getAsDouble();
Just read these 2 blogs, java8 really contains a lot of nice things. Need to read the book when I begin to use a lot of that.
References:
相关推荐
Java Reflection in Action is unique in presenting a clear account of all the cool things you can do with reflection, and at the same time pro- viding the sound conceptual basis that developers need to...
Java Reflection in Action is unique in presenting a clear account of all the cool things you can do with reflection, and at the same time providing the sound conceptual basis that developers need to ...
Java drastically changed with the introduction of Java 8, and this change has been elevated to a whole new level with the new version, Java 9. Java has a well-established past, being more than 20 ...
Java is not a new programming language by any means; it has been around since 1991 and, to date, has proved to be the most popular, especially where web development is concerned. It is one of the ...
Java drastically changed with the introduction of Java 8, and this change has been elevated to a whole new level with the new version, Java 9 and then further with Java 10 and 11. Java has a well-...
Use all of the things in Java and Objective-C that are actually quite similar, like MVC design patterns Learn how to do all of it within Apple's powerful Xcode programming environment using Cocoa ...
When you work with a computer, you can do several things at once. You can hear music while you edit a document in a word processor and read your e-mail. This can be done because your operating system ...
things like the last mail received and calculate how many are new for you. So, when using the JavaMail API, if you want this type of information, you have to calculate it yourself. IMAP IMAP is a...
While practitioners should be aware of this basic material in a general way, there are other things that are more important for students on an introductory level to learn. These notes were written ...
Since I wrote the first edition of this book, servlets and the server-side Java platform have grown in popularity beyond everyone's wildest expectations. Adoption is pervasive. Web server vendors now ...
This is where the new Java API for Integration—Java Business Integration (JBI) seeks attention. JBI provides a collaboration framework which has standard interfaces for integration components and...
couple different Java images, and then Python images. Then my mind flashed back to that terrible day with Graphite. I popped over to the Docker Index (this was before Docker Hub) and did a quick ...
> The Internet of Things, also known as the sensor network, refers to a variety of information sensing devices and the Internet combined to form a huge network, will enable all of the items and ...
Covers debugging, errors, comparisons with C++ and Java, classes and arrays, variables, and more Includes access to a companion website with sample code and bonus materials Everything you need to make...
Before he started working for Microsoft, Mario was involved in several projects based on COM+ and DCOM with Visual Basic and Visual C++ as well as projects based on Java and J2SE. With Beta 2 of the ...
Before he started working for Microsoft, Mario was involved in several projects based on COM+ and DCOM with Visual Basic and Visual C++ as well as projects based on Java and J2SE. With Beta 2 of the ...
Before he started working for Microsoft, Mario was involved in several projects based on COM+ and DCOM with Visual Basic and Visual C++ as well as projects based on Java and J2SE. With Beta 2 of the ...