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?

8
  • 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. Commented Mar 7, 2017 at 11:25
  • 6
    Just casting doesn't change the object at all... Commented Mar 7, 2017 at 11:25
  • What is your repository.findAllCompanyNames actually returning? If it isn't returning a Company casting doesn't change a thing. Commented Mar 7, 2017 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 Commented Mar 7, 2017 at 11:27
  • 2
    If casting would change the object, then every problem could be solved with this magic line mySolutions = (List<Solutions>)(Object) myProblems; // if only Commented Mar 7, 2017 at 11:35

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.