jdk8-day1-作业
考点:foeEach的参数的Consumer接口改造Lambda表达式
使用labmda表达式改造下列作业 并且使用labmda进行遍历
1.用一个大集合存入20个随机数字,然后筛选其中的偶数元素,放到小集合当中然后进行遍历输出。
package Java_01homework;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.function.Predicate;
/**
* @author Hlmove
* @date 创建日期 2023/8/29 18:55
* @Description Java_01homework
* @Note 1.用一个大集合存入20个随机数字,然后筛选其中的偶数元素,放到小集合当中然后进行遍历输出。
*/
public class P1_1 {
public static void main(String[] args) {
Random random = new Random();
List<Integer> arr = new ArrayList<>();
for (int i = 0; i < 20; i++) {
arr.add(random.nextInt(100));
}
Predicate<Integer> predicate = integer -> integer % 2 == 0;
for (Integer integer : getNewList(arr, predicate)) {
System.out.println("integer = " + integer);
}
}
public static List<Integer> getNewList(List<Integer> arr, Predicate<Integer> p ) {
List<Integer> newList = new ArrayList<>();
for (Integer temp : arr) {
if (p.test(temp)) {
newList.add(temp);
}
}
return newList;
}
}
2.ArrayList去除集合中字符串的重复值(字符串的内容相同)
思路:创建新集合方式,新集合添加的时候用contains()判断
package Java_01homework;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
/**
* @author Hlmove
* @date 创建日期 2023/8/29 19:10
* @Description Java_01homework
* @Note 2.ArrayList去除集合中字符串的重复值(字符串的内容相同)
* 思路:创建新集合方式,新集合添加的时候用contains()判断
*/
public class P2_1 {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("a");
list.add("a");
list.add("b");
list.add("b");
list.add("c");
for (String s : listClear(list)) {
System.out.println("s = " + s);
}
}
public static List<String> listClear(List<String> list){
List<String> newList = new ArrayList<>();
for (String temp : list) {
Predicate<String> predicate = s -> !newList.contains(temp);
if (predicate.test(temp)) {
newList.add(temp);
}
}
return newList;
}
}
考点:map中foeEach的参数的BiConsumer接口改造Lambda表达式
1.使用lambda遍历map集合
package Java_01homework;
import java.util.HashMap;
import java.util.Map;
/**
* @author Hlmove
* @date 创建日期 2023/8/29 19:26
* @Description Java_01homework
* @Note 使用lambda遍历map集合
*/
public class P3_1 {
public static void main(String[] args) {
Map<String,Integer> map = new HashMap<>();
map.put("a", 5);
map.put("b",5);
map.put("c",5);
map.put("d",5);
map.forEach((k, v) -> System.out.println("k="+k+":v="+v));
}
}
考点:Runnable函数式接口改造Lambda表达式
1.使用lambda改造Runnable创建线程
package Java_01homework;
/**
* @author Hlmove
* @date 创建日期 2023/8/29 19:31
* @Description Java_01homework
* @Note ### 考点:Runnable函数式接口改造Lambda表达式
*
* ##### 1.使用lambda改造Runnable创建线程
*/
public class P4_1 {
public static void main(String[] args) {
Runnable runnable = () -> {
System.out.println("改造 Runnable,使用 lambda表达式");
};
new Thread(runnable).start();
}
}
考点:函数式接口和Lambda表达式
1.自定义一个函数式接口,提供方法将字符串转为数字,使用Lambda测试。抽象方法 toNum。
package Java_01homework;
/**
* @author Hlmove
* @date 创建日期 2023/8/29 19:33
* @Description Java_01homework
* @Note 考点:函数式接口和Lambda表达式
* 1.自定义一个函数式接口,提供方法将字符串转为数字,使用Lambda测试。抽象方法 toNum。
*/
public class P5_1 {
public static void main(String[] args) {
String s = "61118941";
Action action = s1 -> System.out.println(Integer.parseInt(s1));
action.toNum(s);
}
}
interface Action{
void toNum(String s);
}
考点:考点:函数式接口和Lambda表达式 String的toUpperCase和subString方法
1.声明函数式接口,接口中声明抽象方法,public String getValue(String str);声明类 TestLambda,类中编写方法使用接口作为参数,将每一个字符串转换成大写,并作为 方法的返回值,再将一个字符串的第2个和第4个索引位置进行截取子串。
package Java_01homework;
/**
* @author Hlmove
* @date 创建日期 2023/8/29 19:37
* @Description Java_01homework
* @Note 1.声明函数式接口,接口中声明抽象方法,public String getValue(String str);
* 声明类 TestLambda,类中编写方法使用接口作为参数,将每一个字符串转换成大写,并作为 方法的返回值,
* 再将一个字符串的第2个和第4个索引位置进行截取子串。
*/
public class P6_1 {
public static void main(String[] args) {
String str = "bcdedit";
TestLambda testLambda = new TestLambda();
String value = testLambda.getValue(str);
System.out.println(value);
}
interface Action{
String getValue(String str);
}
static class TestLambda implements Action{
@Override
public String getValue(String str) {
String newStr = str.substring(2, 4);
return newStr.toUpperCase();
}
}
}
考点:函数式接口和Lambda表达式 接口作为方法形参
1.声明一个带两个泛型的函数式接口,泛型类型为<T,R> T为参数,R为返回值接口中声 明对应抽象方法在TestLambda类中声明方法,使得接口作为参数,计算两个long型参 数的和,再计算两个long型参数的乘积.
package Java_01homework;
/**
* @author Hlmove
* @date 创建日期 2023/8/29 19:43
* @Description Java_01homework
* @Note 考点:函数式接口和Lambda表达式 接口作为方法形参
* 1.声明一个带两个泛型的函数式接口,泛型类型为<T,R> T为参数,R为返回值
* 接口中声明对应抽象方法
* 在TestLambda类中声明方法,使得接口作为参数,计算两个long型参 数的和,再计算两个long型参数的乘积.
*/
public class P7_1 {
public static void main(String[] args) {
Action<Long,Long> sum = (a, b) -> a+b;
Action<Long,Long> num = (a, b) -> a*b;
long l1 = 12L;
long l2 = 12L;
System.out.println("sum.action(l1,l2) = " + sum.action(l1, l2));
System.out.println("num.action(l1,l2) = " + num.action(l1, l2));
}
interface Action<T,R>{
R action(T t,R r);
}
}
考点:Preidcate接口的使用和方法and
1.使用Predicate接口String[] arr = {"卢本伟,男","五五开,女","white,男"};把三个字的,性 别为男的存一个集合中。
package Java_01homework;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;
/**
* @author Hlmove
* @date 创建日期 2023/8/29 19:53
* @Description Java_01homework
* @Note 考点:Preidcate接口的使用和方法and
* 1.使用Predicate接口String[] arr = {"卢本伟,男","五五开,女","white,男"};
* 把三个字的,性别为男的存一个集合中。
*/
public class P8_1 {
public static void main(String[] args) {
String[] arr = {"卢本伟,男", "五五开,女", "white,男"};
Predicate<String> predicate = string -> "男".equals(string);
split(arr,predicate).forEach((a, b) -> System.out.println(a+":"+b));
}
public static Map<String, String> split(String[] arr,Predicate<String> p) {
Map<String, String> map = new HashMap<>();
for (String str : arr) {
String[] split = str.split(",");
if (p.test(split[1])) {
map.put(split[0], split[1]);
}
}
return map;
}
}
考点:Consumer接口的使用 StringBuilder.reverse()
1.使用Consumer接口将字符串进行反转输出
考点:Consumer接口的使用 和方法andThen()
1.使用Consumer接口
格式化打印信息
String[] arr = {"卢本伟,男","五五开,女","white,男"};
格式化结果:
卢本伟:男 姓名:卢本伟,性别:男
五五开:女
white:男
package Java_01homework;
import java.util.function.Consumer;
/**
* @author Hlmove
* @date 创建日期 2023/8/29 20:07
* @Description Java_01homework
* @Note 考点:Consumer接口的使用 和方法andThen()
* 1.使用Consumer接口
* 格式化打印信息
* String[] arr = {"卢本伟,男","五五开,女","white,男"};
* 格式化结果:
* 卢本伟:男 姓名:卢本伟,性别:男
* 五五开:女
* white:男
* 在Java中,andThen()是一个函数式接口Function的默认方法。
* 它允许你将两个函数组合在一起,形成一个新的函数,该新函数首先执行第一个函数,然后将其结果作为参数传递给第二个函数。
*/
public class P10_1 {
public static void main(String[] args) {
String[] arr = {"卢本伟,男","五五开,女","white,男"};
Consumer<String> consumer1 = s -> {
String[] split = s.split(",");
System.out.println("姓名:"+split[0]+" 性别:"+split[1]);
};
Consumer<String> consumer2 = s -> {
String[] split = s.split(",");
System.out.print(split[0]+":"+split[1]+" ");
};
Consumer<String> consumer = consumer2.andThen(consumer1);
for (String s : arr) {
consumer.accept(s);
}
}
}
考点:Function接口的使用 和方法andThen()
1.使用Function接口 long long把String类型的”123”,转换为Integer类型,把转换后的结 果加10把增加后的Integer类型数据转换为String类型
package Java_01homework;
import java.util.function.Function;
/**
* @author Hlmove
* @date 创建日期 2023/8/29 20:18
* @Description Java_01homework
* @Note 考点:Function接口的使用 和方法andThen()
*1.使用Function接口 long long把String类型的”123”,转换为Integer类型,把转换后的结 果加10把增加后的Integer类型数据转换为String类型
*/
public class P11_1 {
public static void main(String[] args) {
String s = "123";
Function<String,Integer> function1 = s1 -> Integer.parseInt(s1);
Function<Integer,Integer> function2 = integer -> integer*10;
Function<Integer,String> function3 = integer -> String.valueOf(integer);
Function<String, String> function = function1.andThen(function2).andThen(function3);
System.out.println("function.apply(s) = " + function.apply(s));
}
}
考点:Supplier接口的使用 数组求最大值
1.使用Supplier接口求数组元素最大值:
int[] arr = {2,3,4,52,333,23};
package Java_01homework;
import java.util.function.Supplier;
/**
* @author Hlmove
* @date 创建日期 2023/8/29 20:26
* @Description Java_01homework
* @Note 考点:Supplier接口的使用 数组求最大值
* 1.使用Supplier接口求数组元素最大值:
* int[] arr = {2,3,4,52,333,23};
*/
public class P12_1 {
public static void main(String[] args) {
int[] arr = {2,3,4,52,333,23};
Supplier<Integer> supplier = () -> {
int max = 0;
for (int i = 0; i < arr.length; i++) {
int temp = arr[i];
if (temp > max){
max=temp;
}
}
return max;
};
System.out.println(supplier.get());
}
}