`
sillycat
  • 浏览: 2560766 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

New Things in Java8

    博客分类:
  • JAVA
 
阅读更多
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

    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...

    Manning Java Reflection In Action

    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+9+Programming+By+Example-Packt+Publishing(2017).pdf

    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 ...

    mastering.java

    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.Projects.2nd.2018.epub

    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-...

    Learn Objective-C for Java Developers 2009 (Apress)

    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 ...

    Java 7 Concurrency Cookbook

    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 ...

    Java邮件开发Fundamentals of the JavaMail API

    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...

    Fundamentals of Computer Graphics With Java

    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 ...

    《Java Servlet编程(第二版)》英文版 chm (含源码)

    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 ...

    Packt.Publishing.Service.Oriented.Java.Business.Integration.Feb.2008

    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...

    Docker-in-Action.pdf

    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 ...

    某公司的JAVA笔试题

    &gt; 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 ...

    C# 5.0 All-in-One For Dummies

    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...

    Apress.Pro.ASPNET3.5.in.C.Sharp.2008.Includes.Silverlight2.3rd.Edition.Jan.2009 part 3-1

    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 ...

    Apress.Pro.ASPNET3.5.in.C.Sharp.2008.Includes.Silverlight2.3rd.Edition.Jan.2009 part 3-3

    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 ...

    Apress.Pro.ASPNET3.5.in.C.Sharp.2008.Includes.Silverlight2.3rd.Edition.Jan.2009 part 3-2

    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 ...

Global site tag (gtag.js) - Google Analytics