Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the Java collection framework made of?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "what is the composition of the Java collection framework", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what is the composition of the Java collection framework" this article.

The first part introduces the collection framework.

The Java platform provides a new collection framework. The collection framework mainly consists of a set of interfaces used to manipulate objects. Different interfaces describe a set of different data types.

2003-11-30926010.bmp "align=absMiddle border=0 >

ASPectratio= "t" vgroup extt = "edit" >

Java 2 Collection Framework Diagram

Collection interface: 6 interfaces (represented by short dashed lines) that represent different collection types and are the basis of the collection framework.

Abstract classes: 5 abstract classes (represented by long dashed lines) that partially implement the collection interface. Can be extended to a custom collection class.

Implementation class: 8 implementation classes (solid line representation), the specific implementation of the interface.

To a large extent, once you understand the interface, you understand the framework. Although you always have to create an interface-specific implementation, access to the actual collection should be limited to the use of interface methods; therefore, it allows you to change the basic data structure without having to change other code.

The Collection interface is a set of objects that allow repetition.

The Set interface inherits Collection, but does not allow repetition, using its own internal arrangement mechanism.

The List interface inherits Collection, allows repetition, places elements in the order in which they are placed, and does not rearrange them.

The Map interface is a key-value object that makes up a pair, that is, what is held is key-value pairs. There can be no duplicate key in Map. Has its own internal arrangement mechanism.

The elements in the container are all of type object. When you get an element from a container, you must convert it to its original type.

Java 2 simplified set frame diagram

The second part is the collection interface

1.Collection interface

Used to represent any object or group of elements. Use this interface when you want to work with a set of elements as routinely as possible.

(1) add or delete a single element:

Boolean add (Object o): add objects to the collection

Boolean remove (Object o): if there is an object in the collection that matches o, delete the object o

(2) query operation:

Int size (): returns the number of elements in the current collection

Boolean isEmpty (): determines whether there are any elements in the collection

Boolean contains (Object o): find out if the collection contains objects o

Iterator iterator (): returns an iterator that accesses each element in the collection

(3) Group operation: acts on a group of elements or an entire collection

Boolean containsAll (Collection c): find out if the collection contains all the elements in collection c

Boolean addAll (Collection c): add all elements in collection c to the collection

Void clear (): delete all elements in the collection

Void removeAll (Collection c): removes all elements in collection c from the collection

Void retainAll (Collection c): removes elements from the collection that are not contained in collection c

(4) convert Collection to Object array:

Object [] toArray (): returns an array that contains all the elements of the collection

Object [] toArray (Object [] a): returns an array that contains all the elements of the collection. The array returned at run time is of the same type as parameter an and needs to be converted to the correct type.

In addition, you can convert the collection to any other array of objects. However, you cannot directly convert the collection to an array of basic data types, because the collection must hold objects.

"the italic interface method is optional. Because an interface implementation must implement all interface methods, the caller needs a way to know whether an optional method is not supported. If an UnsupportedOperationException is thrown when an optional method is called, the operation fails because the method is not supported. This exception class inherits the RuntimeException class and avoids putting all collection operations into try-catch blocks. "

Collection does not provide the get () method. If you want to traverse elements in Collectin, you must use Iterator.

1.1.AbstractCollection abstract class

The AbstractCollection class provides the basic functionality of the concrete Collection Framework class. Although you can implement all the methods of the Collection interface yourself, except that the iterator () and size () methods are implemented in the appropriate subclasses, all other methods are implemented by the AbstractCollection class. If the subclass does not override certain methods, optional methods such as add () will throw an exception.

1.2.Iterator interface

The iterator () method of the Collection interface returns an Iterator. The Iterator interface method iteratively accesses each element in the collection and securely removes the appropriate elements from the Collection.

(1) boolean hasNext (): determines whether there is another accessible element

Object next (): returns the next element to be accessed. If the end of the collection is reached, a NoSuchElementException exception is thrown.

(2) void remove (): delete the object returned by the last access. This method must be executed immediately after the access of an element. If the collection has been modified since the last access, the method throws an IllegalStateException.

"deletion in Iterator also has an impact on the underlying Collection. "

Iterators are fail-fast. This means that when another thread modifies the underlying collection, if you are traversing the collection with Iterator, Iterator throws a ConcurrentModificationException (another RuntimeException exception) exception and immediately fails.

2.List interface

The List interface inherits the Collection interface to define an ordered collection that allows duplicates. This interface not only processes part of the list, but also adds location-oriented operations.

(1) location-oriented operations include the ability to insert an element or Collection, as well as the ability to get, remove, or change elements. Searching for an element in List can start at the head or tail of the list, and if it is found, the location of the element will be reported:

Void add (int index, Object element): adds the element element to the specified location index

Boolean addAll (int index, Collection c): adds all elements of collection c to the specified location index

Object get (int index): returns the element at the specified location in the List

Int indexOf (Object o): returns the position of the first occurrence element o, otherwise returns-1

Int lastIndexOf (Object o): returns the position of the last occurrence element o, otherwise returns-1

Object remove (int index): deletes elements at a specified location

Object set (int index, Object element): replaces the element on the position index with the element element and returns the old element

(2) the List interface can not only iterate through the entire list with a sequence of locations, but also deal with a subset of the collection:

ListIterator listIterator (): returns a list iterator to access the elements in the list

ListIterator listIterator (int index): returns a list iterator to access the elements in the list starting from the specified location index

List subList (int fromIndex, int toIndex): returns a list view of the elements in the range from fromIndex (inclusive) to toIndex (exclusive) at the specified location

"changes to the sublist, such as add (), remove (), and set () calls, also affect the underlying List. "

2.1.ListIterator interface

The ListIterator interface inherits the Iterator interface to support adding or changing elements in the underlying collection, as well as two-way access. The ListIterator has no current position, and the cursor is between the value returned by calling the previous and the next method. A list of length n with 1 valid index value for n:

(1) void add (Object o): add object o to the front of the current location

Void set (Object o): replaces the previous element accessed by the next or previous method with the object o. If the list structure has been modified since the last call, an IllegalStateException exception will be thrown.

(2) boolean hasPrevious (): determines whether there are elements accessible when iterating backward

Object previous (): returns the previous object

Int nextIndex (): returns the index of the element that will be returned the next time the next method is called

Int previousIndex (): returns the index of the element that will be returned the next time the previous method is called

"normally, you don't have to ListIterator to change the direction of traversing collection elements at one time-forward or backward. Although technically possible, a call to next () immediately after previous () returns the same element. Reverse the order of calling next () and previous (), and the result is the same. "

"We also need to explain the add () operation a little bit. Adding an element causes the new element to be added immediately in front of the implicit cursor. Therefore, a call to previous () after adding an element returns the new element, while the call to next () has no effect, returning the next element before the add operation. "undefined

The above is all the content of the article "what is the Java Collection Framework made up of?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report