0

I'm trying to cast a list of Objects to a list of my own class type. So far what I'm doing is:

List<Company> companies = (List<Company>)(Object) repository.findAllCompanyNames();

repository.findAllCompanyNames() is returning a List<Object>

My Company class looks like:

public class Company {

    public Company(){}

    public Company(int id, String name){
        this.id = id;
        this.name = name;
    }

    public int id;
    public String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

I don't get any error the problem is the value of companies after the cast, it is:

companies = {ArrayList@11743}  size = 4
 0 = {Object[2]@11753} 
  0 = {Integer@11761} "1"
  1 = "Company 1"
 1 = {Object[2]@11754} 
  0 = {Integer@11785} "3"
  1 = "Company 3"
 2 = {Object[2]@11755} 
  0 = {Integer@11779} "4"
  1 = "Company TT"
 3 = {Object[2]@11756} 
  0 = {Integer@11764} "5"
  1 = "Company 34"
 4 = {Object[2]@11757} 

and I think the companies list should contain Company objects like:

- Id: 1
- Name: Company 1
- etc ..

Any idea what it's going on?

gon250
  • 3,405
  • 6
  • 44
  • 75
  • but, from what is shown there, it does contain it? you´re just printing the additional hashcode from the class itself and the one from the `List`. – SomeJavaGuy Mar 07 '17 at 11:25
  • 6
    Just casting doesn't change the object at all... – Jon Skeet Mar 07 '17 at 11:25
  • What is your `repository.findAllCompanyNames` actually returning? If it isn't returning a Company casting doesn't change a thing. – M. Deinum Mar 07 '17 at 11:26
  • @JonSkeet what is the right way to change the object? I thought casting the list would change as well the object :S – gon250 Mar 07 '17 at 11:27
  • @M.Deinum ``repository.findAllCompanyNames`` return a ``List`` – gon250 Mar 07 '17 at 11:28
  • So what type of Object is returned there? Just `List` is pretty useless... – Jon Skeet Mar 07 '17 at 11:32
  • 2
    If casting would change the object, then every problem could be solved with this magic line `mySolutions = (List)(Object) myProblems; // if only` – Patrick Parker Mar 07 '17 at 11:35
  • Possible duplicate of [How to cast List to List](http://stackoverflow.com/questions/1917844/how-to-cast-listobject-to-listmyclass) – Ole V.V. Mar 07 '17 at 11:44

2 Answers2

4

No, the list shouldn't contain Company objects because you're not retrieving companies, but their names. The method is called findAllCompanyNames() and it returns a List<Object[]> (runtime type), with each element containing (presumably) the company id and the company name.

If you show the repository class, it should be more obvious and you'll probably find a method that returns the company objects you want.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
0

Are you able to use java-8? You can just transform a List<Object[]> into a List<Company> using stream:

List<Object[]> companies = Arrays.asList(
            new Object[]{1, "Company nam 1"}, 
            new Object[]{2, "Company nam 2"}
);

now we can convert companies to a List<Company>:

List<Company> companyList = companies.stream().map(
               o -> new Company((Integer) o[0], (String) o[1])
).collect(Collectors.toList());

I hope this helps.

Anton Balaniuc
  • 10,889
  • 1
  • 35
  • 53