前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Java学习笔记——集合

Java学习笔记——集合

作者头像
梦飞
发布于 2022-06-23 04:25:09
发布于 2022-06-23 04:25:09
29100
代码可运行
举报
文章被收录于专栏:csdn文章同步csdn文章同步
运行总次数:0
代码可运行

文章目录

存储对象可以使用数组 (基本数据类型 & 引用数据类型) 和集合 (引用数据类型),用数组存储对象的弊端有:一旦创建,其长度不可变;数组中真正存储的对象个数不可知,除非自定义类。使用集合可以解决这些问题。

1 Java集合框架

JDK提供的集合API位于java.util包内。Java 集合可分为 Collection 和 Map 两种体系。

  • Collection (interface) JDK不提供此接口的任何直接实现,而是提供更具体的子接口 (如:Set和List) 实现。
    • List (interface):元素有序、可重复的集合(动态数组)。
      • ArrayList (主要实现类,首选)
      • LinkedList (对于频繁的插入、删除操作)
      • Vector (古老的实现类、线程安全的,但效率低于ArrayList)
    • Set (interface):元素无序、不可重复的集合。
      • HashSet (主要实现类)
        • LinkedHashSet
      • SortedSet (interface)
        • TreeSet
  • Map (interface):具有映射关系的 “key-value对” 的集合。
    • HashMap (主要实现类)
      • LinkedHashMap
    • SortedMap (interface)
      • TreeMap
    • Hashtable
      • Properties
  • Queue (interface)

对象排序接口:

  • Comparable
  • Comparator

容器工具类:

  • Collections

在 Java5 之前,Java 集合会丢失容器中所有对象的数据类型,把所有对象都当成 Object 类型处理;从 Java5 增加了泛型以后,Java 集合可以记住容器中对象的数据类型。

2 Collection接口API

方法摘要

boolean

add(E e) 确保此 collection 包含指定的元素(可选操作)。

boolean

addAll(Collection<? extends E> c) 将指定 collection 中的所有元素都添加到此 collection 中(可选操作)。

void

clear() 移除此 collection 中的所有元素(可选操作)。

boolean

contains(Object o) 如果此 collection 包含指定的元素,则返回 true。

boolean

containsAll(Collection<?> c) 如果此 collection 包含指定 collection 中的所有元素,则返回 true。

boolean

equals(Object o) 比较此 collection 与指定对象是否相等。

int

hashCode() 返回此 collection 的哈希码值。

boolean

isEmpty() 如果此 collection 不包含元素,则返回 true。

Iterator<E>

iterator() 返回在此 collection 的元素上进行迭代的迭代器。

boolean

remove(Object o) 从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。

boolean

removeAll(Collection<?> c) 移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。

boolean

retainAll(Collection<?> c) 仅保留此 collection 中那些也包含在指定 collection 的元素(可选操作)。

int

size() 返回此 collection 中的元素数。

Object[]

toArray() 返回包含此 collection 中所有元素的数组。

<T> T[]

toArray(T[] a) 返回包含此 collection 中所有元素的数组;返回数组的运行时类型与指定数组的运行时类型相同。

增(add, addAll)、删(clear, remove, removeAll),size,contains、containsAll,equals,isEmpty

3 遍历

4 List接口

List集合类中元素有序、且可重复,集合中的每个元素都有其对应的顺序索引。

要求List集合中的元素重写equals方法,才能适当地进行操作(如remove(Object obj)等)。

List 集合里添加了一些根据索引来操作集合元素的方法:

  • void add(int index, Object ele)
  • boolean addAll(int index, Collection eles)
  • Object get(int index)
  • int indexOf(Object obj)
  • int lastIndexOf(Object obj)
  • Object remove(int index)
  • protected void removeRange(int fromIndex, int toIndex)
  • Object set(int index, Object ele)
  • List subList(int fromIndex, int toIndex)

JDK API中List接口的实现类常用的有:ArrayList、LinkedList和Vector。

4.1 ArrayList

ArrayList 是线程不安全的,而 Vector 是线程安全的,即使为保证 List 集合线程安全,也不推荐使用Vector。

Arrays.asList(…) 方法返回的 List 集合既不是 ArrayList 实例,也不是 Vector 实例。 Arrays.asList(…) 返回值是一个固定长度的 List 集合。

  • JDK API 1.7

Modifier and Type

Method and Description

boolean

add(E e) Appends the specified element to the end of this list.

void

add(int index, E element) Inserts the specified element at the specified position in this list.

boolean

addAll(Collection<? extends E> c) Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection’s Iterator.

boolean

addAll(int index, Collection<? extends E> c) Inserts all of the elements in the specified collection into this list, starting at the specified position.

void

clear() Removes all of the elements from this list.

Object

clone() Returns a shallow copy of this ArrayList instance.

boolean

contains(Object o) Returns true if this list contains the specified element.

void

ensureCapacity(int minCapacity) Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.

E

get(int index) Returns the element at the specified position in this list.

int

indexOf(Object o) Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

boolean

isEmpty() Returns true if this list contains no elements.

Iterator<E>

iterator() Returns an iterator over the elements in this list in proper sequence.

int

lastIndexOf(Object o) Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.

ListIterator<E>

listIterator() Returns a list iterator over the elements in this list (in proper sequence).

ListIterator<E>

listIterator(int index) Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.

E

remove(int index) Removes the element at the specified position in this list.

boolean

remove(Object o) Removes the first occurrence of the specified element from this list, if it is present.

boolean

removeAll(Collection<?> c) Removes from this list all of its elements that are contained in the specified collection.

protected void

removeRange(int fromIndex, int toIndex) Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.

boolean

retainAll(Collection<?> c) Retains only the elements in this list that are contained in the specified collection.

E

set(int index, E element) Replaces the element at the specified position in this list with the specified element.

int

size() Returns the number of elements in this list.

List<E>

subList(int fromIndex, int toIndex) Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.

Object[]

toArray() Returns an array containing all of the elements in this list in proper sequence (from first to last element).

<T> T[]

toArray(T[] a) Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.

void

trimToSize() Trims the capacity of this ArrayList instance to be the list’s current size.

4.2 LinkedList

对于频繁的插入或删除元素的操作,建议使用LinkedList类,效率较高。

新增方法:

  • void addFirst(Object obj)
  • void addLast(Object obj)
  • Object getFirst()
  • Object getLast()
  • Object removeFirst()
  • Object removeLast()

4.3* Vector

Vector 是一个古老的集合,JDK1.0就有了。大多数操作与ArrayList相同,区别之处在于Vector是线程安全的。

新增方法:

  • void addElement(Object obj)
  • void insertElementAt(Object obj,int index)
  • void setElementAt(Object obj,int index)
  • void removeElement(Object obj)
  • void removeAllElements()

在各种list中,最好把ArrayList作为缺省选择。当插入、删除频繁时,使用LinkedList;Vector总是比ArrayList慢,所以尽量避免使用。

4.4* ListIterator

List 额外提供了一个 listIterator() 方法,该方法返回一个 ListIterator 对象, ListIterator 接口继承了 Iterator 接口,提供了专门操作 List 的方法:

  • void add()
  • boolean hasPrevious()
  • Object previous()
  • Boolean hasNext()
  • Object next()

Iterator和ListIterator主要异同点:

  1. ListIterator和Iterator都有hasNext()和next()方法,可以实现顺序向后遍历。但是ListIterator有hasPrevious()和previous()方法,可以实现逆向(顺序向前)遍历。Iterator就不可以。
  2. ListIterator可以定位当前的索引位置,nextIndex()和previousIndex()可以实现。Iterator 没有此功能。
  3. ListIterator有add()方法,可以向List中插入对象,而Iterator不能。
  4. 都可实现删除对象,但是ListIterator可以实现对象的修改,set()方法可以实现。Iterator仅能遍历,不能修改。因为ListIterator的这些功能,可以实现对LinkedList等List数据结构的操作。

总结:

    • 都可以顺序向后遍历 (hasNext()和next()) 查
    • 都可以删除对象 (remove()) 删
    • ListIterator可以逆序遍历 (hasPrevious()和previous()) 查
    • ListIterator可以定位当前索引 (nextIndex()和previousIndex())
    • ListIterator可以插入数据 (add()) 增
    • ListIterator可以实现对象的修改 (set()) 改

即两者都有查、删;ListIterator多了增、改,加强了查。

5 Set接口

  1. 无序性:不等于随机性,无序性指的是元素在底层存储的位置是无序的。
  2. 不可重复性:不可添加Set中已有的元素。(要求添加进Set中的元素所在的类,一定要重写equals()和hashCode()方法,以保证Set元素的不可重复性)

5.1 HashSet

HashSet 按 Hash 算法来存储集合中的元素,因此具有很好的存取和查找性能。

当向 HashSet 集合中存入一个元素时,HashSet 会调用该对象的 hashCode() 方法来得到该对象的 hashCode 值,然后根据 hashCode 值决定该对象在 HashSet 中的存储位置。

HashSet 集合判断两个元素相等的标准:两个对象通过 hashCode() 方法比较相等,并且两个对象的 equals() 方法返回值也相等。重写hashCode()的原则是要尽量保证与equals()一致,即hashCode不同的两个对象,应不相等,hashCode相同,应相等。

5.2 LinkedHashSet

LinkedHashSet 是 HashSet 的子类,也是根据元素的 hashCode 值来决定元素的存储位置,但它同时使用链表维护元素的次序,这使得元素看起来是以插入顺序保存的。

LinkedHashSet插入性能略低于 HashSet(因为需要维护链表),但在迭代访问 Set 里的全部元素时有很好的性能。

5.3 TreeSet

TreeSet 是 SortedSet 接口的实现类,TreeSet 可以确保集合元素处于排序状态。

向TreeSet中添加的必须是同一个类的对象,并且这个类必须实现Comparable接口并实现compareTo(Object obj)方法(或使用Comparator),TreeSet中的两个对象通过该方法的返回值来比较大小。设计时应使hashCode、equals、compareTo或compare保持一致。

当向TreeSet中添加自定义类的对象时,有两种排序方法:①自然排序②定制排序

5.3.1 自然排序

TreeSet 会调用集合元素的 compareTo(Object obj) 方法来比较元素之间的大小关系,然后将集合元素按升序排列。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public class Person implements Comparable {
	private String name;
	private Integer age;
    
	...
        
	@Override
	public int compareTo(Object obj) {
		if (obj instanceof Person) {
			Person person = (Person) obj;
			int i = this.age.compareTo(person.getAge());
			if(i == 0) {
				return this.name.compareTo(person.getName());
			}
			return i;
		}
		return 0;
	}
	
}
5.3.2 定制排序

实现Comparator中的int compare(T o1,T o2)方法,比较o1和o2的大小:如果方法返回正整数,则表示o1大于o2;如果返回0,表示相等;返回负整数,表示o1小于o2。

将实现了Comparator接口的对象作为形参传递给TreeSet的构造器。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
// 1.创建一个实现了Comparator接口的类对象
Comparator comparator = new Comparator() {
    @Override
    public int compare(Object o1, Object o2) {
        if (o1 instanceof Customer && o2 instanceof Customer) {
            Customer c1 = (Customer) o1;
            Customer c2 = (Customer) o2;
            int i = c1.getId().compareTo(c2.getId());
            if(i == 0) {
                return c1.getName().compareTo(c2.getName());
            }
            return i;
        }
        return 0;
    }
};
// 2.将此对象作为形参传递给TreeSet的构造器
TreeSet set = new TreeSet(comparator);
...

6 Map接口

Map与Collection并列存在。用于保存具有映射关系的数据: Key-Value。

Map 中的 key 和 value 可以是任何引用类型的数据。

Map 中的 key 用 Set 存放,对key的类的要求与Set中元素类似。

常用String类作为Map的“键”。

当后添加的元素的key值与已有的key重复时,后来的数据会覆盖原有的数据。

Modifier and Type

Method and Description

void

clear() Removes all of the mappings from this map (optional operation).

boolean

containsKey(Object key) Returns true if this map contains a mapping for the specified key.

boolean

containsValue(Object value) Returns true if this map maps one or more keys to the specified value.

Set<Map.Entry<K,V>>

entrySet() Returns a Setview of the mappings contained in this map.

boolean

equals(Object o) Compares the specified object with this map for equality.

V

get(Object key) Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

int

hashCode() Returns the hash code value for this map.

boolean

isEmpty() Returns true if this map contains no key-value mappings.

Set<K>

keySet() Returns a Set view of the keys contained in this map.

V

put(K key, V value) Associates the specified value with the specified key in this map (optional operation).

void

putAll(Map<? extends K,? extends V> m) Copies all of the mappings from the specified map to this map (optional operation).

V

remove(Object key) Removes the mapping for a key from this map if it is present (optional operation).

int

size() Returns the number of key-value mappings in this map.

Collection<V>

values() Returns a Collectionview of the values contained in this map.

  • 添加、删除操作:
    • Object put(Object key,Object value)
    • Object remove(Object key)
    • void putAll(Map t)
    • void clear()
  • 元视图操作的方法:
    • Set keySet()
    • Collection values()
    • Set entrySet()
  • 元素查询的操作:
    • Object get(Object key)
    • boolean containsKey(Object key)
    • boolean containsValue(Object value)
    • int size()
    • boolean isEmpty()
    • boolean equals(Object obj)

6.1 HashMap

HashMap 判断两个 key 相等的标准是:两个 key 通过 equals() 方法返回 true,hashCode 值也相等。 HashMap 判断两个 value相等的标准是:两个 value 通过 equals() 方法返回 true。

遍历
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Map map = new HashMap();
map.put("AA", 213);
map.put("AB", 213);
map.put("BB", 456);
map.put(123, "CC");
map.put(null, null);
map.put(new Person("DD", 12), null);

// 遍历key集
Set set = map.keySet();
for(Object obj : set) {
    System.out.println(obj);
}

// 遍历value
Collection values = map.values();
for(Object obj : values) {
    System.out.println(obj);
}

// 遍历key-value
// 方式一
for(Object obj : set) {
    System.out.println(obj + ": " + map.get(obj));
}

// 方式二
Set entrySet = map.entrySet();
for(Object obj : entrySet) {
    Map.Entry entry = (Entry) obj;
    System.out.println(entry);
}

6.2 LinkedHashMap

与LinkedHashSet类似,使用链表维护添加进Map中的顺序,遍历Map时是按添加的顺序遍历的。

6.3 TreeMap

对key进行排序,对key的要求可参考TreeSet。

6.4* Hashtable

古老的 Map 实现类,线程安全,不允许使用 null 作为 key 和 value。不建议使用。

6.5 Properties

Hashtable 的子类,用于处理属性文件。

由于属性文件里的 key、value 都是字符串类型,所以 Properties 里的 key 和 value 都是字符串类型。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Properties properties = new Properties();
properties.load(new FileInputStream(new File("test.properties")));
System.out.println(properties.getProperty("user"));

7 Collections工具类

Collections 是一个操作 Set、List 和 Map 等集合的工具类。

Collections 中提供了一系列静态的方法对集合元素进行排序、查询和修改等操作,还提供了对集合对象设置不可变、对集合对象实现同步控制等方法。

  • 排序操作
    • reverse(List)
    • shuffle(List)
    • sort(List)
    • sort(List,Comparator)
    • swap(List,int, int)
  • 查找、替换
    • Object max(Collection)
    • Object max(Collection,Comparator)
    • Object min(Collection)
    • Object min(Collection,Comparator)
    • int frequency(Collection,Object)
    • void copy(List dest,List src)
    • boolean replaceAll(List list,Object oldVal,Object newVal)
  • 同步控制 Collections 类中提供了多个 synchronizedXxx() 方法,该方法可使将指定集合包装成线程同步的集合,从而可以解决多线程并发访问集合时的线程安全问题。

Modifier and Type

Method and Description

static <T> boolean

addAll(Collection<? super T> c, T... elements) Adds all of the specified elements to the specified collection.

static <T> Queue<T>

asLifoQueue(Deque<T> deque) Returns a view of a Deque as a Last-in-first-out (Lifo) Queue.

static <T> int

binarySearch(List<? extends Comparable<? super T>> list, T key) Searches the specified list for the specified object using the binary search algorithm.

static <T> int

binarySearch(List<? extends T> list, T key, Comparator<? super T> c) Searches the specified list for the specified object using the binary search algorithm.

static <E> Collection<E>

checkedCollection(Collection<E> c, Class<E> type) Returns a dynamically typesafe view of the specified collection.

static <E> List<E>

checkedList(List<E> list, Class<E> type) Returns a dynamically typesafe view of the specified list.

static <K,V> Map<K,V>

checkedMap(Map<K,V> m, Class<K> keyType, Class<V> valueType) Returns a dynamically typesafe view of the specified map.

static <E> Set<E>

checkedSet(Set<E> s, Class<E> type) Returns a dynamically typesafe view of the specified set.

static <K,V> SortedMap<K,V>

checkedSortedMap(SortedMap<K,V> m, Class<K> keyType, Class<V> valueType) Returns a dynamically typesafe view of the specified sorted map.

static <E> SortedSet<E>

checkedSortedSet(SortedSet<E> s, Class<E> type) Returns a dynamically typesafe view of the specified sorted set.

static <T> void

copy(List<? super T> dest, List<? extends T> src) Copies all of the elements from one list into another.

static boolean

disjoint(Collection<?> c1, Collection<?> c2) Returns true if the two specified collections have no elements in common.

static <T> Enumeration<T>

emptyEnumeration() Returns an enumeration that has no elements.

static <T> Iterator<T>

emptyIterator() Returns an iterator that has no elements.

static <T> List<T>

emptyList() Returns the empty list (immutable).

static <T> ListIterator<T>

emptyListIterator() Returns a list iterator that has no elements.

static <K,V> Map<K,V>

emptyMap() Returns the empty map (immutable).

static <T> Set<T>

emptySet() Returns the empty set (immutable).

static <T> Enumeration<T>

enumeration(Collection<T> c) Returns an enumeration over the specified collection.

static <T> void

fill(List<? super T> list, T obj) Replaces all of the elements of the specified list with the specified element.

static int

frequency(Collection<?> c, Object o) Returns the number of elements in the specified collection equal to the specified object.

static int

indexOfSubList(List<?> source, List<?> target) Returns the starting position of the first occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.

static int

lastIndexOfSubList(List<?> source, List<?> target) Returns the starting position of the last occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.

static <T> ArrayList<T>

list(Enumeration<T> e) Returns an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration.

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.

static <T> T

max(Collection<? extends T> coll, Comparator<? super T> comp) Returns the maximum element of the given collection, according to the order induced by the specified comparator.

static <T extends Object & Comparable<? super T>> T

min(Collection<? extends T> coll) Returns the minimum element of the given collection, according to the natural ordering of its elements.

static <T> T

min(Collection<? extends T> coll, Comparator<? super T> comp) Returns the minimum element of the given collection, according to the order induced by the specified comparator.

static <T> List<T>

nCopies(int n, T o) Returns an immutable list consisting of n copies of the specified object.

static <E> Set<E>

newSetFromMap(Map<E,Boolean> map) Returns a set backed by the specified map.

static <T> boolean

replaceAll(List<T> list, T oldVal, T newVal) Replaces all occurrences of one specified value in a list with another.

static void

reverse(List<?> list) Reverses the order of the elements in the specified list.

static <T> Comparator<T>

reverseOrder() Returns a comparator that imposes the reverse of the natural ordering on a collection of objects that implement the Comparable interface.

static <T> Comparator<T>

reverseOrder(Comparator<T> cmp) Returns a comparator that imposes the reverse ordering of the specified comparator.

static void

rotate(List<?> list, int distance) Rotates the elements in the specified list by the specified distance.

static void

shuffle(List<?> list) Randomly permutes the specified list using a default source of randomness.

static void

shuffle(List<?> list, Random rnd) Randomly permute the specified list using the specified source of randomness.

static <T> Set<T>

singleton(T o) Returns an immutable set containing only the specified object.

static <T> List<T>

singletonList(T o) Returns an immutable list containing only the specified object.

static <K,V> Map<K,V>

singletonMap(K key, V value) Returns an immutable map, mapping only the specified key to the specified value.

static <T extends Comparable<? super T>> void

sort(List<T> list) Sorts the specified list into ascending order, according to the natural ordering of its elements.

static <T> void

sort(List<T> list, Comparator<? super T> c) Sorts the specified list according to the order induced by the specified comparator.

static void

swap(List<?> list, int i, int j) Swaps the elements at the specified positions in the specified list.

static <T> Collection<T>

synchronizedCollection(Collection<T> c) Returns a synchronized (thread-safe) collection backed by the specified collection.

static <T> List<T>

synchronizedList(List<T> list) Returns a synchronized (thread-safe) list backed by the specified list.

static <K,V> Map<K,V>

synchronizedMap(Map<K,V> m) Returns a synchronized (thread-safe) map backed by the specified map.

static <T> Set<T>

synchronizedSet(Set<T> s) Returns a synchronized (thread-safe) set backed by the specified set.

static <K,V> SortedMap<K,V>

synchronizedSortedMap(SortedMap<K,V> m) Returns a synchronized (thread-safe) sorted map backed by the specified sorted map.

static <T> SortedSet<T>

synchronizedSortedSet(SortedSet<T> s) Returns a synchronized (thread-safe) sorted set backed by the specified sorted set.

static <T> Collection<T>

unmodifiableCollection(Collection<? extends T> c) Returns an unmodifiable view of the specified collection.

static <T> List<T>

unmodifiableList(List<? extends T> list) Returns an unmodifiable view of the specified list.

static <K,V> Map<K,V>

unmodifiableMap(Map<? extends K,? extends V> m) Returns an unmodifiable view of the specified map.

static <T> Set<T>

unmodifiableSet(Set<? extends T> s) Returns an unmodifiable view of the specified set.

static <K,V> SortedMap<K,V>

unmodifiableSortedMap(SortedMap<K,? extends V> m) Returns an unmodifiable view of the specified sorted map.

static <T> SortedSet<T>

unmodifiableSortedSet(SortedSet<T> s) Returns an unmodifiable view of the specified sorted set.

8 总结

  • List (有序<可用下标访问>、可重复) 首选使用ArrayList,随机访问效率高; 当频繁使用插入、删除操作时,使用LinkedList; Vector虽然线程安全,但效率较低,且Collections工具类能解决线程安全的问题,所以基本不使用Vector。
  • Set (无序、不可重复) Set会根据hashCode()和equals()方法判断两个元素是否相等(重复),所以放入Set的元素应重写这个方法,以达到期望的排除“重复”元素的效果。 LinkedHashSet插入性能略低于 HashSet(因为需要维护链表),但在迭代访问 Set 里的全部元素时有很好的性能。遍历集合元素时,是按照添加进去的顺序实现的;频繁的遍历,较少的添加、插入操作建议选择。 TreeSet是排序的集合,所以得有一个比较的操作,有两种方式现实,一种是实现Comparable并实现其中的compareTo方法,另一种是创建一个实现Comparator的类对象并将其作为实参传给TreeSet的构造器。
  • Map 可用keySet(),values(),entrySet()对其进行遍历。
  • Collections 一个用来操作集合的工具类,需要进行一些集合操作时可以考虑使用。

Collection可用Iterator遍历,List首选ArrayList,Set首选HashSet,Map首选HashMap。

List元素类该重写equals,Set元素类和Map的key所在类该重写equals和hashCode,TreeSet和TreeMap涉及到排序,所以还得有比较的操作。

此外,为解决元素存储的安全性问题,和读取数据时需要类型强转的问题,使代码更加健壮和简介,常使用泛型

注:以上笔记参考自尚硅谷

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-01-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
docker访问宿主机_docker容器获取宿主机ip
我们需要让宿主机的mysql允许远程接入。 需要授权,不同版本的mysql授权语句不一样,这个在之前讲过。 如下是mysql8.0之前版本的授权语句:
全栈程序员站长
2022/09/25
3.4K0
docker访问宿主机_docker容器获取宿主机ip
宿主机访问容器ip_docker宿主机和容器
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/171599.html原文链接:https://javaforall.cn
全栈程序员站长
2022/09/24
6K0
docker复制文件到宿主机_下面哪几个属于docker网络模式
此种方式是将容器的某个端口映射到宿主机的某个端口,其它主机访问容器提供的服务需要通过宿主机的IP进行访问:
全栈程序员站长
2022/09/25
8400
docker复制文件到宿主机_下面哪几个属于docker网络模式
docker 镜像启动命令_宿主机ping不通docker
使用docker镜像nginx:latest以后台模式启动一个容器,并将容器命名为mynginx。
全栈程序员站长
2022/11/09
2.6K0
同宿主机暴露多个docker容器IP
使用docker时,如果想从局域网访问容器,比较常用的方式是将容器的网络模式设置为host模式,或者使用端口映射。但如果想部署多个应用并使用相同的端口,前面这两种方式就不适用了。
DifficultWork
2023/01/31
2.3K2
宿主机ping不通docker容器_kali虚拟机ping不通
Docker网络模式分为四种,一般我们不设置时默认为bridge单桥模式,容器使用独立的network Namespace,并连接到docker0虚拟网卡中。通过docker0网桥以及Iptables nat表配置与宿主机通信。   此时在堡垒机上进行测试,利用busybox进行测试:
全栈程序员站长
2022/09/25
6.5K0
宿主机ping不通docker容器_kali虚拟机ping不通
不同宿主机docker 通信_如何设置同网段IP
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/171609.html原文链接:https://javaforall.cn
全栈程序员站长
2022/09/24
8350
docker for windows 容器和宿主机 ip互通
Docker官方推荐我们通过端口映射的方式把Docker容器的服务提供给宿主机或者局域网其他容器使用。一般过程是:
Laikee
2022/04/25
3.8K0
docker for windows 容器和宿主机 ip互通
虚拟机与宿主机网络配置——可互通可上网「建议收藏」
大家好,又见面了,我是你们的朋友全栈君。 为了学习和使用Linux,多数人选择了使用虚拟机的方式来安装Linux系统。这样我们就可以在windows系统中安装Linux系统了,其中windows机器系统本身我们称作宿主机,安装的虚拟机系统我们简称虚拟机。
全栈程序员站长
2022/09/24
15.9K1
虚拟机与宿主机网络配置——可互通可上网「建议收藏」
docker访问宿主机端口_docker 访问宿主机局域网
有时候就需要在docker容器里访问宿主机提供的服务。 例如容器里的应用需要访问宿主机的mysql服务。
全栈程序员站长
2022/09/25
4.3K0
docker 访问宿主局域网_docker链接宿主数据库
例如你的62616964757a686964616fe4b893e5b19e31333433626437docker环境的虚拟IP是192.168.99.100,那么宿主机同样会托管一个和192.168.99.100同网段的虚拟IP,并且会是主IP:192.168.99.1,那么就简单了,在容器中访问192.168.99.1这个地址就等于访问宿主机。
全栈程序员站长
2022/09/25
2.4K0
2020-09-07:Docker的四种网络类型?
1.bridge模式:使用–net =bridge指定,默认设置。桥接式网络模式(默认)。
福大大架构师每日一题
2020/09/07
1.9K0
docker网络配置方法总结
docker启动时,会在宿主主机上创建一个名为docker0的虚拟网络接口。默认选择172.17.42.1/16,一个16位的子网掩码给容器提供了65534个IP地址。docker0仅仅是一个在绑定到这上面的其它网卡间自己主动转发数据包的虚拟以太网桥,它能够使容器和主机相互通信,容器与容器间通信。问题是,怎样让位于不同主机上的docker容器能够通信。怎样有效配置docker网络眼下来说还是一个较复杂的工作,因而也涌现了非常多的开源项目来解决问题,如flannel、Kubernetes、weave、pipework等等。
全栈程序员站长
2022/02/11
7620
虚拟机与宿主机网络[通俗易懂]
桥接方式下,虚拟机和宿主机处于同一网段,真实存在于网络中,像是一台真实的主机。虚拟机和宿主机彼此互通,且网络中的其他主机也可以互通。就像是连接在hub中的主机一样。获取的IP地址网段为:192.168.1.X,实际获取的为192.168.1.220。
全栈程序员站长
2022/09/25
2.1K0
虚拟机与宿主机网络[通俗易懂]
Docker网络,网络工程师还不赶紧收藏!
Evth-pair就是一对的虚拟设备接口,他们都是成对出现的,一段连着协议,一段彼此相连.正因为这个特性,evth-pair 充当一个桥梁,连接各种虚拟网络设备。
全栈程序员站长
2022/09/09
9730
Docker网络,网络工程师还不赶紧收藏!
Docker容器跨主机通讯的几种方式
此时docker引擎会创建一个veth对,一端连接到容器实例并命名为eth0,另一端连接到指定的网桥中(比如docker0),因此同在一个主机的容器实例由于连接在同一个网桥中,它们能够互相通信。容器创建时还会自动创建一条SNAT规则,用于容器与外部通信时,类似家里上网用的ISP提供给我们的动态IP。如果用户使用了-p或者-P端口,还会创建对应的端口映射规则,使得外部请求能够访问容器的服务,但是你不能通过IP直接访问,本文提供了3种方式实现容器的跨主机访问。
1850810
2021/06/06
2.4K0
mac下docker从容器内部访问宿主机ip[通俗易懂]
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/171581.html原文链接:https://javaforall.cn
全栈程序员站长
2022/09/25
2.5K0
docker无法访问宿主机_docker访问宿主机端口
已通过docker启动mongodb,监听端口为27017. 直接启动应用(不通过docker)可以正常访问到mongodb,但是通过docker访问却不行,访问的url为: mongodb://127.0.0.1:27017或mongodb://localhost:27017
全栈程序员站长
2022/09/25
7.3K0
Docker容器跨主机通信之:直接路由方式
直观上看,要实现网络通信,机器需要至少一个网络接口(物理接口或虚拟接口)与外界相通,并可以收发数据包;此外,如果不同子网之间要进行通信,需要额外的路由机制。
py3study
2020/02/20
17.3K6
Docker容器学习梳理--容器间网络通信设置(Pipework和Open vSwitch)
自从Docker容器出现以来,容器的网络通信就一直是被关注的焦点,也是生产环境的迫切需求。容器的网络通信又可以分为两大方面:单主机容器上的相互通信,和跨主机的容器相互通信。下面将分别针对这两方面,对容器的通信原理进行简单的分析,帮助大家更好地使用docker。前面已经在Docker容器学习梳理--基础知识(2)这一篇中详细介绍了Docker的网络配置以及pipework工具。 docker单主机容器通信 基于对net namespace的控制,docker可以为在容器创建隔离的网络环境,在隔离的网络环境下,
洗尽了浮华
2018/01/23
3.6K0
Docker容器学习梳理--容器间网络通信设置(Pipework和Open vSwitch)
推荐阅读
相关推荐
docker访问宿主机_docker容器获取宿主机ip
更多 >
目录
  • 文章目录
  • 1 Java集合框架
  • 2 Collection接口API
  • 3 遍历
  • 4 List接口
    • 4.1 ArrayList
    • 4.2 LinkedList
    • 4.3* Vector
    • 4.4* ListIterator
  • 5 Set接口
    • 5.1 HashSet
    • 5.2 LinkedHashSet
    • 5.3 TreeSet
      • 5.3.1 自然排序
      • 5.3.2 定制排序
  • 6 Map接口
    • 6.1 HashMap
      • 遍历
    • 6.2 LinkedHashMap
    • 6.3 TreeMap
    • 6.4* Hashtable
    • 6.5 Properties
  • 7 Collections工具类
  • 8 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档