java集合类用来干什么?------java对数据结构的支持以及对映射关系的数据的支持,用来装对象的地址值信息
1集合类用来存储有数据结构关系的数据
集合类是对数据结构的抽象实现,是对动态顺序存储结构/链式存储结构的实现,集合类是用来实现具体的某种数据结构并用它存储数据的
2集合类是用来存储有映射关系的关联数组
java的集合类都放在哪里?多线程下安全吗?
所有的集合类放在java.util包下,后来为了支持多线程安全,在java.utill.concurrent下存放了支持线程安全的集合类
集合里只能保存引用变量------即对象的地址值
数组可以保存基本变量的值,也可以保存引用变量的值,即对象的地址值
java集合类的体系结构?
两大超级父接口:Collection对数据结构的支持 Map对映射关系的数据的支持
1Collection对数据结构的支持?
Collection父接口,集合的通用方法
public interface Collection<E> extends Iterable<E> {
//定义了Collection集合的通用方法
int size();
boolean isEmpty();
boolean contains(Object o);
Iterator<E> iterator();
Object[] toArray();
boolean add(E e);
boolean remove(Object o);
boolean containsAll(Collection<?> c);
boolean addAll(Collection<? extends E> c);
boolean removeAll(Collection<?> c);
@since 1.8从jdk8开始接口支持有方法体,必须为static/default修饰
default boolean removeIf(Predicate<? super E> filter) {
Objects.requireNonNull(filter);
boolean removed = false;
final Iterator<E> each = iterator();
while (each.hasNext()) {
if (filter.test(each.next())) {
each.remove();
removed = true;
}
}
return removed;
}
boolean retainAll(Collection<?> c);
void clear();
boolean equals(Object o);
int hashCode();
}
Iterator接口------遍历Collection集合的接口实现
public interface Iterator<E> {
//遍历Collection集合的接口实现
boolean hasNext();
E next();
@jdk1.8
default void remove() {
throw new UnsupportedOperationException("remove");
}
default void forEachRemaining(Consumer<? super E> action) {
Objects.requireNonNull(action);
while (hasNext())
action.accept(next());
}
}
使用foreach遍历集合
foreach(Object obj:集合名字){
}
List接口继承Collection父接口,增加一些List接口专有的方法, 【有序集合,元素可以重复】
public interface List<E> extends Collection<E> {
//定义了Collection集合的通用方法
int size();
boolean isEmpty();
Iterator<E> iterator();
Object[] toArray();
boolean add(E e);
boolean containsAll(Collection<?> c);
boolean addAll(Collection<? extends E> c);
boolean addAll(int index, Collection<? extends E> c);
boolean removeAll(Collection<?> c);
boolean retainAll(Collection<?> c);
@since 1.8从jdk8开始接口支持有方法体,必须为static/default修饰
default void replaceAll(UnaryOperator<E> operator) {
Objects.requireNonNull(operator);
final ListIterator<E> li = this.listIterator();
while (li.hasNext()) {
li.set(operator.apply(li.next()));
}
}
default void sort(Comparator<? super E> c) {
Object[] a = this.toArray();
Arrays.sort(a, (Comparator) c);
ListIterator<E> i = this.listIterator();
for (Object e : a) {
i.next();
i.set((E) e);
}
}
void clear();
boolean equals(Object o);
int hashCode();
//定义List集合接口专有的方法
E get(int index);
E set(int index, E element);
void add(int index, E element);
E remove(int index);
int indexOf(Object o);
int lastIndexOf(Object o);
ListIterator<E> listIterator();
ListIterator<E> listIterator(int index);
List<E> subList(int fromIndex, int toIndex);
}
实现类:
ArrayList
Vector
LinkedList
Set接口继承父接口Collection,没有增加一些Set接口专有的方法, 【无序集合,元素不可以重复】
public interface Set<E> extends Collection<E> {
int size();
boolean isEmpty();
Iterator<E> iterator();
Object[] toArray();
boolean add(E e);
boolean remove(Object o);
boolean containsAll(Collection<?> c);
boolean addAll(Collection<? extends E> c);
boolean retainAll(Collection<?> c);
boolean removeAll(Collection<?> c);
void clear();
boolean equals(Object o);
int hashCode();
}
实现类:
HashSet
TreeSet
EnumSet
Queue接口继承父接口Collection,增加一些Queue接口专有的方法, 【队列】
public interface Queue<E> extends Collection<E> {
boolean add(E e);
//Queue特有的方法
boolean offer(E e);
E remove();
E poll();
E element();
E peek();
}
实现类:
PriorityQueue
Deque
ArrayQueue
LinkedList
剩下的就是对各个子接口List/Queue/Set的实现类喽嗯~ o(* ̄▽ ̄*)o
2 Map对映射关系的数据的支持
Map父接口,提供Map集合通用方法,内部还有一Entry接口
public interface Map<K,V> {
//Map集合通用方法
int size();
boolean isEmpty();
boolean containsKey(Object key);
boolean containsValue(Object value);
V get(Object key);
V put(K key, V value);
V remove(Object key);
void putAll(Map<? extends K, ? extends V> m);
void clear();
Set<K> keySet();
Collection<V> values();
Set<Map.Entry<K, V>> entrySet();
interface Entry<K,V> {//内部接口
K getKey();
V getValue();
V setValue(V value);
boolean equals(Object o);
int hashCode();
//接口也有方法体实现@jdk1.8
public static <K extends Comparable<? super K>, V> Comparator<Map.Entry<K,V>> comparingByKey() {
return (Comparator<Map.Entry<K, V>> & Serializable)
(c1, c2) -> c1.getKey().compareTo(c2.getKey());
}
public static <K, V extends Comparable<? super V>> Comparator<Map.Entry<K,V>> comparingByValue() {
return (Comparator<Map.Entry<K, V>> & Serializable)
(c1, c2) -> c1.getValue().compareTo(c2.getValue());
}
public static <K, V> Comparator<Map.Entry<K, V>> comparingByKey(Comparator<? super K> cmp) {
Objects.requireNonNull(cmp);
return (Comparator<Map.Entry<K, V>> & Serializable)
(c1, c2) -> cmp.compare(c1.getKey(), c2.getKey());
}
public static <K, V> Comparator<Map.Entry<K, V>> comparingByValue(Comparator<? super V> cmp) {
Objects.requireNonNull(cmp);
return (Comparator<Map.Entry<K, V>> & Serializable)
(c1, c2) -> cmp.compare(c1.getValue(), c2.getValue());
}
}
boolean equals(Object o);
int hashCode();
default V getOrDefault(Object key, V defaultValue) {
V v;
return (((v = get(key)) != null) || containsKey(key))
? v
: defaultValue;
}
default void forEach(BiConsumer<? super K, ? super V> action) {
Objects.requireNonNull(action);
for (Map.Entry<K, V> entry : entrySet()) {
K k;
V v;
try {
k = entry.getKey();
v = entry.getValue();
} catch(IllegalStateException ise) {
// this usually means the entry is no longer in the map.
throw new ConcurrentModificationException(ise);
}
action.accept(k, v);
}
}
default void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
Objects.requireNonNull(function);
for (Map.Entry<K, V> entry : entrySet()) {
K k;
V v;
try {
k = entry.getKey();
v = entry.getValue();
} catch(IllegalStateException ise) {
// this usually means the entry is no longer in the map.
throw new ConcurrentModificationException(ise);
}
// ise thrown from function is not a cme.
v = function.apply(k, v);
try {
entry.setValue(v);
} catch(IllegalStateException ise) {
// this usually means the entry is no longer in the map.
throw new ConcurrentModificationException(ise);
}
}
}
default V putIfAbsent(K key, V value) {
V v = get(key);
if (v == null) {
v = put(key, value);
}
return v;
}
default V merge(K key, V value,
BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
Objects.requireNonNull(value);
V oldValue = get(key);
V newValue = (oldValue == null) ? value :
remappingFunction.apply(oldValue, value);
if(newValue == null) {
remove(key);
} else {
put(key, newValue);
}
return newValue;
}
}
Map下没有子接口,只有对父接口Map的实现类
实现类:
HashMap
HashTable
LinkedHashMap
SortedMap
TreeMap
IdentityMap
EnumMap