Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
126 views
in Technique[技术] by (71.8m points)

java - Assigning List of Integer Into a List of String

I was learning Generics in Java and I came close to a very interesting piece of code. I know in Java it is illegal to add list of one type to another.

List<Integer> integerList = new ArrayList<Integer>();
List<String> stringList=integerList;

So in the second line I get a compile time error.
But if I create a generic method inside a class like this,

class  GenericClass <E>{
    void genericFunction(List<String> stringList) {
        stringList.add("foo");
    }
    // some other code
}

And in the main class call the method with list of Integer I am not getting any error.

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

     GenericClass genericClass=new GenericClass();
     List<Integer> integerList= new ArrayList<Integer>();
     integerList.add(100);
     genericClass.genericFunction(integerList);
     System.out.println(integerList.get(0));
     System.out.println(integerList.get(1));
  }
}

Output
100
foo

Why I am not getting any error?

question from:https://stackoverflow.com/questions/25400690/assigning-list-of-integer-into-a-list-of-string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have mixed Generic with raw type. It will compile fine but at run-time it might fail because Generic information is lost at run-time.

Generic should be used to track such bugs at compile-time.

It's better explained at What is a raw type and why shouldn't we use it? in detail.


Warning: Type safety: The method genericFunction(List) belongs to the raw type GenericClass. References to generic type GenericClass<E> should be parameterized.

If you have two methods with same name with different Generic type of List then it results into compile time error. The compiler is unable to resolve Generic type in case of method arguments that can be proved by below sample code.

Sample code: (compiler error - not a valid overloaded method)

void genericFunction(List<String> stringList){...}
void genericFunction(List<Integer> stringList){...}

Make some changes and try it again:

class  GenericClass <E>{
    void genericFunction(List<E> stringList) {
        ...
    }
    // some other code
}

...

GenericClass<String> genericClass=new GenericClass<String>(); // Genreric object
List<Integer> integerList= new ArrayList<Integer>();
integerList.add(100);
genericClass.genericFunction(integerList); // compile time error

Create methods in such a way

class GenericClass<E> {
    private List<E> list = new ArrayList<E>();

    public void addAll(List<E> newList) {
        list.addAll(newList);
    }

    public void add(E e) {
        list.add(e);
    }

    public E get(int index) {
        return list.get(index);
    }
    // some other code
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...