这个方法声明有什么区别:
public static <E extends Number> List<E> process(List<E> nums){
Run Code Online (Sandbox Code Playgroud)
和
public static List<Number> process(List<Number> nums){
Run Code Online (Sandbox Code Playgroud)
你会在哪里使用前者?
为什么boolean containsAll(Collection <?> c); 每种类型都允许使用收集框架的方法吗?.但是 boolean addAll(Collection <?extends E> c); 允许?延伸E.所以,我写了一个澄清程序.这是我的计划
public class ContainAllTest {
// take ServiceDto
ArrayList<ServiceDto> resultList = new ArrayList<ServiceDto>();
void Test() {
ServiceDto serviceDto = new ServiceDto();
serviceDto.setName("test");
resultList.add(serviceDto);
// another arraylist that takes String
ArrayList<String> resultList1 = new ArrayList<String>();
resultList1.add("test");
// no error, goes for run time.Contain all checking is done for two generic type ServiceDto and String:
resultList.containsAll(resultList1);
// error shown at compile time,as addAll take ServiceDto as generic type …
Run Code Online (Sandbox Code Playgroud)