函数式接口
函数式接口:有且仅有一个抽象方法的接口
@FunctionalInterface
public interface MyInterface {
void show();
}
public class MyInterfaceDemo {
public static void main(String[] args) {
MyInterface my=()-> System.out.println("函数式接口");
my.show();
}
}
public class RunnableDemo {
public static void main(String[] args) {
//匿名内部类
startThread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"线程启动了");
}
});
//lambda表达式
startThread(()->{
System.out.println(Thread.currentThread().getName()+"线程启动了");
});
}
private static void startThread(Runnable r){
new Thread(r).start();
}
}
public class ComparatorDemo {
public static void main(String[] args) {
ArrayList<String> arr=new ArrayList<>();
arr.add("cccc");
arr.add("aa");
arr.add("b");
arr.add("ddd");
System.out.println("排序前"+arr);
Collections.sort(arr,getComparator());
System.out.println("排序后"+arr);
}
private static Comparator<String> getComparator() {
//匿名内部类方式实现
/*Comparator<String> com=new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.length()-s2.length();
}
};
return com;*/
/* return new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.length()-s2.length();
}
};*/
/* return (String s1,String s2)->{
return s1.length()-s2.length();
};*/
return (s1,s2)->s1.length()-s2.length();
}
}
public class SupplierDemo {
public static void main(String[] args) {
/*String s=getString(()->{
return "林青霞";
});*/
String s=getString(()->"林青霞");
System.out.println(s);
Integer i=getInteger(()->30);
System.out.println(i);
}
//定义一个方法,返回一个整数数据
private static Integer getInteger(Supplier<Integer> sup) {
return sup.get();
}
//定义一个方法,返回字符串
private static String getString(Supplier<String> sup) {
return sup.get();
}
}
public class SupplierTest {
public static void main(String[] args) {
int [] arr={20,31,14,25};
int maxValue=getMax(()->{
int max=arr[0];
for (int i = 1; i < arr.length; i++) {
if(arr[i]>max){
max=arr[i];
}
}
return max;
});
System.out.println(maxValue);
}
private static int getMax(Supplier<Integer> sup){
return sup.get();
}
}
public class ConsumerDemo {
public static void main(String[] args) {
/* operatorString("林青霞",(String s)->{
System.out.println(s);
});*/
// operatorString("林青霞",s->System.out.println(s));
// operatorString("林青霞",System.out::println);
/* operatorString("林青霞",s -> {
System.out.println(new StringBuilder(s).reverse().toString());
});*/
operatorString("林青霞",s -> System.out.println(new StringBuilder(s).reverse().toString()));
}
//定义一个方法,消费一个字符串数据
private static void operatorString(String name, Consumer<String> con){
con.accept(name);
}
}
public class ConsumerDemo {
public static void main(String[] args) {
operatorString("林青霞",s-> System.out.println(s),s -> System.out.println(new StringBuilder(s).reverse().toString()));
}
//定义一个方法,用不同的方式消费同一个字符串数据两次
private static void operatorString(String name, Consumer<String> con1,Consumer<String> con2){
// con1.accept(name);
// con2.accept(name);
con1.andThen(con2).accept(name);
}
}
public class ConsumerDemo {
public static void main(String[] args) {
String [] strArray={"林青霞,30","张曼玉,35","王祖贤,33"};
printInfo(strArray,(String str)->{
String name=str.split(",")[0];
System.out.print("姓名:"+name);
},(String str)->{
Integer age=Integer.parseInt(str.split(",")[1]);
System.out.println(",年龄" +age);
});
System.out.println("============");
printInfo(strArray,str-> System.out.print("姓名:" +str.split(",")[0]),
str-> System.out.println(",年龄:" +Integer.parseInt(str.split(",")[1])));
}
private static void printInfo(String[] strArray, Consumer<String> con1,Consumer<String> con2){
for (String str:strArray) {
con1.andThen(con2).accept(str);
}
}
}
public class PredicateDemo {
public static void main(String[] args) {
/* boolean b1=checkString("hello",(String s)->{
return s.length()>8;
});*/
boolean b1=checkString("hello",s->s.length()>8);
System.out.println(b1);
}
//判断给定的字符串是否满足要求
private static boolean checkString(String s, Predicate<String> pre){
// return pre.test(s);
// 取反 正常不这样写
// return !pre.test(s);
return pre.negate().test(s);
}
}
import java.util.function.Predicate;
public class PredicateDemo {
public static void main(String[] args) {
boolean b1=checkString("hello",s->s.length()>8,s->s.length()<15);
System.out.println(b1);
boolean b2=checkString("helloworld",s->s.length()>8,s->s.length()<15);
System.out.println(b2);
}
//同一个字符串给出两个不同的判断条件,最后把这两个判断的结果做逻辑与运算的结果作为最终的结果
private static boolean checkString(String s, Predicate <String> pre1,Predicate<String> pre2){
/*boolean b1=pre1.test(s);
boolean b2=pre2.test(s);
boolean b=b1 && b2;
return b;*/
// return pre1.and(pre2).test(s);
return pre1.or(pre2).test(s);
}
}
public class PredicateDemo {
public static void main(String[] args) {
String[] strArray={"林青霞,30","柳岩,34","张曼玉,35","貂蝉,31","王祖贤,33"};
ArrayList<String> array=printInfo(strArray,s->s.split(",")[0].length()>2,
s->Integer.parseInt(s.split(",")[1])>33);
for(String str:array){
System.out.println(str);
}
}
public static ArrayList printInfo(String [] strArray, Predicate<String> pre1,Predicate<String> pre2){
ArrayList<String> arr=new ArrayList<>();
for(String s:strArray){
if(pre1.and(pre2).test(s)){
arr.add(s);
}
}
return arr;
}
}
public class PredicateDemo {
public static void main(String[] args) {
/*convert("100",(String s)->{
return Integer.parseInt(s);
});
convert("100",Integer::parseInt); */
convert("100",s->Integer.parseInt(s));
convert(100,i->String.valueOf(i+566));
convert("100",s->Integer.parseInt(s),i->String.valueOf(i+566));
}
//定义一个方法,把一个int类型的数据加上一个整数之后,转为字符串在控制台输出
public static void convert(String s, Function<String,Integer> fun){
int i=fun.apply(s);
System.out.println(i);
}
//定义一个方法,把一个int类型的数据加上一个整数之后,转为字符串在控制台输出
public static void convert(int i,Function<Integer,String> fun){
String s=fun.apply(i);
System.out.println(s);
}
//定义一个方法,把一个字符串转换为int类型,把int类型数据加上一个整数之后,转为字符串在控制台输出
public static void convert(String s,Function<String,Integer> fun1,Function<Integer,String> fun2){
/* int i=fun1.apply(s);
String ss=fun2.apply(i);
System.out.println(ss);*/
String ss = fun1.andThen(fun2).apply(s);
System.out.println(ss);
}
}
public class PredicateDemo {
public static void main(String[] args) {
String s="林青霞,30";
convert(s,s1->s1.split(",")[1],s2->Integer.parseInt(s2),i->i+70);
convert(s,s1->s1.split(",")[1],Integer::parseInt,i->i+70);
}
public static void convert(String s,Function<String,String> fun1,Function<String,Integer> fun2,
Function<Integer,Integer> fun3){
int i=fun1.andThen(fun2).andThen(fun3).apply(s);
System.out.println(i);
}
}
public class PredicateDemo {
public static void main(String[] args) {
ArrayList<String> arrayList=new ArrayList<>();
arrayList.add("张三");
arrayList.add("张无忌");
arrayList.add("张三丰");
arrayList.add("张敏");
arrayList.add("李四");
arrayList.add("王五");
ArrayList<String> zhangList=new ArrayList<>();
for (String zhang:arrayList){
if(zhang.startsWith("张")){
zhangList.add(zhang);
}
}
ArrayList<String> threeList=new ArrayList<>();
for(String str:zhangList){
if(str.length()==3){
threeList.add(str);
}
}
for (String s:threeList){
System.out.println(s);
}
System.out.println("--------------");
arrayList.stream().filter(s->s.startsWith("张")).filter(s->s.length()==3).
forEach(s-> System.out.println(s));
arrayList.stream().filter(s->s.startsWith("张")).filter(s->s.length()==3).
forEach(System.out::println);
}
}
public class PredicateDemo {
public static void main(String[] args) {
List<String> list=new ArrayList<>();
Stream<String> listStream = list.stream();
Set<String> set=new HashSet<>();
Stream<String> setStream = set.stream();
Map<String,Integer> map=new HashMap<>();
Stream<String> keyStream = map.keySet().stream();
Stream<Integer> valueStream = map.values().stream();
Stream<Map.Entry<String, Integer>> entryStream = map.entrySet().stream();
String [] strArray={"hello","world","java"};
Stream<String> strArrayStream = Stream.of(strArray);
Stream<String> strArrayStream2 = Stream.of("hello","world","java");
Stream<Integer> intStream = Stream.of(10,20,30);
}
}
public class PredicateDemo {
public static void main(String[] args) {
List<String> list=new ArrayList<>();
list.add("林青霞");
list.add("张曼玉");
list.add("柳岩");
list.add("张敏");
list.add("张无忌");
//需求1:取前3个数据在控制台输出
list.stream().limit(3).forEach(System.out::println);
System.out.println("------------");
//需求2:取跳过3个元素,把剩下的元素在控制台输出
list.stream().skip(3).forEach(System.out::println);
System.out.println("------------");
//需求3:取跳过2个元素,把剩下的元素中前2在控制台输出
list.stream().skip(2).limit(2).forEach(System.out::println);
}
}
public class PredicateDemo {
public static void main(String[] args) {
List<String> list=new ArrayList<>();
list.add("林青霞");
list.add("张曼玉");
list.add("王祖贤");
list.add("柳岩");
list.add("张敏");
list.add("张无忌");
//需求1:取前4个数据组成一个流
Stream<String> s1 = list.stream().limit(4);
//需求2:跳过2个数据组成一个流
Stream<String> s2 = list.stream().skip(2);
//需求3:合并需求1和需求2得到的流,并把结果在控制台输出
// Stream.concat(s1,s2).forEach(System.out::println);
System.out.println("------------");
//需求4:合并需求1和需求2得到的流,并把结果在控制台输出,要求字符串元素不能重复
Stream.concat(s1,s2).distinct().forEach(System.out::println);
}
}
public class PredicateDemo {
public static void main(String[] args) {
List<String> list=new ArrayList<>();
list.add("lingqingxia");
list.add("zhangmanyu");
list.add("wangzuxian");
list.add("liuyan");
list.add("zhangmin");
list.add("zhangwuji");
//需求1:按照字母顺序把数据在控制台输出
// list.stream().sorted().forEach(System.out::println);
//需求2:按照字符串长度把数据在控制台输出
list.stream().sorted((s1,s2)->{
int num=s1.length()-s2.length();
int num2=num==0?s1.compareTo(s2):num;
return num2;
}).forEach(System.out::println);
}
}
public class PredicateDemo {
public static void main(String[] args) {
List<String> list=new ArrayList<>();
list.add("10");
list.add("20");
list.add("30");
list.add("40");
list.add("50");
//需1:将集合中的字符串数据转换为整数之后在控制台输出
// list.stream().map(s->Integer.parseInt(s)).forEach(System.out::println);
// list.stream().map(Integer::parseInt).forEach(System.out::println);
// list.stream().mapToInt(Integer::parseInt).forEach(System.out::println);
//返回此流中元素的和
int result = list.stream().mapToInt(Integer::parseInt).sum();
System.out.println(result);
}
}
public class PredicateDemo {
public static void main(String[] args) {
List<String> list=new ArrayList<>();
list.add("林青霞");
list.add("张曼玉");
list.add("王祖贤");
list.add("柳岩");
list.add("张敏");
list.add("张无忌");
//需求1:把集合中的元素在控制台输出
list.stream().forEach(System.out::println);
//需求2:统计集合中有几个以张开头的元素,并把统计结果在控制台输出
long count = list.stream().filter(s -> s.startsWith("张")).count();
System.out.println(count);
}
}
public class Actor {
private String name;
public Actor(String name) { this.name = name; }
public String getName() {return name; }
public void setName(String name) { this.name = name; }
}
import java.util.ArrayList;
public class StreamTest {
public static void main(String[] args) {
ArrayList<String> manList = new ArrayList<String>();
manList.add("周润发");
manList.add("成龙");
manList.add("刘德华");
manList.add("吴京");
manList.add("周星驰");
manList.add("李连杰");
ArrayList<String> womanList = new ArrayList<String>();
womanList.add("林心如");
womanList.add("张曼玉");
womanList.add("林青霞");
womanList.add("柳岩");
womanList.add("林志玲");
womanList.add("王祖贤");
}
}
public class StreamTest {
public static void main(String[] args) {
ArrayList<String> manList = new ArrayList<String>();
manList.add("周润发");
manList.add("成龙");
manList.add("刘德华");
manList.add("吴京");
manList.add("周星驰");
manList.add("李连杰");
ArrayList<String> womanList = new ArrayList<String>();
womanList.add("林心如");
womanList.add("张曼玉");
womanList.add("林青霞");
womanList.add("柳岩");
womanList.add("林志玲");
womanList.add("王祖贤");
Stream<String> s1 = manList.stream().filter(s -> s.length() == 3).limit(3);
Stream<String> s2 = womanList.stream().filter(s -> s.startsWith("林")).skip(1);
Stream<String> stream = Stream.concat(s1, s2);
// stream.map(Actor::new).forEach(System.out::println);
stream.map(Actor::new).forEach(p -> System.out.println(p.getName()));
System.out.println("---------");
Stream.concat(manList.stream().filter(s->s.length()==3).limit(3),
womanList.stream().filter(s->s.startsWith("林")).skip(1)).
map(Actor::new).forEach(p-> System.out.println(p.getName()));
}
}
public class CollectionDemo {
public static void main(String[] args) {
List<String> list=new ArrayList<>();
list.add("林青霞");
list.add("张曼玉");
list.add("王祖贤");
list.add("柳岩");
//需求1:得到名字为3个字的流
Stream<String> listStream = list.stream().filter(s -> s.length() == 3);
//需求2:把使用Stream流操作完毕的数据收集到list集合中并遍历
List<String> names = listStream.collect(Collectors.toList());
for (String name:names){
System.out.println(name);
}
//创建Set集合对象
Set<Integer> set=new HashSet<>();
set.add(10);
set.add(20);
set.add(30);
set.add(33);
set.add(35);
//需求3:得到年龄大于25的流
Stream<Integer> setStream = set.stream().filter(age -> age > 25);
//需求4:把使用Stream流操作完毕的数据收集到Set集合中并遍历
Set<Integer> ages = setStream.collect(Collectors.toSet());
for (Integer age:ages){
System.out.println(age);
}
//定义一个字符串数组,把每一个字符串数据由姓名数据和年龄数据组合而成
String [] strArray={"林青霞,30","张曼玉,35","王祖贤,33","柳岩,25"};
//需求5:得到字符串中年龄数据大于28的流
Stream<String> arrayStream = Stream.of(strArray).filter(s -> Integer.parseInt(s.split(",")[1]) > 28);
//需求6:把使用Stream流操作完毕的数据收集到Map集合中遍历,字符串中的姓名作键,年龄作值
Map<String, Integer> map = arrayStream.collect(Collectors.toMap(
s -> s.split(",")[0],
s -> Integer.parseInt(s.split(",")[1])));
Set<String> keySet = map.keySet();
for (String key : keySet) {
Integer value = map.get(key);
System.out.println(key + "," + value);
}
}
}