时间:2022-12-20 10:11:38 | 栏目:JAVA代码 | 点击:次
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<A[]> 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. * * <p>This is a <a href="package-summary.html#StreamOps" rel="external nofollow" >terminal * operation</a>. * * @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: * <pre>{@code * Person[] men = people.stream() * .filter(p -> p.getGender() == MALE) * .toArray(Person[]::new); * }</pre> * * @param <A> 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> A[] toArray(IntFunction<A[]> generator);
可以看到toArray()的参数是IntFunction<A[]>类型,从@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异常时应该对应着数组中的元素类型去查找错误,构造数组时应按照正确的类型来构造。
Object[] toArray(); T[] toArray(T[] a);
分析:不带参数的方法默认是把数组转换为Object类型,而带参数的方法会将数组转换为指定的类型;
指定目标数组数据类型:
List<Integer> list = new ArrayList<Integer>(); list.add(12); list.add(13); list.toArray(new Integer[list.size()]);
不指定目标数组数据类型获得的数组类型是Object类型:
List<Integer> list = new ArrayList<Integer>(); list.add(12); list.add(13); list.toArray();
public class StingUtilsTest{ public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); list.add(12); list.add(13); list.toArray(new Long[list.size()]); } }
Exception in thread "main" java.lang.ArrayStoreException
at java.lang.System.arraycopy(Native Method)
at java.util.ArrayList.toArray(ArrayList.java:390)
at common.lang.StingUtilsTest.main(StingUtilsTest.java:23)
分析:出现这种异常是由于数组中存入的数据与要转换的目标数组的类型不一致导致的;还有一点需要注意的是toArray参数数组的初始化大小如果list.size大于等于list的列表的长度那么就默认使用当前的参数数组,如果小于list的长度就会重新创建一个数组,建议如果知道list的长度一定要初始化数组的长度,这样可以节省内存空间,提高效率;