关于java
2016-04-06 17:35
555 查看
1,生产者使用extends , List <extends T> , 因此你不能往列表里添加任何元素
2,消费者使用super, List <super T> , 因此你不能保证从中读取到的元素的类型
3,既是生产者又是消费者,不能用
例子:JDK 1.8 java.util.Collections里的copy方法
2,消费者使用super, List <super T> , 因此你不能保证从中读取到的元素的类型
3,既是生产者又是消费者,不能用
例子:JDK 1.8 java.util.Collections里的copy方法
/** * Copies the elements from the source list to the destination list. At the * end both lists will have the same objects at the same index. If the * destination array is larger than the source list, the elements in the * destination list with {@code index >= source.size()} will be unchanged. * * @param destination * the list whose elements are set from the source list. * @param source * the list with the elements to be copied into the destination. * @throws IndexOutOfBoundsException * when the destination list is smaller than the source list. * @throws UnsupportedOperationException * when replacing an element in the destination list is not * supported. */ public static <T> void copy(List<? super T> destination, List<? extends T> source) { if (destination.size() < source.size()) { throw new IndexOutOfBoundsException("destination.size() < source.size(): " + destination.size() + " < " + source.size()); } Iterator<? extends T> srcIt = source.iterator(); ListIterator<? super T> destIt = destination.listIterator(); while (srcIt.hasNext()) { try { destIt.next(); } catch (NoSuchElementException e) { // TODO: AssertionError? throw new IndexOutOfBoundsException("Source size " + source.size() + " does not fit into destination"); } destIt.set(srcIt.next()); } }
相关文章推荐
- JDK8新特性
- Java异常处理机制 try-catch-finally
- Java异常处理机制 try-catch-finally
- [Spring实战系列](19)Servlet不同版本之间的区别
- JAVA JDBC--源自技术
- java高级程序设计(第四周)
- 浅谈Java中的反射机制
- SpringMVC入门
- eclipse下web开发中缓存问题
- Java之绘制方法
- 如何求最两个正整数的大公约数【java实现】
- 详解Java的MyBatis框架中SQL语句映射部分的编写
- Java 位运算(移位、位与、或、异或、非)
- Java集合容器类
- Spring集成log4j日志管理
- 在Eclipse中配置Teamcenter二次开发环境时候debug,Tteamcenter不能正常登录
- MyEclipse控制台中文输出乱码
- java 时间工具类
- 如何调优JVM - 优化Java虚拟机(大全+实例)
- 如何调优JVM - 优化Java虚拟机(大全+实例)