void accept(T t)
//Consumer<T> 消费型接口
public void happy(double money, Consumer<Double> con){
con.accept(money);
}
T get();
//Supplier<T> 供给型接口
//需求:产生一些整数,并放入集合中
public List<Integer> getNumList(int num, Supplier<Integer> sup){
List<Integer> list = new ArrayList<>();
for (int i = 0; i < num; i++){
Integer n = sup.get();
list.add(n);
}
return list;
}
@Test
public void test2(){
List<Integer> numList = getNumList(10, () -> (int)(Math.random() * 100));
for (Integer num : numList){
System.out.println(num);
}
}
R apply(T t);
//Function<T, R> 函数型接口
//需求:用于处理字符串
public String strHandler(String str, Function<String, String> fun){
return fun.apply(str);
}
@Test
public void test3(){
String newStr = strHandler("\t\t\t e路纵横开发团队", (str) -> str.trim());
System.out.println(newStr);
String subStr = strHandler("e路纵横开发团队", (str) -> str.substring(0, 4));
System.out.println(subStr);
}
boolean test(T t);
//Predicate<T> 断言型接口
//需求:将满足条件的字符串,放入集合中去
public List<String> filterStr(List<String> list, Predicate<String> pre){
List<String> strList = new ArrayList<>();
for (String str : list){
if (pre.test(str)){
strList.add(str);
}
}
return strList;
}
@Test
public void test4(){
List<String> list = Arrays.asList("Hello", "e路纵横", "Lambda", "ok");
List<String> strList = filterStr(list, (s) -> s.length() > 3);
for (String str : strList){
System.out.println(str);
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。