In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "what is the use of Java collection class", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what is the use of Java collection class" this article.
The collection class is the implementation of the Java data structure. Java's collection class is an important part of the java.util package, allowing elements to be grouped in a variety of ways and defining ways to make them easier to manipulate. Java collection class is that Java encapsulates and enhances some basic and frequently used basic classes and then provides them in the form of a class. Collection class is a class in which multiple objects can be saved, and objects are stored. Different collection classes have different functions and characteristics, which are suitable for different occasions to solve some practical problems.
Collection class brief:
Collection classes are used to store certain types of objects. A common feature of collection classes is that they hold only objects (actually object names, that is, pointers to addresses). Unlike arrays, arrays can hold objects and simple data. If you want to use both the simple data type and the flexibility of the collection class in the collection class, you can turn the simple data type data into an object of that data type class and then put it into the collection, but the execution efficiency will be reduced.
The objects contained in the collection class are all instances of the Object class. Once an object is placed in the collection class, its class information will be lost, that is to say, all the objects contained in the collection class are pointers to Object class objects. This is designed to make collection classes universal, because the Object class is the ancestor of all classes, so you can store any class in these collections without restrictions. Of course, this also brings inconvenience, which makes it necessary to reshape the collection member before using it.
The collection class is the implementation of the Java data structure. When writing a program, it is often necessary to deal with all kinds of data, and it is very important for the running efficiency of the program to choose the data structure in order to deal with these data.
one。 Set
1. According to the data with different characteristics, you can select the main classes of the corresponding interface for instantiation and method calls.
2. Be familiar with the differences and characteristics of different implementation classes of interfaces
3. The underlying implementation of the relevant interface implementation class: storage structure
two。 Set frame structure
Collection: store data one by one
List: storing ordered, repeatable data: replacement array, "dynamic array"
ArrayList/LinkedList/Vector
Set: storing unordered, non-repeatable data: a collection of high school
HashSet/LinkedHashSet/TreeSet
Map: store a pair of data (key-value): a high school function. Y = f (x) (x1 ~ (1)), (x ~ (2))
HashMap/LinkedHashMap/TreeMap/Hashtable/Properties
3. Collection includes two subinterfaces, List and Set.
Among them, List stores orderly and repeatable data-à dynamic array.
List is a subinterface of Collection, and the methods declared in Collection can be used in the implementation class of List.
ArrayList:Collection collection = new ArrayList ()
List list = Arrays.aslist (); / / converts the array to list, which is usually used during initialization. Add () and remove () are not supported, which means they can be used without changing the length.
ArrayList list = new ArrayList ()
List list = new ArrayList (); / / polymorphism
Common methods in Collection
Functions: add, delete, change, look up, insert, length, traverse
1. Add (Object obj): add the element obj to the current collection, which can be boxed automatically
2.size (): gets the number of elements in the collection
3.addAll (Collection coll): adds elements from the coll collection to the current collection.
4.isEmpty (): determines whether the current collection is empty.
5.clear (): clears the current collection
6.contains (Object obj): determines whether the current collection contains obj elements
7.containsAll (Collection coll): determines whether the current collection contains all the elements in the coll collection
8.remove (Object obj): deletes the obj element that first appears in the current collection
9.removeAll (Collection coll): subtraction: removes elements it shares with the coll collection from the current collection
10.retainAll (Collection coll): intersection: gets the elements common to the current collection and the coll collection
11. Equals (Object obj): determines whether the current collection is equal to the obj element
12.hashCode (): returns the hash value of the current collection object
13.toArray (): converts a collection to an array
ArrayList: thread is not safe, efficient, compared with other List is the main implementation class, suitable for a lot of search work, the bottom is the Object [] array, orderly and repeatable.
Integer [] arr2 = new Integer [] {1pm 2pm 3}
Int [] arr3 = new int [] {1pm 2pm 3}
List list2 = Arrays.asList (arr2)
List list3 = Arrays.asList (arr3); / / stored address value
List list4 = Arrays.asList (1, 2, 3)
System.out.println (list2.size ()); / / 3
System.out.println (list3.size ()); / / 1
System.out.println (list4.size ()); / / 3
System.out.println (list3); / / [[I@4459eb14]
LinkedList:LinkedList list = new LinkedList ()
Threads are not safe; the underlying layer uses two-way linked lists to store data, which is efficient for frequent insertions and deletions.
Vector:Vector v = new Vector ()
The ancient implementation class of List is thread-safe and inefficient, and the underlying layer uses Object [] storage
Set: where Set stores disordered, non-repeatable data-à is similar to a collection of junior high school concepts
HashSet:HashSet set = new HashSet ()
Set set = new HashSet ()
The main implementation class, thread is not safe, can store null values, the bottom is also the use of array + linked list + red-black tree storage.
Disorder: not equal to randomness, the underlying storage location of elements is not arranged in turn like an array, but refers to the storage location determined by its hashCode value, which is understood as disorder.
Non-repeatability: judge according to the object's equals (). If true is returned, the add fails. It ensures non-repeatability.
Therefore, the hashCode () and equals () methods must be overridden, and the consistency of the two methods must be maintained, and equal objects must have equal hash codes.
LinkedHashSet:
LinkedHashSet set = new LinkedHashSet ()
Set set = new LinkedHashSet ()
Is a subclass of HashSet, in addition to adding data, but also through a pair of pointers to record the order in which they are added, making it more efficient than HashSet to traverse Set elements.
TreeSet:TreeSet set = new TreeSet ()
You can traverse according to the size of the specified attributes of the added elements, arranged in natural and custom sorting, and the underlying red-black tree.
Special note: the elements added to TreeSet must be objects of the same type, and the same elements cannot be stored in TreeSet. The criterion of judgment is not the hashCode () and equals () methods, but the comparison according to the compareTo () or compare () methods overridden in natural or custom sorting.
Map interface, stores a pair of data, key-value pair (key-value), a key-value in map constitutes an Entry,Map interface similar function.
HashMap:HashMap map = new HashMap (); / / create an Entry array table with length 16 at the underlying level
The key in Map is not repeatable and out of order, so add the element method put (); and add elements according to seven above and eight down (add elements in jdk7 at the beginning, and add elements in jdk8 at the end)
Common methods in Map: Object put (Object key,Object value); void putAll (Map m); Object remove (Object key); void clear (); Object get (Object key); boolean containKey (Object key); boolean containsValue (Object value); int size (); boolean isEmpty; boolean equals (Object obj); Set keyset (); Collection values (); Set entrySet ()
5. Summary:
Added: put (Object key,Object value)
Delete: remove (Object key)
Change to: put (Object key,Object value)
Check: get (Object key)
Length: size ()
Traversal: keySet (); values (); entrySet ()
LinkedHashMap:
TreeMap:TreeMap map = new TreeMap ()
Adding key-value to TreeMap requires that all key must be objects of the same class, and for key, implement natural sorting or custom sorting.
Hashtable:
A subclass of Properties:Hashtable, where key-value is of type String and is often used to deal with properties files.
Source code parsing of HashMap (underlying implementation principle)
Storage structure used by HashMap: jdk8: array + linked list + red-black tree jdk7: array + linked list (list structure up and down)
With the addition of red-black tree, the efficiency of data search and comparison can be improved.
Linked list: "seven up and eight down"
Initialization problem: new HashMap ()
Jdk8: the underlying array is not initialized (lazy)
Jdk7: the underlying array is initialized when instantiated (hungry)
Jdk8: the underlying array Node [] (class HashMap.Node implements Map.entry)
Jdk7: the underlying array Entry [] (class HashMap.Entry implements Map.entry)
Class Node/Entry {
Int hash
K key;V value
Node/Entry next
}
The process of adding data:
In jdk8, when put () is called for the first time, the underlying layer creates an array of length 16, and then adds data
In jdk7, put () is called for the first time to add data expansion mechanism directly.
Critical value threshold. When the added element exceeds the critical value, capacity expansion should be considered. The default capacity expansion is 2 times that of the original.
By default, equal to
DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY. That's 12.
Load factor: 0.75 by default
In addition to the add operation, get (), remove () also refer to put (key,value)
Enhanced for loop (for-each loop)
Format:
For (collection element type variable name: reference to the collection object to be traversed) {
}
Example:
For (Object obj: collection) {
System.out.println (obj)
}
Iterator Iterator
The object of Iterator becomes an iterator, which is mainly used to traverse the elements in the Collection collection
Collection c = new ArrayList ()
C.add (1); / / automatic boxing
C.add (2)
C.add (3)
Iterator iterator = c.iterator ()
While (iterator.hasNext ()) {
Object obj = iterator.next ()
System.out.println (obj)
System.out.println (iterator.next ()); / / Yes, but not recommended
}
The above is all the content of this article "what is the use of Java collection classes?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.