Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert list of objects to list of interfaces?

I have some class that works with interfaces:

Here is the interface:

public interface Orderable {     int getOrder()     void setOrder() } 

Here is the worker class:

public class Worker {    private List<Orderable> workingList;     public void setList(List<Orderable> value) {this.workingList=value;}     public void changePlaces(Orderable o1,Orderable o2)    {      // implementation that make o1.order=o2.order and vice versa    } } 

Here is an object that implements the interface:

public class Cat implements Orderable {     private int order;      public int getOrder()     {       return this.order;     }      public void setOrder(int value)     {       this.order=value;     }      public Cat(String name,int order)     {        this.name=name;        this.order=order;     } } 

In the main procedure I create a list of cats. I use glazed lists to dynamically update controls when the list is changed and when a control model is created with this list.

The goal is to transfer this list to a worker object, so I can add some new cat to the list in the main procedure, and the worker will know about it without setting its list property again (list is same object in main proc and in worker). But when I call worker.setList(cats) it alerts about expecting an Orderable, but getting a Cat... but Cat implements Orderable. How do I solve this?

Here is the main code:

void main() {    EventList<Cat> cats=new BasicEventList<Cat>();     for (int i=0;i<10;i++)    {       Cat cat=new Cat("Maroo"+i,i);       cats.add(cat);    }     Worker worker=new Worker();     worker.setList(cats); // wrong!    // and other very useful code } 
like image 753
Artyom Chernetsov Avatar asked Sep 27 '11 16:09

Artyom Chernetsov


People also ask

How do I convert a List of strings to a List of objects?

Pass the List<String> as a parameter to the constructor of a new ArrayList<Object> . List<Object> objectList = new ArrayList<Object>(stringList); Any Collection can be passed as an argument to the constructor as long as its type extends the type of the ArrayList , as String extends Object .

Can we create object for List interface?

Since List is an interface, we cannot create objects from it. In order to use functionalities of the List interface, we can use these classes: ArrayList.

What is List<> in Java?

The List interface in Java provides a way to store the ordered collection. It is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements.

Can we convert List to object?

A list can be converted to a set object using Set constructor. The resultant set will eliminate any duplicate entry present in the list and will contains only the unique values. Set<String> set = new HashSet<>(list);


1 Answers

You need to change the Worker class so that it accepts List<? extends Orderable>

public class Worker {    private List<? extends Orderable> workingList;     public void setList(List<? extends Orderable> value) {this.workingList=value;}     public void changePlaces(Orderable o1,Orderable o2)    {      // implementation that make o1.order=o2.order and vice verca      } } 
like image 164
Saintali Avatar answered Oct 13 '22 00:10

Saintali