java集合框架_Java集合框架

java集合框架

Java集合框架 (Java Collection Framework)

Java collection framework represents a hierarchy of set of interfaces and classes that are used to manipulate group of objects.

Java收集框架表示用于操作对象组的一组接口和类的层次结构。

Collections framework was added to Java 1.2 version. Prior to Java 2, Java provided adhoc classes such as Dictionary, Vector, Stack and Properties to store and manipulate groups of objects.

集合框架已添加到Java 1.2版本。 在Java 2之前,Java提供了自定义类,例如Dictionary,Vector,Stack和Properties,用于存储和操作对象组。

Java collections framework is contained in java.util package. It provides many important classes and interfaces to collect and organize group of objects.

Java集合框架包含在java.util包中。 它提供了许多重要的类和接口来收集和组织对象组。

什么是收藏 (What is collection)

Collection in java can be referred to an object that collects multiple elements into a single unit. It is used to store, fetch and manipulate data. For example, list is used to collect elements and referred by a list object.

Java中的Collection可以称为将多个元素收集到一个单元中的对象。 它用于存储,获取和操作数据。 例如,列表用于收集元素并由列表对象引用。

收集框架的组成部分 (Components of Collection Framework)

The collections framework consists of:

收集框架包括:

  • Collection interfaces such as sets, lists, and maps. These are used to collect different types of objects.

    集合接口,例如集合,列表和地图。 这些用于收集不同类型的对象。
  • Collection classes such as ArrayList, HashSet etc that are implementations of collection interfaces.

    集合类,例如ArrayList,HashSet等,它们是集合接口的实现。
  • Concurrent implementation classes that are designed for highly concurrent use.

    专为高度并发使用而设计的并发实现类。
  • Algorithms that provides static methods to perform useful functions on collections, such as sorting a list.

    提供静态方法以对集合执行有用功能(例如对列表进行排序)的算法。

收集框架的优势 (Advantage of Collection Framework)

  • It reduces programming effort by providing built-in set of data structures and algorithms.

    通过提供内置的数据结构和算法集,它减少了编程工作。
  • Increases performance by providing high-performance implementations of data structures and algorithms.

    通过提供数据结构和算法的高性能实现来提高性能。
  • Provides interoperability between unrelated APIs by establishing a common language to pass collections back and forth.

    通过建立公共语言来回传递集合,从而在不相关的API之间提供互操作性。
  • Increase Productivity

    提高生产率
  • Reduce operational time

    减少运营时间
  • versatility to work with current collection as well

    通用性也可以与当前馆藏一起使用

Collection API的重要接口 (Important Interfaces of Collection API)

Interface Description
Collection Enables you to work with groups of object; it is at the top of Collection hierarchy
Deque Extends Queue to handle double ended queue.
List Extends Collection to handle sequences list of object.
Queue Extends Collection to handle special kind of list in which element are removed only from the head.
Set Extends Collection to handle sets, which must contain unique element.
SortedSet Extends Set to handle sorted set.
接口 描述
采集 使您可以处理对象组; 它在集合层次结构的顶部
双端队列 扩展队列以处理双端队列。
清单 扩展Collection以处理对象的序列表。
队列 扩展Collection以处理特殊类型的列表,其中仅从头部删除元素。
将Collection扩展为处理集,该集必须包含唯一元素。
SortedSet 扩展Set以处理排序的set。

Java集合层次结构 (Java Collection Hierarchy )

Collection Framework hierarchy is represented by using a diagram. You can get idea how interfaces and classes are linked with each other and in what hierarchy they are situated. Top of the framework, Collection framework is available and rest of interfaces are sub interface of it.

使用图表示Collection Framework层次结构。 您可以了解接口和类如何相互链接以及它们位于什么层次结构中。 该框架的顶部是可用的Collection框架,其余接口是其子接口。

java集合框架_Java集合框架

Collection Heirarchy

集合层次结构

All these Interfaces give several methods which are defined by collections classes which implement these interfaces.

所有这些接口都提供了几种方法,这些方法由实现这些接口的集合类定义。

收集框架的常用方法 (Commonly Used Methods of Collection Framework)

Method Description
public boolean add(E e) It inserts an element in this collection.
public boolean addAll(Collection<? extends E> c) It inserts the specified collection elements in the invoking collection.
public boolean remove(Object element) It deletes an element from the collection.
public boolean removeAll(Collection<?> c) It deletes all the elements of the specified collection from the invoking collection.
default boolean removeIf(Predicate<? super E> filter) It deletes all the elements of the collection that satisfy the specified predicate.
public boolean retainAll(Collection<?> c) It deletes all the elements of invoking collection except the specified collection.
public int size() It returns the total number of elements in the collection.
public void clear() It removes the total number of elements from the collection.
public boolean contains(Object element) It searches for an element.
public boolean containsAll(Collection<?> c) It is used to search the specified collection in the collection.
方法 描述
public boolean add(E e) 它在此集合中插入一个元素。
公共布尔addAll(Collection <?扩展E> c) 它将指定的集合元素插入到调用集合中。
公共布尔remove(Object元素) 它从集合中删除一个元素。
公共布尔removeAll(Collection <?> c) 它从调用集合中删除指定集合的​​所有元素。
默认布尔值removeIf(Predicate <?super E>过滤器) 它删除集合中所有满足指定谓词的元素。
public boolean keepAll(Collection <?> c) 它将删除调用集合中除指定集合之外的所有元素。
public int size() 它返回集合中元素的总数。
公共无效clear() 它从集合中删除元素总数。
公共布尔包含(对象元素) 它搜索一个元素。
公共布尔containsAll(Collection <?> c) 用于搜索集合中的指定集合。

为什么将收藏设为通用? (Why Collections were made Generic?)

Generics added type safety to Collection framework. Earlier collections stored Object class references which meant any collection could store any type of object. Hence there were chances of storing incompatible types in a collection, which could result in run time mismatch. Hence Generics was introduced through which you can explicitly state the type of object being stored.

泛型为Collection框架添加了类型安全性 。 较早的集合存储了Object类引用,这意味着任何集合都可以存储任何类型的对象。 因此,有可能在集合中存储不兼容的类型,这可能导致运行时不匹配。 因此引入了泛型,您可以通过泛型明确声明要存储的对象的类型。

收藏和自动装箱 (Collections and Autoboxing)

We have studied that Autoboxing converts primitive types into Wrapper class Objects. As collections doesn't store primitive data types(stores only refrences), hence Autoboxing facilitates the storing of primitive data types in collection by boxing it into its wrapper type.

我们研究了自动装箱将原始类型转换为Wrapper类对象的情况。 由于集合不存储原始数据类型(仅存储引用),因此自动装箱通过将原始数据类型装箱到其包装器类型中,从而有助于在集合中存储原始数据类型。

Collections Framework中最常引发的异常 (Most Commonly thrown Exceptions in Collections Framework)

There may be chance of getting exceptions while working with collections. We have listed some most common exceptions that may occur during program execution.

使用集合时,可能会出现异常。 我们列出了程序执行期间可能发生的一些最常见的异常。

Exception Name Description
UnSupportedOperationException occurs if a Collection cannot be modified
ClassCastException occurs when one object is incompatible with another
NullPointerException occurs when you try to store null object in Collection
IllegalArgumentException thrown if an invalid argument is used
IllegalStateException thrown if you try to add an element to an already full Collection
例外名称 描述
UnSupportedOperationException 如果无法修改集合,则会发生
ClassCastException 当一个对象与另一对象不兼容时发生
空指针异常 当您尝试在集合中存储空对象时发生
IllegalArgumentException 如果使用了无效的参数则抛出
IllegalStateException 如果尝试将元素添加到已经完整的Collection中,则抛出该异常

翻译自: https://www.studytonight.com/java/collection-framework.php

java集合框架