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

The usage of Java data structure List and Map

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

Share

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

This article mainly explains "the usage of Java data structure List and Map". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the usage of Java data structure List and Map".

1: collection Collection (single column set) List (ordered, repeatable) ArrayList underlying data structure is an array, fast query, slow deletion thread is unsafe, efficient Vector underlying data structure is array, fast query, slow deletion thread safe, inefficient LinkedList underlying data structure is linked list, slow query, add and delete fast thread is unsafe, efficient Set (out of order) The only) HashSet underlying data structure is a hash table (which is actually an instance of HashMap). Hash table depends on two methods: hashCode () and equals () execution order: first determine whether the hashCode () value is the same: continue to execute equals (), see its return value is true: indicate that the elements are duplicated, do not add is false: add directly to the collection No: add directly to the collection and finally: automatically generate hashCode () and equals ()

The underlying data structure of LinkedHashSet consists of linked lists and hash tables. The order of elements is guaranteed by the linked list. The hash table ensures that the element is unique. The underlying data structure of TreeSet is a red-black tree. (is a self-balanced binary tree) how to ensure the uniqueness of elements? How to ensure the sorting of elements based on whether the return value of the comparison is 0 or not? There are two ways to sort naturally (elements are comparable) so that the class to which the element belongs implements the Comparable interface comparator sort (collection is comparative) so that the collection receives an Comparator implementation class object Map (double column set) the data structure of the A:Map collection is valid only for keys It has nothing to do with value. B: elements in the form of key-value pairs are stored. The key is unique and the value is repeatable. HashMap HashMap is an asynchronous implementation based on the hash table Map interface introduced by Java 1.2 (HashMap inherits from AbstractMap,AbstractMap to complete the Map interface). This implementation provides all optional mapping operations and allows the use of null values and null keys. This class does not guarantee the order of the mapping, especially it does not guarantee that the order is permanent. The underlying data structure is composed of [array + linked list]. Thread is not safe, efficient hash table depends on two methods: hashCode () and equals () execution order: first determine whether the hashCode () value is the same: continue to execute equals (), see its return value is true: indicates that the element is duplicated Do not add false: add directly to the collection No: add directly to the collection finally: automatically generate hashCode () and equals () LinkedHashMap the underlying data structure consists of a two-way circular linked list and a hash table. The order of elements is guaranteed by the linked list. The hash table ensures that the element is unique. Hashtable Hashtable is based on the stale Dictionary class and completes the Map interface. Thread-safe and inefficient hash tables rely on two methods: hashCode () and equals () execution order: first, determine whether the hashCode () value is the same: continue to execute equals (), and see that the return value is true: indicates that the elements are duplicated Do not add false: directly add to the collection No: directly add to the collection finally: automatically generate hashCode () and equals () can TreeMap the underlying data structure is a red-black tree. (is a self-balanced binary tree) how to ensure the uniqueness of elements? How to ensure the sorting of elements based on whether the return value of the comparison is 0 or not? There are two ways to sort naturally (elements are comparative) so that the class to which the element belongs implements the Comparable interface comparator sort (the collection is comparative) so that the collection receives an Comparator implementation class object

two。 On the principle of set selection

Whether it is in the form of a key object: yes: whether the Map key needs to be sorted: yes: TreeMap No: HashMap does not know, use HashMap. No: whether the Collection element is unique: yes: whether the Set element needs sorting: yes: TreeSet No: HashSet does not know Just use HashSet No: is List safe: yes: Vector No: ArrayList or LinkedList add and delete many: LinkedList query many: ArrayList does not know, use ArrayList do not know, use ArrayList

3: common methods and traversal methods of collections Collection: add () remove () contains () iterator () size ()

Traversal: enhanced for iterator |-- List get () traversal: normal for |-- SetMap: put () remove () containskey () ContainsValue () keySet () get () value () entrySet () size () traversal: look for keys and values for objects according to key values. Thank you for your reading. The above is the content of "the usage of Java data structure List and Map". After the study of this article, I believe you have a deeper understanding of the usage of Java data structure List and Map, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report