Can any class that implements java.io.serializable be added to an array list of generic type <Serializable>

StackOverflow https://stackoverflow.com/questions/19472884

  •  01-07-2022
  •  | 
  •  

سؤال

Description:

I have an ArrayList that takes any class which implements Serializable. I am even able to add instances of my classes that implement Serializable to this array list without any compilation errors. How does Java Generics handle adding these serializable objects to this List?

I am aware of the fact that we cannot define a reference List<Object> l

However I don't see any compilation problems with my List<Serializable> serializables reference.

My second question I have here is how do I retrieve my elements in these objects that are added to this serializable list?

If an answer has already been posted for a similar question, please post your links.

/***Here is my main class***/

import java.io.Serializable;
import java.util.*;

public class LearnArrayList {
    public static void main(String[] args) {

        List<Serializable> serializables = new ArrayList<Serializable>();
        serializables.add(new RandomClass1());
        serializables.add(new RandomClass2());

    }
}

/***Here is my RandomClass1 that I will add it to List<Serializable>***/

import java.io.Serializable;

public class RandomClass1 implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private int a;
    private int b;

/*Getters and setters here*/
}

/***Here is my RandomClass2 that I will add it to List<Serializable>***/
import java.io.Serializable;

public class RandomClass2 implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String s;

/*Getters and setters here*/
}
هل كانت مفيدة؟

المحلول 2

Please refer to below code snippet. I hope this is self explanatory.

    public static void main(String[] args) {
    List<Serializable> serializables = new ArrayList<Serializable>();
    serializables.add(new RandomClass1());
    serializables.add(new RandomClass2());
    process(serializables);
    process2(serializables);//no issues

    List<RandomClass1> randomClassList = new ArrayList<RandomClass1>();
    randomClassList.add(new RandomClass1());
    randomClassList.add(new RandomClass1());
    process(randomClassList);
    process2(randomClassList);//compile time error in this line
}


private static void process(List<? extends Serializable> list){
    Iterator<? extends Serializable> itr = list.iterator();
    while (itr.hasNext()){
        Serializable s = itr.next();
        System.out.println("process1: " + s.getClass());
    }
}    

private static void process2(List<Serializable> list){
    Iterator<Serializable> itr = list.iterator();
    while (itr.hasNext()){
        Serializable s = itr.next();
        System.out.println("process2: " + s.getClass());
    }
}

Output is

process1: RandomClass1

process1: RandomClass2

process2: RandomClass1

process2: RandomClass2

process1: RandomClass1

process1: RandomClass1

نصائح أخرى

How does Java Generics handle adding these serializable objects to this List?

The compiler checks what you are adding is a Serializable.

I am aware of the fact that we cannot define a reference List l

Actually you can.

However I don't see any compilation problems with my List serializables reference.

I don't see why you would get compilation errors either.

My second question I have here is how do I retrieve my elements in these objects that are added to this serializable list?

The same way you would any list. With get(n) or iterate over them.

But what is the right way to look at the elements in my class after retrieving the objects from my list.

The right way is the simplest and clearest way to implement what your program has to do.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top