Skip to content

4.3 Analyze the interoperability of collections that use raw types and generic types

Weverton edited this page Apr 6, 2017 · 4 revisions
public class Teste14 {
    
    public static void main(String[] args) {
        Parcel parcel = new Parcel<String>();// É permitido, mas apresenta warning
    }
}

class Parcel<T> {
    private T t;

    public void set(T t) {
        this.t = t;
    }

    public T get() {
        return t;
    }
}

class Twist4_2 {
    public static void main(String args[]) {
        CustomMap map = new CustomMap<Integer, String>(); // Gera um warning
        map.put(new String("1"), "Selvan"); // Gera um warning

        String strVal = map.get(new Integer(1)); // Gera um erro de compilação
        System.out.println(strVal); // 4
    }
}

interface MyMap<K, V> {
    void put(K key, V value);

    V get(K key);
}

class CustomMap<K, V> implements MyMap<K, V> {
    K key;
    V value;

    public void put(K key, V value) {
        this.key = key;
        this.value = value;
    }

    public V get(K key) {
        return value;
    }

Lembre que isso não pode:

List<Object> list = new ArrayList<String>();