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 function of the collection framework in java

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "what is the role of the collection framework in java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The collection framework is a unified standard architecture for representing and manipulating collections. Any set framework contains three pieces of content: the external interface, the implementation of the interface and the algorithm for set operation.

Java Collection Framework:

1. What is a framework: a collection of class libraries

two。 Collection framework: a unified architecture for representation and operation, including interfaces and classes that implement collections

3. Collection: a container in which data is stored

The collection framework consists of two parts: one is the interface and the other is the class.

4. Why interfaces appear: because many classes in the collection framework have similar functions [so use interfaces to standardize classes]

Main structure diagram:

Note: in the Collection Framework, interfaces Map and Collection have no relationship in the hierarchy, they are completely different.

Don't simply think that these are the opportunities for collections. There are a lot of collections in jdk, which are just what we often use.

Collection, List, Set, Queue, and Map are all Interface, not concrete class implementations.

I. list [public interface Listextends Collection]:

A. an ordered collection interface that precisely controls the insertion position of each element in the list. Users can access elements based on their integer index (position in the list) and search for elements in the list.

B. unlike set, lists usually allow duplicate elements. More formally, lists usually allow elements E1 and e2 to satisfy e1.equals (e2). And if the list itself allows null elements, they usually allow multiple null elements. It is inevitable that some people want to prohibit duplicate lists by throwing a runtime exception when the user tries to insert a repeating element.

The C.List interface adds some other conventions to the protocols of the iterator, add, remove, equals, and hashCode methods to further standardize the conventions specified in the Collection interface. For convenience, declarations of other inheritance methods are also included here

The D.List interface provides four access methods for locating (indexing) list elements. Lists (like Java arrays) are based on 0. Note that these operations may be performed in a time proportional to the index value of some implementations, such as the LinkedList class. Therefore, if the caller does not know the implementation, iterating on the list element is usually better than traversing the list with an index

e. In addition to the iterator () method that is necessary for the Collection interface, List also provides a listIterator () method that returns a ListIterator interface. Compared to the standard Iterator interface, ListIterator has more methods such as add (), allowing you to add, delete, set elements, and traverse forward or backward.

As for List, it provides the following methods:

There are many List interface implementation classes:

AbstractList, AbstractSequentialList, ArrayList, AttributeList, CopyOnWriteArrayList, LinkedList, RoleList, RoleUnresolvedList, Stack, Vector are generally mainly used by ArrayList, and LinkedList. Other classes are not useless.

ArrayList

ArrayList allows all elements, including null. ArrayList is out of sync

Understanding 1: ArrayList uses a built-in array to store elements, with a starting capacity of 10. 0. When the array needs to grow, the new capacity is obtained according to the following formula: new capacity = (old capacity * 3) / 2x1, which means that the capacity will increase by about 50% each time. This means that if you have an ArrayList object that contains a large number of elements, then eventually a lot of space will be wasted, which is caused by the way ArrayList works. If there is not enough space to store the new elements, the array will have to be reassigned so that new elements can be added.

Reallocating the array will result in a sharp degradation in performance. If we know how many elements an ArrayList will have, we can specify the capacity through the constructor. We can also use the trimToSize method to get rid of wasted space after the ArrayList is allocated.

Understanding 2: ArrayList is implemented in an array, which is not a real linked list. When initializing, it sets an initial capacity for the array. When the array space is insufficient, it reconstructs a larger array and then copies the previous elements into it.

Whether it's one or two, regardless of the way he stores the elements. You can confirm that he uses the built-in array.

LinkedList

The linked list implementation of the List interface. Implement all optional list operations and allow all elements (including null). In addition to implementing the List interface

The LinkedList class also provides a unified naming method for the get, remove, and insert elements at the beginning and end of the list. These operations allow linked lists to be used as stacks, queues, or double-ended queues (deque). This class implements the Queue interface and provides first-in-first-out queue operation for add, poll, etc. Other stack and double-ended queue operations can be easily cast again based on standard list operations. Although they may run slightly faster than equivalent list operations, they are included here mainly for convenience.

This is the end of the content of "what is the purpose of the collection framework in java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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