将List 转换为List 时的歧义(Ambiguities while converting List to List)

编程入门 行业动态 更新时间:2024-10-26 10:32:55
将List 转换为List 时的歧义(Ambiguities while converting List to List)

我们试图通过使用hibernate SQL查询从数据库获取结果,如果存在记录,它将返回List<Object[]> 。 但后来我们想将它转换成一个List<Integer> 。 所以不是遍历Object[]列表并继续添加到整数列表,我们尝试了一些东西,示例代码就像

public class ListObjectArrayToListInteger { public static void main(String[] args) { Object[] ob1 = {1}; Object[] ob2 = {5}; Object[] ob3 = {9}; List objList = new ArrayList(); objList.add(ob1); objList.add(ob2); objList.add(ob3); //Case - 1 List<Integer> intList = objList; System.out.println(intList); //Case - 2 List<Integer> intList = new ArrayList<Integer>(); intList.addAll(objList); System.out.println(intList); } }

我们只是试图从查询中获取一列,这就是为什么所有Arrays都只有一个元素。 在这两种情况下, Case-1Case-2都分别执行并且输出相同,都是这样的

[[Ljava.lang.Object; @ 3e25a5,[Ljava.lang.Object; @ 19821f,[Ljava.lang.Object; @ addbf1]

Case-1 intList , List<Integer>一个类型指向List和Object[]的一个对象。 由于数据类型完全不同,我们期待运行时发生异常。

case-2中,我们明确地将List<Integer> Object[]添加到List<Integer>并且没有任何异常,它提供了一个输出,直到我的知识为止。

那么有人可以解释为什么我们会得到这样的输出吗? 如何一个List<Integer> ,它在所有的位置上只有Integers ,会持有这样的数据[[Ljava.lang.Object;@3e25a5, [Ljava.lang.Object;@19821f] ? 为什么Java允许用户将数据从原始数据列表添加到通用列表中。 在我的代码中,如果我使Object[] ob1 = {1, 2, "3", "abc"}; 它可以作为intList.addAll(objList);添加到intList中intList.addAll(objList); 这是允许和平稳运行。

We were trying to fetch results from database via SQL query using hibernate which will return List<Object[]>, if records are present. But later we want to convert it into a List<Integer>. So instead of iterating through the Object[] list and keep on adding to integer list, we tried something and the sample code is like

public class ListObjectArrayToListInteger { public static void main(String[] args) { Object[] ob1 = {1}; Object[] ob2 = {5}; Object[] ob3 = {9}; List objList = new ArrayList(); objList.add(ob1); objList.add(ob2); objList.add(ob3); //Case - 1 List<Integer> intList = objList; System.out.println(intList); //Case - 2 List<Integer> intList = new ArrayList<Integer>(); intList.addAll(objList); System.out.println(intList); } }

We are only trying to fetch one column from the query and that's why all the Arrays are having only one element. Both Case-1 and Case-2 were executed separately and output was same in both case, it was something like this

[[Ljava.lang.Object;@3e25a5, [Ljava.lang.Object;@19821f, [Ljava.lang.Object;@addbf1]

In Case-1 intList a type of List<Integer>, is pointing to an object of List with Object[]s. We were expecting an Exception in run time because the data types are entirely different.

In Case-2 we are explicitly adding Object[]s to List<Integer> and without any exception it is giving an output which up to my knowledge is wiered.

So can anybody explain why we are getting output like this? How a List<Integer>, which is expecting only Integers at all its locations, would hold a data like this [[Ljava.lang.Object;@3e25a5, [Ljava.lang.Object;@19821f]? Why would Java allow users to add data from a raw data list to a generic list. In my code if I make Object[] ob1 = {1, 2, "3", "abc"}; it can be added to intList as intList.addAll(objList); which is allowed and runs smoothly.

最满意答案

@Arun Sudhakaran,你问的三个问题可以这样回答:

问题1:

情况1:

你说: intList是List<Integer>一种类型,指向List with Object[] s ”的 Object[] 。

这是不正确的,因为你没有用参数<Object[]>初始化objList ; 你正在像这样初始化objList : List objList = new ArrayList();

通过这样做, objList将被创建为具有raw类型的List ,而不是具有Object[]类型的List 。

(更多关于原始类型: https //docs.oracle.com/javase/tutorial/java/generics/rawTypes.html )

由于intList是List<Integer>一种类型,这意味着intList指向List 类型raw的对象 - 而不是List类型为Object[]的Object[] 。

JDK 1.5开始,泛型一直是Java的一部分。 为了保持向后兼容性,Java没有删除原始类型的集合初始化。

//Case - 1 List<Integer> intList = objList;

在这种情况下, objList用raw-type初始化。 通过将intList指向intList ,编译器不知道objList集合变量所持有的值的类型。 这就是编译器允许你将一个Object数组添加到objList 。

案例2:

//Case - 2 List<Integer> intList = new ArrayList<Integer>(); intList.addAll(objList); System.out.println(intList);

你说: 我们明确的将Object[]添加到List<Integer> ,没有任何异常,它给出了一个输出

再次,这是不正确的。 您将List<Integer> - not Object[] s添加到List<Integer>类型的Object[] List<Integer> 。 你不是在做intList.add(ob1); intList.add(ob2); intList.add(ob1); intList.add(ob2); 。 如果你这样做了,那么你可以说我们将Object[]添加到List<Integer> 。

JDK 1.5开始,您可以使用泛型来初始化集合变量,如下所示:

List<Integer> list = new ArrayList<>(); //or List<?> list = new ArrayList<>();

之后,当试图将元素添加到list ,编译器将检查元素是否与list或list相同子类型具有相同的数据类型。 当你将鼠标悬停在诸如eclipse或intelliJ之类的IDE中的集合变量上时,你可能会看到像List<? extends Integer> List<? extends Integer> 。

但是在这里, objList是用原始类型初始化的,而不是泛型类型。 这意味着编译器不知道objList不是Integer类型或者是Integer类型。 如果是这种情况,编译器不会抛出异常。 相反,编译器将遍历列表并给出ob1 , ob2和`ob3的参考值。

问题2:

当试图在编译时将objList所有元素添加到List<Integer>中时,编译器将不知道objList的数据类型,因为objList是raw类型的 。 因此,编译器不会抛出异常。 相反,编译器将允许用户将这样的元素添加到List<Integer> 。 当试图迭代List<Integer> ,你会发现ob3元素的参考值是ob1 , ob2和ob3 。

问题3:

Java只允许用户将数据从原始数据列表添加到通用列表中。 这样做是为了支持兼容性,并且是您的代码在JDK 1.4JDK 1.8中均可平稳运行而不更改任何代码的原因。

@Arun Sudhakaran, The three questions you asked can be answered like this:

Question 1:

Case 1:

You said: "intList a type of List<Integer>, is pointing to an object of List with Object[]s ".

This is incorrect because you are not initializing objList with the parameter <Object[]>; you are initializing objList like this: List objList = new ArrayList();.

By doing so, objList will be created as a List with raw-type, not as a List with type Object[].

(more on raw-type: https://docs.oracle.com/javase/tutorial/java/generics/rawTypes.html)

Since intList is a type of List<Integer>, this means that intList is pointing to an object of List with raw-type- not to an object of List with type Object[]s.

Generics have been a part of Java since JDK 1.5. To maintain backward compatibility, Java has not removed collection initialization with raw-type.

//Case - 1 List<Integer> intList = objList;

In this case, objList is initialized with raw-type. By pointing intList to objList, the compiler does not know the type of value the objList collection variable is holding. This is why the compiler allows you to add an array of Object into objList.

Case 2:

//Case - 2 List<Integer> intList = new ArrayList<Integer>(); intList.addAll(objList); System.out.println(intList);

You said: " we are explicitly adding Object[]s to List<Integer> and without any exception it is giving an output "

Again, This is incorrect. You are adding a List of type Object[] into List<Integer>- not Object[]s into List<Integer>. You are not doing intList.add(ob1); intList.add(ob2);. If you did this, then you could say that we are adding Object[]s into List<Integer>.

From JDK 1.5 and onward, you can use generics to initialize collection variables like this:

List<Integer> list = new ArrayList<>(); //or List<?> list = new ArrayList<>();

After that, when trying to add elements into list, the compiler will check whether or not the element has the same datatype as list or same sub-type of list. When you hover over a collection variable in IDEs like eclipse or intelliJ, you may see something like List<? extends Integer>.

But here, objList is initialized with raw-type not a generic type. This means the compiler does not know that objList is not of type Integer or of sub-type Integer. If this is the case, the compiler will not throw an exception. Instead, the compiler will traverse the list and give the reference values of ob1, ob2 and `ob3.

Question 2:

When trying to add all elements of objList into List<Integer>, at compile time, the compiler will not know the data-type of objList because objList is of raw-type. As a result, the compiler will not throw an exception. Instead, the compiler will allow the user to add such element into List<Integer>. When trying to iterate through List<Integer>, you will find the reference value of the elements in objList which are ob1, ob2 and ob3.

Question 3:

Java only allows users to add data from a raw data list to the generic list. This is done to support compatibility and is the reason that your code will run smoothly in both JDK 1.4 and JDK 1.8 without changing any code.

更多推荐

本文发布于:2023-07-26 08:11:00,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:歧义   转换为   List   Ambiguities   converting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!