首页 / JAVA / JAVA8实战第一章读书笔记
JAVA8实战第一章读书笔记
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了JAVA8实战第一章读书笔记,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3816字,纯文字阅读大概需要6分钟。
内容图文
![JAVA8实战第一章读书笔记](/upload/InfoBanner/zyjiaocheng/745/d8b54ac4c82647389228335b697b5143.jpg)
通过行为参数化来传递代码
#### example:
假如说你要编写一个筛选绿苹果的函数,你可能会这么写:
import java.util.ArrayList;
import java.util.List;
public class test {
public static List<apple> filter(List<apple> inventory){
List<apple> result = new ArrayList<apple>();
for(apple a:inventory){
if("green".equals(a.getColor())){
result.add(a);
}
}
return result;
}
}
如果这个时候需求改为筛选出质量大于150克的苹果呢?加入需求又改为筛选出红色和绿色而且质量大于150克的苹果呢?
可以根据选择标准建模,我们可以实现一个谓词接口,来返回boolean值.
public interface ApplePredicate{
boolean test(apple apple);
}
public static class AppleRedAndHeavyPredicate implements ApplePredicate{
public boolean test(apple apple){
return "red".equals(apple.getColor())&&apple.getWeight()>150;
}
}
public static List<apple> filter(List<apple> inventory,ApplePredicate p){
List<apple> result = new ArrayList<apple>();
for(apple a:inventory){
if(p.test(a)){
result.add(a);
}
}
return result;
}
那么就可以new 一个谓词对象来做filter方法
List<apple> result = filter(inventory,new AppleRedAndHeavyPredicate());
exam:
编写一个 prettyPrintApple 方法,它接受一个 Apple 的 List ,并可以对它参数化,以
多种方式根据苹果生成一个 String 输出(有点儿像多个可定制的 toString 方法)。例如,你
可 以 告 诉 prettyPrintApple 方 法 , 只 打 印 每 个 苹 果 的 重 量 。 此 外 , 你 可 以 让
prettyPrintApple 方法分别打印每个苹果,然后说明它是重的还是轻的。解决方案和我们
前面讨论的筛选的例子类似。为了帮你上手,我们提供了 prettyPrintApple 方法的一个粗
略的框架
import java.nio.charset.IllegalCharsetNameException;
import java.util.ArrayList;
import java.util.List;
public class printApple {
public interface Predicate{
String accept(apple a);
}
public static class printFancyApple implements Predicate{
public String accept(apple a){
String isHeavy = a.getWeight()>150?"heavy":"light";
return "A "+isHeavy+" "+a.getColor()+" apple";
}
}
public class printSimpleApple implements Predicate{
public String accept(apple a){
return "A "+a.getWeight()+" g "+a.getColor()+" apple";
}
}
public static void printApple(List<apple> Inventory,Predicate p){
for(apple a: Inventory){
String output = p.accept(a);
System.out.println(output);
}
}
public static void main(String [] args)throws Exception{
apple apple1 = new apple("red",160);
apple apple2 = new apple("green",80);
List<apple> inventory = new ArrayList<apple>();
inventory.add(apple1);
inventory.add(apple2);
printApple(inventory,new printFancyApple());
}
}
使用匿名类
那么你就可以实现随时重写filter方法了
List<apple> redApples = filter(inventory, new ApplePredicate() {
@Override
public boolean test(apple apple) {
return "red".equals(apple.getColor());
}
});
使用匿名函数
List<apple> res = filter(inventory,(apple a) -> "red".equals(a.getColor()));
这样更简洁了.
public interface Predicate<T>{
boolean test(T t);
}
public static <T> List<T> filter(List<T>list,Predicate<T>p){
List<T> result = new ArrayList<>();
for(T t:list){
if(p.test(t)){
result.add(t);
}
}
return result;
}
public static void main(String [] args)throws Exception{
apple apple1 = new apple("red",160);
apple apple2 = new apple("green",80);
List<apple> inventory = new ArrayList<apple>();
inventory.add(apple1);
inventory.add(apple2);
List<Integer> numbers = Arrays.asList(1,2,3,4);
List<apple> apples = filter(inventory,(apple a) -> "red".equals(a.getColor()));
List<Integer> evenNums = filter(numbers,(i)-> i%2==0);
}
inventory.sort(new Comparator<apple>(){
@Override
public int compare(apple apple, apple t1) {
return apple.getWeight().compareTo(t1.getWeight());
}
});
inventory.sort((apple a1,apple a2) -> a1.getWeight().compareTo(a2.getWeight()));
Lambda表达式
基本语法:
(parameters) -> expression
(parameters) -> {return xxx;}
在哪里以及如何使用Lambda?
在一些函数式接口可以用lambda函数替代.
函数式接口如下:
public interface Comparator<T>{
int compare(T o1,T o2);
}
public interface Runable{
void run();
}
public interface Callable <V>{
V call();
}
函数描述符
内容总结
以上是互联网集市为您收集整理的JAVA8实战第一章读书笔记全部内容,希望文章能够帮你解决JAVA8实战第一章读书笔记所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。