如何使用 java 流 api 将这段代码转换为流

how do I convert this piece of code to stream using java stream api

我正在尝试将以下代码转换为流。这并不难,但我不确定如何处理流中的空值。我确实检查了 Optinal.ofNullable 方法,但不确定是否必须使用它 2-3 次才能获得正确的流代码。我现在可以使用下面的代码,但我希望学会在流中进行。请大家帮我学习。

Map<String, Map<String, List<String>>> fileTypeMapping = new HashMap<>();

Map<String, List<String>> map = new HashMap<>();
    map.put("codec", new ArrayList(Arrays.asList("ext1", "ext2")));
    fileTypeMapping.put("image", map);

String fileType = "Image";
String codec = "Codec";
String extension = "ext2";
boolean exist = false;
if(fileType != null) {
    Map<String, List<String>> codecMap = fileTypeMapping.get(fileType.toLowerCase());
    if(codecMap != null) {
        List<String> list = codecMap.get(codec.toLowerCase());
        if (list != null) {
            exist = list.contains(extension.toLowerCase());
        }
    }
}
System.out.print(exist);

似乎与 Stream API 无关,只需使用 Map.getOrDefault 即可摆脱 null return。

import java.util.*;

public class NullableMapGet {
    public static void main(String[] args) {
        Map<String, Map<String, List<String>>> fileTypeMapping = new HashMap<>();

        Map<String, List<String>> map = new HashMap<>();
        map.put("codec", new ArrayList(Arrays.asList("ext1", "ext2")));
        fileTypeMapping.put("image", map);

        String fileType = "Image";
        String codec = "Codec";
        String extension = "ext2";
        boolean exist = false;
        if (fileType == null || codec == null || extension == null) {
            exist = false;
        } else {
            exist = fileTypeMapping
                    .getOrDefault(fileType.toLowerCase(), Collections.emptyMap())
                    .getOrDefault(codec.toLowerCase(), Collections.emptyList())
                    .contains(extension.toLowerCase());
        }
        System.out.print(exist);
    }
}