关于injava8的信息

博主:adminadmin 2023-03-22 08:18:06 560

今天给各位分享injava8的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

关于Java 8,有什么好书推荐

推荐 Kishori Sharan 的 Java 系列书籍,除了用法介绍,还详细描述了设计原则,使用场景等等,内容全面而不单调,通俗易懂,无论是入门还是参考都很适用。

如果从 0 开始学习 Java 8,推荐:

《Beginning Java 8 Fundamentals - Kishori Sharan》800 多页

如果只想学习 Java 8 新特性,推荐:

《Beginning Java 8 Language Features - Kishori Sharan》 600 多页

《OnJava8》pdf下载在线阅读,求百度网盘云资源

《On Java 8》(Bruce Eckel)电子书网盘下载免费在线阅读

资源链接:

链接:

 提取码:aeh1    

书名:On Java 8

作者:Bruce Eckel

豆瓣评分:9.3

出版社:MindView LLC

出版年份:2017-6

内容简介:NOTE: Although the book refers to the web site , that site is not yet live.

The examples are freely available at .

On Java 8 is only available as an eBook, and only via Google Play Books. Any other source or delivery mechanism is illegitimate.

This book is far too large to publish as a single print volume, and my intent has always been to only publish it as an eBook. Color syntax highlighting for code listings is, alone, worth the cost of admission. Searchability, font resizing or text-to-voice for the vision-impaired, the fact you can always keep it with you---there are so many benefits to eBooks it's hard to name them all.

Anyone buying this book needs a computer to run the programs and write code, and the eBook reads nicely on a computer (I was also surprised to discover that it even reads tolerably well on a phone). However, the best reading experience is on a tablet computer. Tablets are inexpensive enough you can now buy one for less than you'd pay for an equivalent print version of this book (which, note, does not exist). It's much easier to read a tablet in bed (for example) than trying to manage the pages of a physical book, especially one this big. When working at your computer, you don't have to hold the pages open when using a tablet at your side. It might feel different at first, but I think you'll find the benefits far outweigh the discomfort of adapting.

I've done the research, and Google Play Books provides a very nice reading experience on every platform, including Linux and iOS devices. As an experiment, I've decided to try publishing exclusively through Google Books.

The free sample includes a complete table of contents.

作者简介:Bruce Eckel is the author of the multi-award-winning books Thinking in Java and Thinking in C++.

Java8的特性有哪些

1、函数式接口

Java 8 引入的一个核心概念是函数式接口(Functional Interfaces)。通过在接口里面添加一个抽象方法,这些方法可以直接从接口中运行。如果一个接口定义个唯一一个抽象方法,那么这个接口就成为函数式接口。同时,引入了一个新的注解:@FunctionalInterface。可以把他它放在一个接口前,表示这个接口是一个函数式接口。这个注解是非必须的,只要接口只包含一个方法的接口,虚拟机会自动判断,不过最好在接口上使用注解 @FunctionalInterface 进行声明。在接口中添加了 @FunctionalInterface 的接口,只允许有一个抽象方法,否则编译器也会报错。

java.lang.Runnable 就是一个函数式接口。

@FunctionalInterface

public interface Runnable {

public abstract void run();

}

2、Lambda 表达式

函数式接口的重要属性是:我们能够使用 Lambda 实例化它们,Lambda 表达式让你能够将函数作为方法参数,或者将代码作为数据对待。Lambda 表达式的引入给开发者带来了不少优点:在 Java 8 之前,匿名内部类,监听器和事件处理器的使用都显得很冗长,代码可读性很差,Lambda 表达式的应用则使代码变得更加紧凑,可读性增强;Lambda 表达式使并行操作大集合变得很方便,可以充分发挥多核 CPU 的优势,更易于为多核处理器编写代码;

Lambda 表达式由三个部分组成:第一部分为一个括号内用逗号分隔的形式参数,参数是函数式接口里面方法的参数;第二部分为一个箭头符号:-;第三部分为方法体,可以是表达式和代码块。语法如下:

1. 方法体为表达式,该表达式的值作为返回值返回。

(parameters) - expression

2. 方法体为代码块,必须用 {} 来包裹起来,且需要一个 return 返回值,但若函数式接口里面方法返回值是 void,则无需返回值。

(parameters) - { statements; }

例如,下面是使用匿名内部类和 Lambda 表达式的代码比较。

下面是用匿名内部类的代码:

button.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

System.out.print("Helllo Lambda in actionPerformed");

}

});

下面是使用 Lambda 表达式后:

button.addActionListener(

\\actionPerformed 有一个参数 e 传入,所以用 (ActionEvent e)

(ActionEvent e)-

System.out.print("Helllo Lambda in actionPerformed")

);

上面是方法体包含了参数传入 (ActionEvent e),如果没有参数则只需 ( ),例如 Thread 中的 run 方法就没有参数传入,当它使用 Lambda 表达式后:

Thread t = new Thread(

\\run 没有参数传入,所以用 (), 后面用 {} 包起方法体

() - {

System.out.println("Hello from a thread in run");

}

);

通过上面两个代码的比较可以发现使用 Lambda 表达式可以简化代码,并提高代码的可读性。

为了进一步简化 Lambda 表达式,可以使用方法引用。例如,下面三种分别是使用内部类,使用 Lambda 表示式和使用方法引用方式的比较:

//1. 使用内部类

FunctionInteger, String f = new FunctionInteger,String(){

@Override

public String apply(Integer t) {

return null;

}

};

//2. 使用 Lambda 表达式

FunctionInteger, String f2 = (t)-String.valueOf(t);

//3. 使用方法引用的方式

FunctionInteger, String f1 = String::valueOf;

要使用 Lambda 表达式,需要定义一个函数式接口,这样往往会让程序充斥着过量的仅为 Lambda 表达式服务的函数式接口。为了减少这样过量的函数式接口,Java 8 在 java.util.function 中增加了不少新的函数式通用接口。例如:

FunctionT, R:将 T 作为输入,返回 R 作为输出,他还包含了和其他函数组合的默认方法。

PredicateT :将 T 作为输入,返回一个布尔值作为输出,该接口包含多种默认方法来将 Predicate 组合成其他复杂的逻辑(与、或、非)。

ConsumerT :将 T 作为输入,不返回任何内容,表示在单个参数上的操作。

例如,People 类中有一个方法 getMaleList 需要获取男性的列表,这里需要定义一个函数式接口 PersonInterface:

interface PersonInterface {

public boolean test(Person person);

}

public class People {

private ListPerson persons= new ArrayListPerson();

public ListPerson getMaleList(PersonInterface filter) {

ListPerson res = new ArrayListPerson();

persons.forEach(

(Person person) -

{

if (filter.test(person)) {//调用 PersonInterface 的方法

res.add(person);

}

}

);

return res;

}

}

为了去除 PersonInterface 这个函数式接口,可以用通用函数式接口 Predicate 替代如下:

class People{

private ListPerson persons= new ArrayListPerson();

public ListPerson getMaleList(PredicatePerson predicate) {

ListPerson res = new ArrayListPerson();

persons.forEach(

person - {

if (predicate.test(person)) {//调用 Predicate 的抽象方法 test

res.add(person);

}

});

return res;

}

}

3、接口的增强

Java 8 对接口做了进一步的增强。在接口中可以添加使用 default 关键字修饰的非抽象方法。还可以在接口中定义静态方法。如今,接口看上去与抽象类的功能越来越类似了。

默认方法

Java 8 还允许我们给接口添加一个非抽象的方法实现,只需要使用 default 关键字即可,这个特征又叫做扩展方法。在实现该接口时,该默认扩展方法在子类上可以直接使用,它的使用方式类似于抽象类中非抽象成员方法。但扩展方法不能够重载 Object 中的方法。例如:toString、equals、 hashCode 不能在接口中被重载。

例如,下面接口中定义了一个默认方法 count(),该方法可以在子类中直接使用。

public interface DefaultFunInterface {

//定义默认方法 countdefault int count(){

return 1;

}

}

public class SubDefaultFunClass implements DefaultFunInterface {

public static void main(String[] args){

//实例化一个子类对象,改子类对象可以直接调用父接口中的默认方法 count

SubDefaultFunClass sub = new SubDefaultFunClass();

sub.count();

}

}

静态方法

在接口中,还允许定义静态的方法。接口中的静态方法可以直接用接口来调用。

例如,下面接口中定义了一个静态方法 find,该方法可以直接用 StaticFunInterface .find() 来调用。

public interface StaticFunInterface {public static int find(){

return 1;

}

}

public class TestStaticFun {

public static void main(String[] args){

//接口中定义了静态方法 find 直接被调用

StaticFunInterface.fine();

}

}

injava8的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、injava8的信息别忘了在本站进行查找喔。