0

I have files Card.java and Util.java which I am not allowed to modify. For my own purposes, I created a class Card2 which extends Card, and adds an equals() method and a hashcode() method. The Util class contains a method which takes an ArrayList<Card> as a parameter. I have an ArrayList<Card2> and I want to run that method.

My understanding is that, in this case, Card2 has an "is-a" relationship with Card, so I would think there should be a way to convert the ArrayList<Card2> to ArrayList<Card>.

I'm unsure how to go about doing this conversion though. I tried (ArrayList<Card>) arr where arr is an ArrayList<Card2>, but I get an error stating these are inconvertible types. I realize I could create a new ArrayList<Card> and add each of the cards in arr independently, but I'm wondering whether there's a better way to go about this.

Bhaskar
  • 680
  • 7
  • 26
  • 1
    What I think you should be doing is, from the start, declaring an `ArrayList` but populating it with `Card2` objects as you create them. – Jim Garrison Jul 08 '20 at 23:13

3 Answers3

2

Given a list of Card2:

ArrayList<Card2> card2s;

Do this:

ArrayList<Card> cards = new ArrayList<>(card2s);

The constructor accepts a Collection<? extends Card>, which in English is a Collection of anything that is or extends Card.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
1
    ArrayList<Card2> card2s = new ArrayList<>();

    ArrayList<Card> cards = (ArrayList<Card>)card2s.stream().map(card2 -> {
        Card card = new Card();
        //your logic to take data from card2 variable and set it to the new Card2 object:
        //e.g. card.setType(card2.getType());
        return card;
    }).collect(Collectors.toList());
Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
  • I'm unsure how this converts an ArrayList arr to ArrayList – Bhaskar Jul 08 '20 at 22:06
  • It creates a stream of cards, and then maps each object of type `Card` to the new object of type `Card2`. You have to set the data or whatever you wish in each object in the commented area, and then it returns mapped object. Finally it collects it into collection. – Giorgi Tsiklauri Jul 08 '20 at 22:08
  • Yeah, I commented before you made the edit. I'm currently testing it in my code. – Bhaskar Jul 08 '20 at 22:10
  • Excuse me? before who made the edit? :) this is just a concept of how to convert one list into another. `cards2` is just an example data. – Giorgi Tsiklauri Jul 08 '20 at 22:12
  • Damn, I'm sorry :') For some reason I thought someone else had commented explaining your answer. – Bhaskar Jul 08 '20 at 22:13
  • Only thing I had to change is casting the result to an `ArrayList` from `List`. Other than that, it works! Thanks! If you can edit your answer so it includes that casting, then I can accept your answer. – Bhaskar Jul 08 '20 at 22:15
  • Done; however, it's much better to hold your variables type more generic like `List`, than specialized like `ArrayList`. – Giorgi Tsiklauri Jul 08 '20 at 22:18
  • Damn, just saw @Bohemian 's response, and it's much more elegant. Thanks for helping with your answer though! – Bhaskar Jul 08 '20 at 22:21
  • It's elegant; however, it will only work if you have an inheritance in the place. With my answer you can convert any object to any other type. – Giorgi Tsiklauri Jul 08 '20 at 22:22
  • Hm yeah. My question was asking specifically about scenarios where inheritance is used, but yeah, I can see this being useful in other contexts. – Bhaskar Jul 08 '20 at 22:24
1

If you have access to that util method, you can use wildcard character in input parameters as described...

https://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html

"There is a small but very important difference here: we have replaced the type List with List<? extends Shape>. Now drawAll() will accept lists of any subclass of Shape, so we can now call it on a List if we want"

If you don't have access to that method you can still cast items inside the list. Sample by Java stream api:

Is it possible to cast a Stream in Java 8?

Lonestar
  • 51
  • 6