In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what details need to be paid attention to when using collection classes in java". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "what details need to be paid attention to when using collection classes in java"!
1. Iterator:
The iterator's next method automatically fetches elements downward, avoiding NoSuchElementException.
That is to say, hasNext is called once in an iterative loop, such as a statement.
sop(it.next()+"... "+ it. next()) will occur. The return type of the iterator's next method is Object, so remember that type conversion is not necessary after applying generics.
2. List collection:
Because the elements in the List collection are marked with corners, the elements in the List collection are ordered. In addition, the List collection can contain duplicate elements or null.
The List collection has an iterator Iterator and a unique iterator list ListIterator.
List collection to determine whether the elements are the same are using the equals method, regardless of contains, remove all rely on the equals method, such as ArrayList to store students, the same name and age as the same person, at this time you need to duplicate the student class Object class in the equals method (very important!!! Watch out!!)
3. Set:
The elements stored in the Set interface are out of order. There can be no duplicate elements and null.
There is only one way to extract a Set, which is the iterator Iterator.
Set collection functions are identical to Collection, with no special methods.
3.1、HashSet:
The elements stored in a collection are unordered and unique.
The underlying data structure is a hash table. The data in the hash table structure is disordered, and the operation efficiency of the hash table structure is efficient.
Threads are out of sync.
The principle for ensuring element uniqueness is to duplicate hashCode and equals methods.
If the hashCode values of the two elements are the same, then continue to determine whether the two elements equals is true.
If the hashCode values of the two elements are different, the equals method is not called.
When we store custom elements into the HashSet collection (such as student objects), we usually overwrite the hashCode and equals methods, and the hashCode and equals methods are not called by us, but by ourselves inside the HashSet collection.
3.2、TreeSet
TreeSet collection can sort the stored elements, making up for the shortcomings of Set collection elements disorder, and the elements are unique.
The underlying data structure is binary tree, binary tree structure is ordered.
Threads are out of sync.
TreeSet collections require that the elements stored in the collection themselves be comparative, otherwise an error will be reported.
TreeSet sets guarantee element uniqueness by using compareTo or compare methods. The first way to sort TreeSets is to make the elements themselves comparative,
Define element classes that implement the Compareble interface, overriding the compare method, in the natural order of elements.
The second way to sort TreeSets: make the set comparative When the element itself does not have comparative or the comparative we need, we need to make the set have custom comparative.
4. Map collection:
4.1、Hashtable
The bottom layer is hash table structure thread-safe, and keys and values cannot be null.
4.2、HashMap
The bottom layer is hash table structure thread-unsafe, keys and values can be null.
4.3、LinkedHashMap
The bottom layer is linked lists and hash tables. Threads are unsafe.
4.4、TreeMap
The bottom layer is a binary tree thread unsafe
At this point, I believe that everyone has a deeper understanding of "what details need to be paid attention to when using collection classes in java," so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.