Reputation: 3
I try to add two arraylist into single arraylist. all arraylist object type is different.
here my two arraylist-
ArrayList<NewsItem> topnewslist=new ArrayList<>();
ArrayList<LatestNewsInfo> latestnewslist=new ArrayLis();
ArrayList<AllNewsInfo> allnewslist=new ArrayList<>();// add here
// I try this
allnewslist.addAll(topnewslist); // not add bcoz differnt type of object
allnewslist.addAll(latestnewslist); //not add bcoz differnt type of object
// Note: all object data model value is same.
What is best solution for this??
Upvotes: 0
Views: 1060
Reputation: 15244
ArrayList<Object> allnewslist=new ArrayList<>();
.
Ideally both NewsItem
and LatestNewsInfo
should inherit common interface, and then you can have the list of this interface.
interface News{} //can be an abstract class
class NewsItem implements News{
}
class LatestNewsInfo implements News{
}
//edit - define variables to interface instead of actual implementation object
List<News> allnewslist=new ArrayList<>();
Upvotes: 6