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 basic introduction of List?

2025-01-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article is to share with you about the basic introduction of List, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Architecture of List

List, which inherits from the interface of Collection, is a collection of ordered queue elements, each of which can be obtained through a subscript index.

AbstractList is an abstract class, which inherits most of the functions in List implemented by AbstractCollection,AbstractCollection, and provides convenience for the implementation class of List.

The basic implementations of List are mainly ArrayList, LinkedList and Vector.

ArrayList

The underlying implementation is based on an array and can be thought of as an array that can be dynamically resized.

Find / update elements are quickly implemented based on subscript, and the time complexity is O (1).

When adding / deleting an operation: if the header of the set is operated, all the remaining elements need to be moved, and the time complexity is O (n). If only one operation is required at the end of the set, the time complexity is O (1). Because the probability knows that each operation needs to be performed 2 times on average, so the time complexity is O (n), so the time complexity of add / delete operation is O (n).

LinkedList

The bottom layer is realized through a two-way linked list.

During the find / update operation: if only one operation is needed at the head or tail of the set, the time complexity is O (1); if the middle position of the operation set is in the middle of the set, the time complexity is O (n), so the time complexity of the find / update operation is O (n).

Add / delete operation: you need to locate the element before doing the operation, so the time complexity is O (n) consistent with the find / update.

Vector

It is basically the same as ArrayList and is implemented through an array.

Thread safety is achieved through synchronize keyword modification.

The default growth policy is 2x, and the growth factor can be customized (ArrayList is 1.5x, not customizable)

List's source code parsing public interface List extends Collection {/ * * returns the number of elements in the current collection If the number of elements in the collection is greater than Integer.MAX_VALUE, return Integer.MAX_VALUE * * / int size () directly; / * * return whether the current collection does not contain any elements * * / boolean isEmpty (); / * * return an Object array containing all elements in the current collection * * / Object [] toArray () / * * returns a specified type T array containing all elements in the current collection * * / T [] toArray (T [] a); / * * returns whether the current collection contains the specified element o * * / boolean contains (Object o); / * * returns whether the current collection contains all elements in the specified collection c * / boolean containsAll (Collection c) / * * add elements e * * / boolean add (E e) to the current collection; / * * add elements e * * / void add (int index, E e) to the specified location index in the current collection; / * * add all elements in the specified collection c to the current collection * * / boolean addAll (Collection c) / * * only the elements that exist in both the current collection and the specified collection c are retained * * / boolean retainAll (Collection c); / * remove all elements in the current collection * * / void clear (); / * * replace each element in the collection with the result of the operation of that element JDK1.8 new method * * / default void replaceAll (UnaryOperator operator) {Objects.requireNonNull (operator); final ListIterator li = this.listIterator (); while (li.hasNext ()) {li.set (operator.apply (li.next ();}} / * * sort collections according to the given collation; JDK1.8 new method * / default void sort (Comparator

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: 290

*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

Internet Technology

Wechat

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

12
Report