在java8中尝试从列表转换为映射时获取ArraysIndexOutOfBoundException
我试图使用在java8中尝试从列表转换为映射时获取ArraysIndexOutOfBoundException,java,java-8,Java,Java 8,我试图使用split()从列表中获取Map,但我得到的是ArrayIndexOutOfBoundException List<String> lst = new ArrayList<>(); lst.add(A1); lst.add(A2); lst.add(A3); Map<String, List<String>> test1 = lst.stream() .map(s
split()
从列表中获取Map
,但我得到的是ArrayIndexOutOfBoundException
List<String> lst = new ArrayList<>();
lst.add(A1);
lst.add(A2);
lst.add(A3);
Map<String, List<String>> test1 = lst.stream()
.map(s -> new AbstractMap.SimpleEntry<String,String>(s.split("[^A-Z]")[0], s.split("[^A-Z]")[1]))
.collect(Collectors.groupingBy(Map.Entry::getKey,
Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
List lst=new ArrayList();
第1条增补(A1);
第1条增补(A2);
第1条增补(A3);
映射test1=lst.stream()
.map(s->new AbstractMap.SimpleEntry(s.split(“[^A-Z]”[0],s.split([^A-Z]”[1]))
.collect(Collectors.groupingBy(Map.Entry::getKey、,
Collectors.mapping(Map.Entry::getValue,Collectors.toList());
结果异常。要避免异常,应筛选出元素不足的数组:
Map<String, List<String>> test1 =
lst.stream()
.map(s -> s.split("[^A-Z]"))
.filter(a -> a.length > 1)
.map(a -> new AbstractMap.SimpleEntry<String,String>(a[0],a[1]))
.collect(Collectors.groupingBy(Map.Entry::getKey,
Collectors.mapping(Map.Entry::getValue,
Collectors.toList())));
是的..如何在Java8中检查拆分长度如果你能想到将s.split(“[^A-Z]”)移动一步
,然后你就知道如何检查长度
(以及什么)。
Map<String, List<String>> test1 =
lst.stream()
.map(s -> s.split("[^A-Z]"))
.filter(a -> a.length > 1)
.collect(Collectors.groupingBy(a -> a[0],
Collectors.mapping(a -> a[1],
Collectors.toList())));