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 data structure of the Java collection framework

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

Share

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

This article mainly introduces the relevant knowledge of "what is the data structure of the Java collection framework". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope this article "what is the data structure of the Java collection framework" can help you solve the problem.

1. What is a collection framework?

In java, there is a set of ready-made data structures, such as sequence lists, linked lists, queues, stacks, priority queues, hash tables, etc., which are encapsulated into corresponding interfaces / classes for programmers to use directly. You only need to create related objects to use, without the need to implement its internal structure.

Collection, is to put multiple elements into a unit, used to add, delete, modify, query, store and manage these elements. For example, a deck of playing cards (a collection of sets of cards), an address book (the mapping of a set of names and phones), and so on.

The following picture is very important! You need to keep in mind its commonly used interfaces and classes!

Because the map interface does not implement the Iterable interface, how do you implement it to traverse its elements?

Map map = new HashMap (); map.put (1, "jack"); map.put (2, "tom"); Set entries = map.entrySet (); / / traversal using iterators to enhance for synopsis Iterator iterator = entries.iterator (); while (iterator.hasNext ()) {Map.Entry entry = iterator.next () System.out.println (entry.getKey () + "+ entry.getValue ());}

Taking HashMap as an example, you can call its entrySet () method to encapsulate each key-value pair in map into a Map.Entry object. Because it is received by the Set interface, you can use an iterator or for-each () to traverse, and each entry object has getKey () and getValue () methods to obtain key and value values, respectively.

Basic relationship (simplified version)

2. Collection interface

Generally, the interface or class that implements the Collection interface is used to accept the objects of the specific implementation class, because as you can see in the above figure, the Collection interface is the parent interface of a series of interfaces and classes, and its internal implementation methods are relatively few, so you cannot call some common methods of some subclasses.

1. Use generics to specify object types in the corresponding collection

Note: the type passed in here can only be a reference type. If it is a basic data type, it should be specified with its wrapper class.

Collection collection1 = new ArrayList (); collection1.add (""); collection1.add ("world"); Collection collection2 = new ArrayList (); collection2.add (1); collection2.add (2); / / collection2.add ("hh") / / an error is reported here, which does not conform to the specified type passed in Integer2.Collection. Common methods use void clear () to delete all elements in the collection boolean isEmpty () to determine whether the collection does not have any elements. It is commonly known as empty collection boolean remove (Object e) if element e appears in the collection. Delete one of the boolean add (E e) put the element e in the collection int size () returns the number of elements in the collection Object [] toArray () returns an array containing all the elements in the collection

Note: in the last Object [] toArray () method, an array of type Object [] is returned. The underlying approach is to take out the elements in the collection one by one, convert them to Object objects, store them in the array to be returned, and finally return an array of type Object []. If you convert it directly to an array of type String [], a type conversion exception is thrown.

Because there is no guarantee that every element in the array will be converted to String, but only an array of type String [] as a whole, if you have to, you need to traverse the returned results first, convert them to type String one by one, and finally assign them to an array of type String []. It is not recommended to convert array types as a whole in java.

Object [] objects = collection1.toArray (); String [] strings = new String [objects.length]; for (int I = 0; I

< objects.length; i++) { strings[i] = (String)objects[i];// 一个一个转,但是没啥必要 }3、Map 接口 通过< k, v >

Data is stored in the form of key-value pairs, where the key value is unique, and each key value can correspond to its corresponding value value. Different key values can correspond to the same value. HashMap: when storing an element, according to its key value, call the internal hashCode function to find out where the element should be placed, so the elements in the hash table are not stored in the order in which they are stored.

Common methods of Map use V get (Object k) to find the corresponding vV getOrDefault (Object k, V defaultValue) according to the specified k If it is not found, the default value V put (K key, V value) will be returned to Mapboolean containsKey (Object key) to determine whether it contains keyboolean containsValue (Object value) to determine whether it contains valueSet entrySet () to return all key-value pairs to boolean isEmpty () to determine whether it is empty int size () to return the number of key-value pairs HashMap map = new HashMap (). / / put () map.put (1, "Zhang Fei"); / / the key value here is unique map.put (1, "Song Jiang"); / / if there is a second inserted key value, replace its value value map.put (2, "Jack"); System.out.println (map); / / get () String S1 = map.get (1) / / return Song Jiang String S3 = map.getOrDefault (3, "Santuan"); / / if not found, return Santuan / / entrySet () / / this method returns a Set object Set entries = map.entrySet () For (Map.Entry entry: entries) {/ / obtain the k, v value System.out.println (entry.getKey () + "" + entry.getValue ()) of each entry through entry.getKey () and entry.getValue ();}

4. Concrete implementation class

This is the end of the introduction on "what is the data structure of the Java collection framework". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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