Incompatible generic wildcard captures

前端 未结 2 1531
情深已故
情深已故 2021-01-14 18:01

in the following snippet:

package test;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

public class WildcardsTest

        
2条回答
  •  醉梦人生
    2021-01-14 18:31

    Do it like this and it will work:

    private final Iterator>
    > iterator;
    

    You can still use the iterator like this:

    public void foo(){
        while(iterator.hasNext()){
            Entry> entry = iterator.next();
            Collection value = entry.getValue();
        }
    }
    

    For reference, read the get and put principle (originally from Java Generics and Collections)

提交回复
热议问题