Section 16 Collections and Generics
来源:互联网 发布:java英语面试题 编辑:程序博客网 时间:2024/05/22 15:34
Collections:
A collection (container) is an object that groups multiple elements into a single unit.
TreeSet: Keeps the elements sorted and prevents duplicates.Use red-black tree.
HashSet: Prevents duplicates.
LinkedList: Better performance for insertion and deletion
HashMap: Store name/value pairs. No duplicate keys. Can have duplicate values.
LinkedHashMap: Remember the order elements inserted.
Map is not a true collection.
Use Set /List/Map as the reference type instead of implementation types.
Set: prevent duplicates.
HashSet: hashtable.
TreeSet: Keep the elements sorted. Use red-black tree.
LinkedHashSet: Keep insertion order.
List:
ArrayList
LinkedList: Better performance for insertion and deletion
Vector
Deque: (deck)
Implement both stack and queue.
Map:
TreeMap:
HashMap:
LinkedHashMap:
HashTable:
1.hashMap去掉了HashTable 的contains方法,但是加上了containsValue()和containsKey()方法。
2.hashTable同步的,而HashMap是非同步的,效率上逼hashTable要高。
3.hashMap允许空键值,而hashTable不允许。
Ordering for sort:
Byte
Signed numericalCharacter
Unsigned numericalLong
Signed numericalInteger
Signed numericalShort
Signed numericalDouble
Signed numericalFloat
Signed numericalBigInteger
Signed numericalBigDecimal
Signed numericalBoolean
Boolean.FALSE < Boolean.TRUE
File
System-dependent lexicographic on path nameString
LexicographicDate
ChronologicalCollationKey
Locale-specific lexicographic1. public static <T extends Comparable<? super T>> void sort(List<T> list)
sort() method only takes comparable objects. T must be comparable. Implement Comparable interface.
2. public static <T> void sort(List<T> list, Comparator<? super T> c)
Use custom Comparator.
Generics: for type-safe collections
Generic Method:
public <T extends Animal> void takeThing(ArrayList<T> list)
HashSet 首先判断hashcode是否一致,利用hashcode产生数组下标。如果hashcode一致(可能是冲突),则调用equals()判断reference是否一样。
a.equals(b) implies a.hashCode()==b.hashCode(). Not verse.
If you override equals(), you must override hashcode().
Polymorphism:
public void takeAnimals(Animals[] animals); // can take subclass of Animal as arguments.public void takeAnimals(ArrayList<Animal> animals); // can take only take Animal class.
To avoid adding wrong type to the ArrayList, use wildcards:
public <T extends Animal> void takeThing(ArrayList<T> list)public void takeThing(ArrayList<? extends Animal> list)
- Section 16 Collections and Generics
- Collections and Generics
- Java Generics and Collections [ILLUSTRATED]
- (Oreilly)Java Generics and Collections 读书笔记一
- 《Java Generics and Collections》笔记-Lists/Maps
- 《Java Generics and Collections》笔记 Set
- 《Java Generics and Collections》笔记 Queues I
- 《Java Generics and Collections》笔记-Queue II
- 《Java Generics and Collections》读书笔记二:子类化与通配符
- Java Generics and Collections.pdf 英文原版 免费下载
- C#数据结构和算法[An Introduction to Collections, Generics, and the Timing Class]
- C++ Builder中使用System.Generics.Collections
- Comparing Java and C# Generics
- Generics and Wildcards in Java
- Delphi 2009 泛型容器单元(Generics.Collections)[2]: TQueue
- GENERICS
- Generics
- Generics
- windows程序设计 第5版又发现新错误
- 微动力WQ_V3.V20150311 UTF8商业版程序完整包免费下载
- struts2拦截器的实现原理及源码剖析
- struts2 session拦截器
- 拦截器(Interceptor)中的invocation.invoke()
- Section 16 Collections and Generics
- 《Java程序设计》实训每天一博文之第四天
- 使用struts2拦截器,对session失效的校验
- gradle DSL method not found: android()错误解决办法
- CreateDIBitmap与CreateDIBSection
- Codeforces#86D Powerful array(分块暴力)
- Jquery命名冲突解决的五种方案
- Can't create table './store/#sql-b2c_1a.frm' (errno: 150)解决方法
- 《Java面试试题》找出连续数组中相同的两个数值