'Print Different Object Data From ArrayList in Java
Is there some way to print different data from an ArrayList containing different objects?
For example, I created two basic classes:
import java.util.*;
class Furniture{
String name;
int weight;
Furniture(String name, int weight){
this.name = name;
this.weight = weight;
}
String getName(){
return name;
}
int getWeight(){
return weight;
}
}
}
class Resident{
String name;
Resident(String name){
this.name = name;
}
String getName(){
return name;
}
}
Then I stored them in an ArrayList<Object> and wanted to print the names, by using declared below printArrayList method:
public class Main{
public static <E> void printArrayList(ArrayList<E> arrayToPrint){
for(E element : arrayToPrint){
try{
System.out.println(element.getName());
}
catch(Exception e){
System.out.println("Exception e: " + e);
}
}
}
public static void main(String []args){
Furniture sofa = new Furniture("Sofa", 5);
Resident mike = new Resident("Mike");
ArrayList<Object> arrayList = new ArrayList<>();
arrayList.add(sofa);
arrayList.add(mike);
printArrayList(arrayList);
}
Now, I know that not all objects can have a variable name or declared get method, therefore I tried to exclude these cases by try/catch. I also tried to exclude it by using fe:
if(elements.getName() == null)
Still, same results.
Solution 1:[1]
You don't need to use a parameterized type. Rather introduce a specific interface (for example NameAware) that exposes the getName() method that your classes with implement. In this way you could rely on a common type.
public interface NameAware{
String getName();
}
public class Resident implements NameAware{
...
public String getName(){
return name;
}
}
public class Furniture implements NameAware{
...
public String getName(){
return name;
}
}
And define your method as :
public static void printArrayList(ArrayList<NameAware> arrayToPrint) {
for (NameAware element : arrayToPrint) {
try {
System.out.println(element.getName());
} catch (Exception e) {
System.out.println("Exception e: " + e);
}
}
}
Note that you should change your actual code from :
ArrayList<Object> arrayList = new ArrayList<>();
to
ArrayList<NameAware> arrayList = new ArrayList<>();
Solution 2:[2]
The best practice would be to declare an interface with the getName method, and have both Furniture and Resident implement it.
public interface Namable {
public String getName();
}
Then, use a List<Namable> instead of a List<Object>:
Furniture sofa = new Furniture("Sofa", 5);
Resident mike = new Resident("Mike");
List<Namable> arrayList = new ArrayList<>();
arrayList.add(sofa);
arrayList.add(mike);
for (Namable n : arrayList) {
System.out.println(n.getName());
}
Solution 3:[3]
We can iterate ArrayList by using 1 . Iterator 2. forEach loop 3. For loop
ArrayList arr store object of class User
ArrayList<User> arr = new ArrayList<User>();
Class User have getter and Setter
class User{
private String name;
private int age;
public User(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Now you can get element of list using iterator of User class type
Iterator<User> itr = arr.iterator();
while (itr.hasNext()) {
User user = itr.next();
System.out.println("Name="+user.getName()+" Age="+user.getAge());
}
Reference : Java ArrayList Example
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Mureinik |
| Solution 3 | Anuj Dhiman |
