Java 集合框架 学习笔记
2024-07-15 11:34:52 0 举报
AI智能生成
Java 集合框架(List,Set,Map) 学习笔记
作者其他创作
大纲/内容
Collection接口
方法
boolean add(E element)
Iterator iterator()
int size()
boolean isEmpty()
boolean contains(Object obj)
boolean containsAll(Collection c)
boolean addAll(Collection from)
boolean remove(Object obj)
boolean removeAll(Collection c)
void clear()
boolean retainAll(Collection c):删除所有与c集合中的元素不同的元素
Object[] toArray()
T[] toArray(T[] arrayToFill)
子类
List接口
ArrayList:动态列表
LinkedList:可以在任何位置进行高效插入和删除操作的有序列表
Deque接口
方法
void addFirst(E item)
void addLast(E item)
boolean offerFirst(E item)
boolean offerLast(E item)
E removeFirst()
E removeLast()
E pollFirst()
E pollLast()
E getFirst()
E getLast()
E peekFirst()
E peekLast()
子类
ArrayDeque:双端队列
Queue接口
方法
boolean add(E item)
boolean offer(E item)
E remove()
E poll()
E element()
E peek()
子类
PriorityQueue:优化级队列
Set接口
HashSet:没有重复元素的无序集合
TreeSet:没有重复元素的有序集合
EnumSet:包含枚举类型值的集合
LinkedHashSet:可以记住元素插入顺序的集合
SortedSet
E first():返回集合中最小的元素
E last():返回集合中最大的元素
NavigableSet
E higher(E value)
E lower(E value)
E ceiling(E value)
E floor(E value)
E pollFirst()
E pollLast()
Map接口
Map.Entry 静态内部类
K getKey()
V getValue()
V setValue(V newValue)
方法
Set keySet()
Collection values()
Set> entrySet()
V get(Object key)
V put(K key, V value)
void putAll(Map entries)
boolean containsKey(Object key)
boolean containsValue(Object value)
子类
HashMap:存储键值关联的数据结构
TreeMap:键值有序排列的映射表
EnumMap:键值属于枚举类型的映射表
LinkedHashMap:可以记住键值添加顺序的映射表
SortedMap
K firstKey():返回最小元素
K lastKey():返回最大元素
WeakHashMap:如果值以后不再被使用则可以被GC回收的映射表
IdentityHashMap:用 == 而不是equals比较键值的映射表
Iterator接口
E next()
boolean hasNext()
void remove()
排序
Comparable接口
作用:让一个类实现此接口,那么这个类的集合就具有了排序功能
方法
int compareTo(T other)
Comparator接口
作用:让一个类实现此接口,这个类可以根据自己的定义的属性灵活的进行排序
方法
int compare(T a, T b)
Collections 工具类
void sort(List items):对列表中的元素排序
void sort(List items, Comparator c):对列表中的元素排序
void shuffle(List items):随机地打乱列表中的元素
void shuffle(List items, Random r):随机地打乱列表中的元素
void reverse(List items):逆转列表中元素的顺序
Comparator reverseOrder():返回一个比较器
Comparator reverseOrder(Comparator c):返回一个比较器
T min(Collection items)
T max(Collection items)
T min(Collection items, Comparator c)
T max(Collection items, Comparator c)
void copy(List to, List from)
void fill(List l, T value)
boolean addAll(Collection items, T... values)
boolean replaceAll(List items, T oldValue, T newValue)
int frequency(Collection items, Object o):返回列表中与对象o相同的元素个数
boolean disjoint(Collection c1, Collection c2):如果两个集合没有共同的元素,则返回true
Collection synchronizedCollection(CollectionM c)
List synchronizedList(List c)
Set synchronizedSet(Set c)
SortedSet synchronizedSortedSet(SortedSet c)
Map synchronizedMap(Map map)
SortedMap synchronizedSortedMap(SortedMap map)
Arrays 工具类
List asList(E... array):返回一个数组元素的列表视图。
0 条评论
下一页