您的位置:首页 > 编程语言 > Java开发

Java学习日记 集合

2015-10-27 19:14 441 查看
一、接口Map<K,V>
1.V put(K key, V value)
2.int size()
3.public class HashMap<K, V> implements Map<K, V>
按键的哈希值存储,put时,key相同会发生替换,一个key值只能对应一个value
4.Map的遍历
Map没有迭代器,通过keyset()和values()遍历
Set<K> keySet()
Collection<V> values()
//5.Set<Map.Entry<K,V>> entrySet()
public static interface Map.Entry<K,V>
6.V get(Object key)
7.LinkedHashMap
TreeMap

二、泛型
1.instanceOf
o instanceof Student
判断对象o是否属于Student类
2.泛型只能添加类类型,不能添加基本数据类型,但是基本数据类型会自动装箱拆箱
3.一个类可以有多个泛型
Map<String, Student>
List<List<Student>>
Map<String,List<Student>>

三、排序
1.Comparable<T>接口
一个类通过implements该接口,实现其 int compareTo(T o)方法,就可以定义该类的一种比较方法,返回一个int值
PS:这种排序实现把排序方法和类绑定,不如Comparator灵活
2.接口 Comparator<T>
int compare(T o1, T o2)
定义一个类,implements Comparator接口,实现compare方法,将这个类的对象传递给sort方法(如 Collections.sort 或 Arrays.sort),实现自定义排序规则,完成对List等的排序

四、Collections
1. java.util.Collections
2.Collections类中包含大量静态方法,实现对集合的多种操作
3.static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)
Returns the maximum element of the given collection, according to the natural ordering of its elements.

五、集合相关
1.集合转数组
Collection接口中有toArray方法 <T> T[] toArray(T[] a)
具体的集合中都实现了该方法,可以直接将集合转为数组
2.数组转集合
没有现成方法,自己写
3.增强for遍历集合
可以使用for-each循环对集合进行遍历
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: