Ambiguity when converting List <Object []> to list <Integer>

We tried to get the results from the database using a query SQL, using hibernatewhich will return List<Object[]>if records are present. But later we want to convert it to List<Integer>. Therefore, instead of repeating in the list Object[]and continuing to add integers to the list, we tried to do something, and the code example is similar to

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 trying to extract only one column from the query and that all Arrayshave only one element. Both Case-1 and Case-2 were executed separately, and the output was the same in both cases, it was something like this

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

Case-1 intList List<Integer> List Object[] s. , .

Case-2 Object[] List<Integer> - , , , wiered.

  • - , ?
  • a List<Integer>, Integers , , [[Ljava.lang.Object;@3e25a5, [Ljava.lang.Object;@19821f]?
  • Java . , Object[] ob1 = {1, 2, "3", "abc"};, intList intList.addAll(objList);, .
+4
3

@Arun Sudhakaran, , , :

1:

1:

: " intList List<Integer>, List Object[] " .

, objList <Object[]>; objList : objList = ArrayList();.

, objList List raw-type, List Object[].

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

intList List<Integer>, , intList List raw-type - List Object[] s.

Generics Java JDK 1.5. , Java raw-type.

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

objList raw-type. intList objList, , objList. Object objList.

2:

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

: " Object[] List<Integer> - "

, . List Object[] List<Integer> - not Object[] List<Integer>. intList.add(ob1); intList.add(ob2);. , , Object[] List<Integer>.

JDK 1.5 generics :

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

List , , List List. IDE, eclipse intelliJ, - List<? extends Integer>.

objList raw-type . , , objList Integer Integer. , . ob1, ob2 `ob3.

2:

objList List<Integer> objList, objList raw-type. . List<Integer>. List<Integer>, objList ob1, ob2 ob3.

3:

Java . , JDK 1.4, JDK 1.8 .

+10

().

, :

  String hql = "select count(me.grp) from  MyEntity me group by grp";
  TypedQuery<Integer> query = entityManager.createQuery(hql, Integer.class);
  List<Integer> result = query.getResult();

, :

  List<Integer> result = objList.stream()
          .map(oarr -> oarr[0])
          .map(Integer.class::cast)
          .collect(Collectors.toList());

select A a, B b, C c int, A:

List<Object[]> objList = ...

List<Integer> result = objList.stream()
          .map(oarr -> oarr[0])           // Object[] to Object
          .map(A.class::cast)             // Object to A
          .map(A::getId)                  // A to int A.getId()
          .collect(Collectors.toList());

objList , .

    List<Object[]> objList = new ArrayList<>();

objList , , intList. erasure intList , .

+3

, , , .

addAll():

public boolean addAll(Collection<? extends E> c)

- . objList - :

List objList = new ArrayList();

This makes addAll()all the arguments to ArrayLists. Change your code to this and you will get the expected error:

List<Integer> objList = new ArrayList<>();
+1
source

Source: https://habr.com/ru/post/1694507/


All Articles