package sunday;
/*
idea
Lambda
接口组成更新
引用方法
函数式接口
stream流
*/
public class
Demo
{
public static void main(String[] args) {
//lambda();
//newInterface();
reference();
}//main
public static void
idea()
{
/*
使用Lambda必须有(仅有一个抽象方法的接口);
匿名内部类与Lambda区别:
所需类型不同
使用限制不同
实现原理不同
一接口一方法可省参数类型,有且仅有一参数可省小括号,仅有一语句可省大括号分号return
usePeg1(x->System.out.println(x));
usePeg1((int x)->{
return x+y;
});
*/
}
public static void
lambda()
{
//匿名内部类
usePeg(new Peg() {
@Override
public void fly(String s) {
System.out.println(s);
System.out.println(“飞瀑流”);
}
});
//Lambda带参无返回值
usePeg((String s)->{
System.out.println(s);
System.out.println(“飞瀑流”);
});
//Lambda带参带返回值
usePeg1((int x,int y)->{
return x+y;
});
}
//接口组成更新
public static void
newInterface()
{
Tools tools = new Tools();
tools.show();
tools.show1();
Peg2.show3();
}
//方法引用
public static void
reference()
{
//引用类的静态方法
usePeg(System.out::println);
//引用对象的成员方法
Tools tools = new Tools();
usePeg(tools::show1);
//引用类的构造方法
usePeg3(Tools::new);
}
//函数式接口
public static void
functionInerface()
{
//supplier生产型接口
int[] arr={21,12,23,34};
int supplier = supplier(() -> {
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
});
System.out.println(supplier);
//consumer消费型接口
String[] stringArray={“罗宾,28”,“汉库克,25”,“佩罗娜,23”};
consumer(stringArray,s -> System.out.print(“name:”+s.split(",")[0]+","),
s -> System.out.println(“age:”+Integer.parseInt(s.split(",")[1])));
//predicate判断型接口
ArrayList arrayList = predicate(stringArray, s -> s.split(",")[0].length() >2,
s -> Integer.parseInt(s.split(",")[1])>23);
for(String s:arrayList){
System.out.println(s);
}
//function处理型接口
String s=“robin,28”;
function(s, s1 -> s1.split(",")[1], Integer::parseInt,integer -> integer+1);
}
public static void
stream()
{
ArrayList arrayList = new ArrayList<>();
arrayList.add(1);
arrayList.add(2);
arrayList.add(3);
arrayList.add(4);
arrayList.add(5);
ArrayList arrayList1 = new ArrayList<>();
arrayList1.add(1);
arrayList1.add(2);
arrayList1.add(3);
arrayList1.add(4);
arrayList1.add(5);
Stream.concat(arrayList.stream().filter(s->s>1).limit(2),
arrayList1.stream().filter(s->s<5).skip(3)).distinct().sorted()
.map(Tools::new).forEach(tools -> System.out.println(tools.getAge()));
//转换
ArrayList strings = new ArrayList<>();
strings.add(“4”);
strings.add(“9”);
int sum = strings.stream().mapToInt(Integer::parseInt).sum();
System.out.println(sum);
//收集
Stream limit = arrayList.stream().filter(s -> s > 1).limit(3);
List list = limit.collect(Collectors.toList());
//记录
long count = arrayList.stream().filter(s -> s > 1).count();
System.out.println(count);
}
//Lambda带参无返回值
public static void
usePeg(Peg peg)
{
peg.fly(“纸绘武身”);
}
//Lambda带参带返回值
public static void
usePeg1(Peg1 peg1)
{
int num=peg1.num(13,13);
System.out.println(num);
}
//引用构造方法
public static void
usePeg3(Peg3 peg3)
{
Tools tools = peg3.peg3(4);
System.out.println(tools.getAge());
}
public static int
supplier(Supplier supplier)
{
return supplier.get();
}
public static void
consumer(String[] strArray, Consumer consumer,Consumer consumer1)
{
for(String str:strArray){
consumer.andThen(consumer1).accept(str);
}
}
public static ArrayList
predicate(String[] stringArray, Predicate predicate,Predicate predicate1)
{
ArrayList arrayList = new ArrayList();
for(String string:stringArray){
if(predicate.and(predicate1).test(string)) {
arrayList.add(string);
}
}
return arrayList;
//predicate.or(predicate1)或
//predicate.test(string)
//predicate.negate().test(string)非
}
public static void
function(String s, Function<String,String> function,Function<String,Integer> function1,Function<Integer,Integer> function2)
{
int i = function.andThen(function1).andThen(function2).apply(s);
System.out.println(i);
}
}//end
package sunday;
public class
Tools
implements Peg2{
private int age;
public Tools(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Tools() {
}
@Override
public void show() {
System.out.println(“纸绘武身”);
}
public void show1(String s){
System.out.println(s);
}
}
package sunday;
public interface
Peg
{
void fly(String s);
}
package sunday;
public interface
Peg1
{
int num(int x,int y);
}
package sunday;
public interface
Peg2
{
void show();
default void show1(){
show4();
System.out.println(“默认方法”);
}
static void show3(){
System.out.println(“静态方法”);
}
private void show4(){
System.out.println(“私有方法”);
}
}
package sunday;
public interface
Peg3
{
Tools peg3(int age);
}