Qi

Cogito ergo sum

Lambda表达式

stream().forEach()循环处理

List<String> list = com.google.common.collect.Lists.newArrayList();
list.add("1");
list.add("2");
list.add("3");

list.stream().forEach(string ->{
System.out.println(string);
});

stream().map()处理集合,collect(Collectors.toList())结果输出为List

List<String> list1 = Lists.newArrayList();
list1.add("1");
list1.add("2");
list1.add("3");

List<String> list2 = list1.stream().map(string -> {
return “stream().map()处理之后:” + string;
}).collect(Collectors.toList());

System.out.println(list2.toString());

stream().filter()过滤集合

List<String> list1 = Lists.newArrayList();
list1.add("1");
list1.add("1");
list1.add("2");
list1.add("3");

List<String> list2 = list1.stream().filter(s -> !s.equals(“1”)).collect(Collectors.toList());
System.out.println(list2.toString());

函数接口

一个只包含一个方法的接口,可以使用Lambda表达式作为参数

new Thread(() -> System.out.println("Hello World!"));

public interface Interface1 {
void test();
}

private void func(Interface1 interface1) {
interface1.test();
}

func(() -> System.out.println(“Hello World”));

//有参数,参数知名类型更规范
func((Integer x) -> System.out.println(“Hello World” + x));

//有参数,有返回值
func((Integer x) -> {
System.out.println(“Hello World” + x);
return true;
});

EL-Spring 表达式语言,支持xml和注解中使用表达式,类似JSP 的EL 表达式,可以实现普通文件、网址、配置文件、系统环境变量的注入

示例

@PropertySource("classpath:application.properties")

@Value(“This is common string”) // 注入普通字符串
private String normal;

@Value(“#{systemProperties[‘os.name’]}”) // 注入操作系统属性
private String osName;

@Value(“#{T(java.lang.Math).random()*100.0}”) // 注入表达式结果
private double randomNumber;

@Value(“#{anotherService.property}”) // 注入其他Bean属性
private String propfromAnother;

@Value(“#{T(com.demo.el.spring_el_demo.DemoService).getCalc()*100}”) // 注入类static方法结果,支持运算处理
private double result;

@Value(“classpath:test.txt”) // 注入文件资源
private Resource testFile;

@Value(“http://www.baidu.com“) // 注入网址资源
private Resource testUrl;

@Value(“${book.name}”) // 注入配置文件
private String bookName;

@Autowired
private Environment environment;

0%