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

How to use the Java collection

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

Share

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

This article will explain in detail how to use the Java collection. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

First of all, let's take a look at the main types of Java collections (set, list, map), where the parent class of set and list is Collection.

Map and Conllection have no inheritance relationship.

1 、 list:

Characteristics

A) repeating elements can be added

B) in order

List subclass (that is, the class subclass we usually call inherits the characteristics of list):

ArrayList: query fast add, delete and slow down the underlying layer is array structure thread unsafe (the basic capacity is expanded by 10 to 1.5x) (question: the difference between array and ArrayList

Array must specify the data type and the size of the array when defining the array

ArrayList can add different types of elements without specifying a size)

LinkedList: add, delete, modify, fast query slow bottom layer is (double) linked list structure thread unsafe (because the linked list structure has no initialization size and no expansion mechanism to add data all the time)

His weakness is both the strength of the other party.

Vector (rarely used, at least I don't use it): the underlying structure is also an array structure that is thread-safe

* 2. Set

Characteristics

A) do not save duplicate elements

B) No order

Set subclass

HashSet: the fastest element to add can be null, but only one null

TreeSet: sort in ascending order according to the comparison results of the added elements (treeset I think it can be thought of that the tree menu is arranged in order)

LinkedHashSet: saving in the order in which they are added

3 、 Queue

Queue FIFO (extension: stack stack FIFO)

And Collerction is the inherited iterable interface (iterator) iterator

Can enhance for loop (foreach)

(iterator traversal) there is a remove () method between collection and iterable. The difference between them is that one has collection and the other iterator has no parameters, which is more efficient than iterator (for example: collection deletes an item if you want to find it one by one from the collection, Delete if you need to delete the second, you need to start from scratch, while iterator is to delete the eligible items one by one and then continue to find the next one without having to start from scratch. The second aspect is fault tolerance, that is, the location of the subscript data may change after the remove (o) method provided by collection deletes the data item. The subscript does not change after the current item is deleted by the remove () method provided by iterator

Map:

HashMap: the fastest access speed for unordered storage (initialization capacity is 16, loading factor is 0.75, capacity expansion is twice the original capacity)

TreeMap: save in ascending order according to the comparison results of elements-keys (year-on-year TreeSet)

LinkedHashMap: saving keys in insertion order preserves the query speed of HashMap

This is the end of the article on "how to use the Java collection". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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