学习内容:P425-P450
- 函数式接口Supplier、Consumer、Predicate、Function
package April.Apr23th;
import April.UsbDemo.Start;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
public class Demo01 {
public static Integer meathodGet01(Supplier<Integer> sup) {
return sup.get();
}
public static void methodConsumer(String name, Consumer<String> cs) {
cs.accept(name);
}
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, -50};
int max02 = meathodGet01(() -> {
int max = arr[0];
for (int i : arr) {
if (max < i)
max = i;
}
return max;
});
System.out.println(max02);
methodConsumer("赵丽颖", (name) -> {
System.out.println(name);
String rname = new StringBuffer(name).reverse().toString();
System.out.println(rname);
});
method03("键盘", (name) ->
System.out.println(name)
, (String name) -> {
String rname = new StringBuffer(name).reverse().toString();
System.out.println(rname);
}
);
Boolean result = method04("asdfbasdf", (str) -> {
return str.length() > 5;
});
System.out.println(result);
Boolean resultF = method05("aasdf", (String s) -> {
return s.length() > 6;
}, (String s) -> {
boolean b = s.contains("a");
return b;
});
System.out.println(resultF);
}
public static void method03(String name, Consumer<String> c1, Consumer<String> c2) {
//多个为c1.andThen(c2).andThen(c3)......
c1.andThen(c2).accept(name);
}
public static boolean method04(String str, Predicate<String> p1) {
return p1.test(str);
}
public static boolean method05(String str, Predicate<String> pre1, Predicate<String> pre2) {
//pre1.test(str) && pre2.(str);
pre1.and(pre2).test(str);//&&
pre1.or(pre2).test(str);//||
pre1.negate().test(str);//!
return true;
}
}
package April.Apr23th;
import java.util.ArrayList;
import java.util.function.Function;
import java.util.function.Predicate;
public class Test {
public static ArrayList<String> method01(String[] str,Predicate<String> pr1,Predicate<String> pr2){
ArrayList<String> list = new ArrayList<>();
boolean b = false;
for (String s : str) {
b = pr1.and(pr2).test(s);
if (b==true){
list.add(s);
}
}
return list;
}
public static Integer method02(String str, Function<String,Integer> fun){
return fun.apply(str);
}
public static String method03(String str, Function<String,Integer> fun,Function<Integer,String> fun2){
return fun.andThen(fun2).apply(str);
}
public static Integer method04(String str,Function<String,Integer> fun){
return fun.apply(str);
}
public static void main(String[] args) {
/*
//判断将结果返回结合
String[] str = {"古力娜扎,女","张三,男","马儿扎哈,男","甄姬,女"};
ArrayList<String> list = method01(str,(String s)->{
return s.split(",")[0].length()==4;
},(String s)->{
// return s.split(",")[1].equals("女");
return s.split(",")[1].contains("女");
});
for (String s : list) {
System.out.println(s);
}
*/
//将数据类型String的整数转化为Integer
String sNumber = "100";
Integer i = method02(sNumber,(String s)->{
return Integer.parseInt(s);
});
System.out.println(i);
//将结果再转为String
String sNum2 = "200";
String sNum3 = method03(sNum2,(String s)->{
return Integer.parseInt(s) + 10;
},i2->i2.toString());
//只有一个数据类型,修饰符和括号都可以省略
if (sNum3 instanceof String){
System.out.println(sNum3);
}
//字符串
String str4 = "张三,20";
int num = method04(str4,(String s)->{
String s2 = s.split(",")[1];
return Integer.parseInt(s2)+100;
});
System.out.println(num);
}
}
- Stream流
public class StreamDemo {
public static void main(String[] args) {
method02();
}
//常规过滤
public static void method01(){
List<String> list = new ArrayList<>();
list.add("张三");
list.add("李四");
list.add("王五");
list.add("张无忌");
//过滤
List<String> list2 = new ArrayList<>();
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()){
String next = iterator.next();
boolean b = next.startsWith("张");
if (b){
list2.add(next);
}
}
for (String s : list2) {
System.out.println(s);
}
}
public static void method02(){
List<String> list = new ArrayList<>();
list.add("张三");
list.add("李四");
list.add("王五");
list.add("张无忌");
list.stream().filter(name->name.startsWith("张"))
.filter(name->name.length()==3)
.forEach(name-> System.out.println(name));
}
}
public static void method03(){
List<String> list = new ArrayList<>();
Stream<String> stream = list.stream();
Set<String> set = new HashSet<>();
Stream<String> stream1 = set.stream();
Map<String,Integer> map = new HashMap<>();
Set<String> set1 = map.keySet();
Stream<String> stream2 = set1.stream();
Stream<Integer> stream3 = map.values().stream();
Map<String,Integer> map2 = new HashMap<>();
Set<Map.Entry<String, Integer>> set2 = map2.entrySet();
Stream<Map.Entry<String, Integer>> stream4 = set2.stream();
//数组转化stream流
String[] arr = {"a","b","c"};
Stream<String> arr1 = Stream.of(arr);
Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5, 6);
}
//流是一次性的,当作参数赋予新的流,则会删除之前的流,流合并数据不能重复
public static void method04(){
ArrayList<Integer> list = new ArrayList<>();
list.add(12);
list.add(24);
list.add(36);
list.stream().forEach(i->System.out.println(i));
Stream<String> s = Stream.of("张三", "王五", "李四");
Stream<String> s2= s.filter(name->name.startsWith("张"));
s2.forEach(name-> System.out.println(name));
//map映射
Stream<String> s3 = Stream.of("1","2","3","4","5");
Stream<Integer> s4 = s3.map((String str1)->{
return Integer.parseInt(str1);
});
s4.forEach((i)->{
System.out.println(i);
});
//count计数终止和foreach一样
Stream<String> s5 = Stream.of("张三", "王五", "李四","张四");
Stream<String> s6 = s5.filter((String str)->{
return str.startsWith("张");
});
long count = s6.count();
System.out.println(count+"个");
//limit取出前几个
Stream<Integer> is = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
Stream<Integer> limit = is.limit(5);
long count1 = limit.count();
System.out.println(count1+"个");
//skip跳过前几个,返回新的流
Stream<Integer> is2 = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
Stream<Integer> skip = is2.skip(3);
skip.forEach(integer -> System.out.print(integer+" "));
//concat
Stream<Integer> is1 = Stream.of(1,2,3,4);
Stream<Integer> is5 = Stream.of(21,33,44);
Stream<Integer> concat1 = Stream.concat(is1, is5);
concat1.forEach(i->{
System.out.print(i+" ");
});
System.out.println();
Stream<Integer> is3 = Stream.of(1,2,3,4);
Stream<String> is4 = Stream.of("张三","李四","王五");
//两个不同的流合并
Stream<? extends Serializable> concat = Stream.concat(is3, is4);
concat.forEach(i->{
System.out.print(i+" ");
});
stream流案例
package April.Apr23th;
import java.util.ArrayList;
import java.util.stream.Stream;
public class FinalDemo {
public static void main(String[] args) {
method01();
}
public static void method01() {
ArrayList<String> list = new ArrayList<>();
ArrayList<String> list2 = new ArrayList<>();
list.add("张张张");
list.add("张四张");
list.add("张三张");
list.add("张二张");
list.add("苏苏张");
list.add("李丽张");
list.add("张张");
list.add("个张张");
list.add("宏张张");
list.add("尼古拉斯张张");
//只要三个字的名字
for (String s : list) {
if (s.length() == 3)
list2.add(s);
}
//只要前三个
ArrayList<String> list3 = new ArrayList<>();
for (int i = 0; i < 3; i++) {
list3.add(list2.get(i));
}
//只要姓张
ArrayList<String> list4 = new ArrayList<>();
for (String s : list3) {
if (s.startsWith("张"))
list4.add(s);
}
//去掉前两个人
ArrayList<String> list5 = new ArrayList<>();
for (int i = 2; i < list4.size(); i++) {
list5.add(list4.get(i));
}
//根据姓名创建对象
ArrayList<Person> list1 = new ArrayList<>();
for (String s : list5) {
list1.add(new Person(s, 18));
}
for (Person person : list1) {
System.out.println(person.getName());
}
}
//使用映射流转换
public static void method02() {
ArrayList<String> list = new ArrayList<>();
list.add("张张张");
list.add("张四张");
list.add("张三张");
list.add("张二张");
list.add("苏苏张");
list.add("李丽张");
list.add("张张");
list.add("个张张");
list.add("宏张张");
list.add("尼古拉斯张张");
Stream<String> stream = list.stream();
stream.filter((name) -> {
return name.length() == 3;
}).limit(3).filter((name) -> {
return name.startsWith("张");
}).skip(2).map((name) -> {
return new Person(name, 18);
}
).forEach(person -> System.out.println(person));
}
}