java.lang.ArrayStoreException异常

异常提示:

java.lang.ArrayStoreException: java.lang.Boolean
	at java.util.stream.Nodes$FixedNodeBuilder.accept(Nodes.java:1222)
	at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
	at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
	at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
	at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
	at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:545)
	at java.util.stream.AbstractPipeline.evaluateToArrayNode(AbstractPipeline.java:260)
	at java.util.stream.ReferencePipeline.toArray(ReferencePipeline.java:438)
	at 
查询百度的解释:试图将错误类型的对象存储到一个对象数组时抛出的异常。之后,在看看自己错误的代码:
Field[] filterCopyFields = Stream.of(appendFields)
     .map(f -> !preFieldNames.contains(f.getName())).toArray(Field[]::new);
很容易看出问题的所在,这里我是想过滤Field[]数组中的元素,!preFieldNames.contains(f.getName())这个是过滤条件,发现了这里使用的居然是map,过滤应该是使用filter,map中的元素应该是返回结果并在toArray方法中转换成数组,这里map中返回的是Boolean布尔类型的数据,也就是说不能将boolean类型的对象存储到Field对象数组中。
这里可以看一下JDK8源码中对toArray(IntFunction generator)方法的定义:
    /**
     * Returns an array containing the elements of this stream, using the
     * provided {@code generator} function to allocate the returned array, as
     * well as any additional arrays that might be required for a partitioned
     * execution or for resizing.
     *
     * 

This is a terminal * operation. * * @apiNote * The generator function takes an integer, which is the size of the * desired array, and produces an array of the desired size. This can be * concisely expressed with an array constructor reference: *

{@code
     *     Person[] men = people.stream()
     *                          .filter(p -> p.getGender() == MALE)
     *                          .toArray(Person[]::new);
     * }
* * @param the element type of the resulting array * @param generator a function which produces a new array of the desired * type and the provided length * @return an array containing the elements in this stream * @throws ArrayStoreException if the runtime type of the array returned * from the array generator is not a supertype of the runtime type of every * element in this stream */ A[] toArray(IntFunction generator);
可以看到toArray()的参数是IntFunction类型,从@param A the element type of the resulting array这个注解中可以看到,A是表示返回数组的元素类型,在我的例子中返回类型是一个Field,而如果Stream中使用了map遍历,返回的类型又是Boolean,类型不匹配而出现错误。
解决更改:
Field[] filterCopyFields = Stream.of(appendFields)
     .filter(f -> !preFieldNames.contains(f.getName())).toArray(Field[]::new);
其实这种小问题应该很容易避免,在出现ArrayStoreException异常时应该对应着数组中的元素类型去查找错误,构造数组时应按照正确的类型来构造。



你可能感兴趣的:(JDK1.8,Java)