本文已参与「新人创作礼」活动,一起开启掘金创作之路。
Lambda 表达式的练习
JDK 中的消费型接口
- 在 JDK 8 中的 Collection 接口的父接口 Iterable 接口中增加了一个默认方法,该方法是消费型接口:
public interface Iterable<T> {
// 遍历Collection集合的每个元素
default void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}
...
}
- 在 JDK 8 中的 Map 集合中增加了一个默认方法,该方法是消费型接口:
public interface Map<K, V> {
// 遍历Map集合的每一个entry对象
default void forEach(BiConsumer<? super K, ? super V> action) {
Objects.requireNonNull(action);
for (Map.Entry<K, V> entry : entrySet()) {
K k;
V v;
try {
k = entry.getKey();
v = entry.getValue();
} catch (IllegalStateException ise) {
// this usually means the entry is no longer in the map.
throw new ConcurrentModificationException(ise);
}
action.accept(k, v);
}
}
...
}
示例
package com.github.demo7;
import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
// 遍历List集合中的每个元素
list.forEach(i -> System.out.println(i));
}
}
示例
package com.github.demo8;
import java.util.HashMap;
public class Test {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
map.put("1", "java");
map.put("2", "c");
map.put("3", "python");
map.put("4", "vb");
map.put("5", "c#");
// 遍历Map中的每个元素
map.forEach((k, v) -> System.out.println(k + ":" + v));
}
}
JDK 中的供给型接口
- 在 JDK 8 中增加 Stream 接口,
java.util.stream.Stream<T>
是一个数据流,该方法是供给型接口,可以创建 Stream 对象:
public interface Stream<T> extends BaseStream<T, Stream<T>> {
public static<T> Stream<T> generate(Supplier<? extends T> s) {
Objects.requireNonNull(s);
return StreamSupport.stream(
new StreamSpliterators.InfiniteSupplyingSpliterator.OfRef<>(Long.MAX_VALUE, s), false);
}
...
}
示例
package com.github.demo9;
import java.util.Random;
import java.util.stream.Stream;
public class Test {
public static void main(String[] args) {
Stream.generate(() -> new Random().nextInt(10) + 1).limit(10).forEach(num -> System.out.println(num));
}
}
JDK 中函数型接口
●在 JDK 8 中的 Map 集合增加了很多函数型接口:
public interface Map<K, V> {
// 替换Map中的值
default void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
Objects.requireNonNull(function);
for (Map.Entry<K, V> entry : entrySet()) {
K k;
V v;
try {
k = entry.getKey();
v = entry.getValue();
} catch (IllegalStateException ise) {
// this usually means the entry is no longer in the map.
throw new ConcurrentModificationException(ise);
}
// ise thrown from function is not a cme.
v = function.apply(k, v);
try {
entry.setValue(v);
} catch (IllegalStateException ise) {
// this usually means the entry is no longer in the map.
throw new ConcurrentModificationException(ise);
}
}
}
...
}
示例
package com.github.demo11;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("1", "java");
map.put("2", "c");
map.put("3", "python");
map.put("4", "vb");
map.put("5", "c#");
// 将Map中的java替换成JavaEE
map.replaceAll((k, v) -> {
if (v.equals("java")) {
v = "JavaEE";
}
return v;
});
map.forEach((k, v) -> System.out.println(k + ":" + v));
}
}
示例
package com.github.demo10;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) {
Map<Integer, Employee> map = new HashMap<>();
Employee e1 = new Employee(1, "张三", 5000.00);
Employee e2 = new Employee(2, "李四", 15000.00);
Employee e3 = new Employee(3, "王五", 75000.00);
Employee e4 = new Employee(4, "赵六", 7000.00);
Employee e5 = new Employee(5, "田七", 200.00);
map.put(e1.getId(), e1);
map.put(e2.getId(), e2);
map.put(e3.getId(), e3);
map.put(e4.getId(), e4);
map.put(e5.getId(), e5);
// 将工资小于10000元的员工的工资设置为10000
map.replaceAll((k, v) -> {
if (v.getSalary() < 10000) {
v.setSalary(10000.00);
}
return v;
});
map.forEach((k, v) -> System.out.println(k + ":" + v));
}
}