阅读笔记
On Java 8 本书的原作者是Java编程第四版的作者 Bruce Eckel 著作的,基于JDK1.8。
新的改变 【On Java 8】JDK1.8新增加的特性
- 允许在接口中有默认方法实现
- Lambda表达式
- 函数式接口
- 方法和构造函数引用
- Lambda的范围
- 内置函数式接口
- Streams
- 增加了 检查列表 功能。
- Map
- 时间日期API
- Annotations
classes to solve this problem, the basic types of which are List, Set, Queue,
and Map. These types are also known as container classes, but I shall use the
term that the Java library uses. Collections provide sophisticated ways to hold objects, and solve a surprising number of
problems
Among their other characteristics—Set, for example, holds only one
object of each value, and Map is an associative array (关联数组)
that lets you associate objects with other objects—the Java collection classes will
automatically resize themselves. So, unlike arrays, you can put in any
number of objects without worrying about how big the collection
should be.
Set:独一无二的集合 你可以,用来保存每一个Object
Map:关联数组, [key, value], 你可以将一个对象和其它对象关联。
不像数组,无法动态的对其进行拓展。你可以往集合中随意的添加元素而不必担心它的容量。
Generics and Type Safe Collections
在Java 5的编译器中,它允许你插入一个不正确的类型进入集合。但是如果取出来的时候,需要强制转型。
import java.util.*;
class Apple {
private static long counter;
private final long id = counter++;
public long id() { return id;
}
}
class Orange {}
public class ApplesAndOrangesWithoutGenerics {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
ArrayList apples = new ArrayList();
for(int i = 0;
i < 3;
i++)
apples.add(new Apple());
// No problem adding an Orange to apples:
apples.add(new Orange());
for(Object apple : apples) {
((Apple) apple).id();
// Orange is detected only at run time
}
}
}
/* Output:
___[ Error Output ]___
Exception in thread "main"
java.
再比方说,你往一个篮子(Arraylist)装(add) 苹果(Object),通过get(index)获取其中一个,就像操作没有方括号的数组一样。
我们将另外一个类型Oranges也装进这个篮子,然后将它们全部取出。通常,Java会给你一个warning的提示,表示这个list没有使用泛型,我们通过@SuppressWarnings(“unchecked”)注解告诉编译器不要给我警告。
Streams
import ssm.pojo.Apple;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static java.util.Comparator.comparing;
public class StreamTest {public static void main(String[] args) {
Trader raoul = new Trader("Raoul", "Cambridge");
Trader mario = new Trader("Mario","Milan");
Trader alan = new Trader("Alan","Cambridge");
Trader brian = new Trader("Brian","Cambridge");
List transactions = Arrays.asList(
new Transaction(brian, 2011, 300),
new Transaction(raoul, 2012, 1000),
new Transaction(raoul, 2011, 400),
new Transaction(mario, 2012, 710),
new Transaction(mario, 2012, 700),
new Transaction(alan, 2012, 950)
);
List newTransactionList = transactions.stream().filter( i -> i.getYear() == 2011)
.sorted((n1, n2) -> n1.compareTo(n2)).collect(Collectors.toList());
newTransactionList.forEach(d -> System.out.println(d.getTrader().getName()));
List list = new ArrayList<>();
list.add(raoul);
list.add(mario);
list.add(alan);
list.add(brian);
System.out.println("=======交易员都在哪些不同的城市工作过========");
List> cities = transactions.stream().map(transaction -> transaction.getTrader().getCity()).distinct().collect(Collectors.toList());
cities.forEach(d -> System.out.println(d));
System.out.println("=======查找所有来自于剑桥的交易员,并按姓名排序========");
List traderOfCambridge = transactions.stream().map(Transaction::getTrader).
filter(t -> t.getCity().equals("Cambridge")).
distinct().sorted(comparing(Trader::getName)).collect(Collectors.toList());
traderOfCambridge.forEach(t -> System.out.println(t.getName()));
System.out.println("=======返回所有交易员的姓名字符串,按字母顺序排序========");
List> allTraderList = transactions.stream().map(Transaction::getTrader).
map(Trader::getName).distinct().collect(Collectors.toList());
allTraderList.forEach(t -> System.out.println(t));
System.out.println("=======有没有交易员是在米兰工作的?========");
List milanTraderList = transactions.stream().map
(Transaction::getTrader).filter(t -> t.getCity().equals("Milan")).distinct().collect(Collectors.toList());
milanTraderList.forEach(t -> System.out.println(t.getName()));
System.out.println("=======打印生活在剑桥的交易员的所有交易额========");
int sum = transactions.stream().filter(t -> t.getTrader().
getCity().equals("Cambridge")).map(Transaction::getValue).reduce(0, Integer::sum);
System.out.println(sum);
System.out.println("=======所有交易中,最高的交易额是多少?========");
int maxValue = https://www.it610.com/article/transactions.stream().map(Transaction::getValue).reduce(0, Integer::max).intValue();
System.out.println(maxValue);
System.out.println("=======找到交易额最小的交易?========");
int minValue = https://www.it610.com/article/transactions.stream().map(Transaction::getValue).reduce(Integer::min).get().intValue();
System.out.println(minValue);
int n = transactions.stream().mapToInt(Transaction::getValue).sum();
System.out.println("-------->>>>>>>" + n);
}
}
未完待续
推荐阅读
- Java|Java基础——数组
- 人工智能|干货!人体姿态估计与运动预测
- java简介|Java是什么(Java能用来干什么?)
- Java|规范的打印日志
- Linux|109 个实用 shell 脚本
- 程序员|【高级Java架构师系统学习】毕业一年萌新的Java大厂面经,最新整理
- Spring注解驱动第十讲--@Autowired使用
- SqlServer|sql server的UPDLOCK、HOLDLOCK试验
- jvm|【JVM】JVM08(java内存模型解析[JMM])
- 技术|为参加2021年蓝桥杯Java软件开发大学B组细心整理常见基础知识、搜索和常用算法解析例题(持续更新...)