2

in Java i have one List of Objects from my database and i want to convert this objects into my class objects but i cant find the solution.

My List of objects is SalvaguardasAGR and i want to convert this objects to List<AGRSalvaguardasInforme>.

I tried with

List<AGRSalvaguardasInforme> InformeFinal = new ArrayList<AGRSalvaguardasInforme>(SalvaguardasAGR);

as i see in How to cast List<Object> to List<MyClass> and How to Convert List<String> to List<Object> but throw this exception:

    25-sep-2014 17:40:47 org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: El Servlet.service() para el servlet [procop2front] en el contexto con ruta [/CDNMS] lanzó la excepción [java.lang.Error: Unresolved compilation problem: 
    The constructor ArrayList<AGRSalvaguardasInforme>(List<Object>) is undefined
] con causa raíz
java.lang.Error: Unresolved compilation problem: 
        The constructor ArrayList<AGRSalvaguardasInforme>(List<Object>) is undefined at com.dominion.procop.agr.struts.actions.AGRInformes.mostrarInformeActivosAGR(AGRInformes.java:1140)

I tried a simple List<AGRSalvaguardasInforme> InformeFinal = (AGRSalvaguardasInforme)SalvaguardasAGR; too but still not working.

What im doing wrong? how i can convert a List of objects into a List of AGRSalvaguardasInforme?

Thank you in advance.

Community
  • 1
  • 1
S. Moreno
  • 526
  • 2
  • 7
  • 29

6 Answers6

2

Java 8:

List<AGRSalvaguardasInforme> InformeFinal = SalvaguardasAGR.stream().map(x -> (AGRSalvaguardasInforme)x).collect(Collectors.toList());

This:

  • treats the elements of the list as a stream, in order
  • transforms each element of the stream by casting it (actually, this doesn't do any transformation at all, but it does check to make sure the object is in the right class and throws a ClassCastException if not)
  • collects the resulting items into a new list

Note: I've tested this and it works.

ajb
  • 31,309
  • 3
  • 58
  • 84
  • Looks totally cool in `Java 8` - I thought about posting similar answer using `Guava`, but skipped that because of ugly anonymous classes :) – Yuriy Nakonechnyy Sep 25 '14 at 17:55
1

Did you try to double-cast it like in answers to How to cast List<Object> to List<MyClass> question?

List<Object> list = getList();
List<DesiredType> castedList = (List<DesiredType>) (List) list;

Be careful though, because calls to castedList.get(i) or other methods may throw ClassCastException if it turns out that list contains element which is not of DesiredType.

That's why I would really recommend to try and get List<DesiredType> instance right-away - where do you get instance of List<Object> from? Is it going from your own code (code which you can modify) or from another library/code which you cannot modify?

Community
  • 1
  • 1
Yuriy Nakonechnyy
  • 3,742
  • 4
  • 29
  • 41
  • 1
    I believe you're wrong about `ClassCastException`. The code you write does **not** check each element of the list to make sure it's the right type--I just tested it. That means doing this could possibly create a `List` that has objects that aren't `DesiredType` in it, defeating Java's whole type system. `ClassCastExceptions` could come later, at totally unexpected places. – ajb Sep 25 '14 at 16:11
  • @Yura is from my own code. In this line is where returns a `Vector` (and i cast, but i dont know why ignore that). and the error when i try to convert my objects list into myclass list: `java.lang.ClassCastException: java.util.Vector cannot be cast to procop.agr.util.AGRSalvaguardasInforme` – S. Moreno Sep 26 '14 at 09:09
  • @S.Moreno could you please add code snippet producing `java.util.Vector` instance which you try to cast to List? I'm sure there's decent typesafe solution to your problem :) – Yuriy Nakonechnyy Sep 26 '14 at 14:04
0

The easiest and most logical way would just be to iterate through the list of Objects, and individually cast them to the 'AGRSalvaguardasInforme' type, and add them to the other list, eg:

 for (Object o : SalvaguardasAGR)
        InformeFinal.add((AGRSalvaguardasInforme) o);

You could also try doing the direct cast of the list like so:

 List<AGRSalvaguardasInforme> InformeFinal = (List<AGRSalvaguardasInforme>)(Object) SalvaguardasAGR

Although that method will flag an unchecked cast warning.

user3062946
  • 682
  • 1
  • 6
  • 17
  • this kind of casting does not work, e.g. List is not List – Kalpesh Soni Sep 25 '14 at 15:53
  • That is true, but the implication he seems to be making is that the Objects in his original list ARE instances of 'AGRSalvaguardasInforme', and he just wanted a way to cast the list into the correct type. – user3062946 Sep 25 '14 at 15:55
0

If you absolutely know the objects are assignable to AGRSalvaguardasInforme, you'll need to cast the elements directly and add them to a list with the target type:

public static <T> List<T> cast(Collection<?> list) {
    List<T> valueList = new ArrayList<T>(list.size());

    for(Object o : list) {
        // throws casting exception
        valueList.add((T)o);  
    }

    return valueList;
}

You can call this method like this:

List<AGRSalvaguardasInformeme> list = cast(salvaguardasAGR);

If handled properly, you shouldn't ever need to cast a list of objects to a list of something else. If you have a List of AGRSalvaguardasInformeme, why to create the properly typed list initially? I understand there are situations where you cant avoid this (like using a poorly written library).

Isaiah van der Elst
  • 1,435
  • 9
  • 14
0

Here's the brute-force way:

// initialize new list with the same size as the old list
List<AGRSalvaguardasInforme> InformeFinal = new ArrayList<AGRSalvaguardasInforme>(SalvaguardasAGR.size());
// cast and insert the contents of the old list into the new list
for(Object obj : SalvaguardasAGR)
{
    AGRSalvaguardasInforme newObj = (AGRSalvaguardasInforme) obj;
    InformeFinal.add(newObj);
}

Note that you'll get a ClassCastException if the Objects aren't instances of AGRSalvaguardasInforme.

Sizik
  • 1,066
  • 5
  • 11
0

if SalvaguardasAGR is List of Object (instead of AGRSalvaguardasInforme)

you ll have to iterate and do this manually

the links you posted go from List to List - specific to generic

You are doing the reverse thing, generic to specific

Kalpesh Soni
  • 6,879
  • 2
  • 56
  • 59