Analysis of two special Java container classes List and Set
Container classes can greatly improve programming efficiency and programming capabilities. In java2, all containers have been redesigned by Joshua Bloch of SUN Company, enriching the functions of the container class library.
The purpose of the Java2 container class library is to "save objects", which is divided into two categories:
Collection----a set of independent elements, usually these elements obey certain rules. List must maintain a specific order of elements, while Set cannot have duplicate elements.
Map----a set of "key-value pair" objects, that is, its elements are paired objects. The most typical application is the data dictionary, and there are other extensive applications. In addition, Map can return a Set composed of all its keys and a Collection composed of all its values, or a Set composed of its key-value pairs, and it can also extend the multi-dimensional Map like an array, as long as each "value" of the key-value pair in the Map "Just a Map."
1. Iterator
An iterator is a design pattern that is an object that iterates through and selects objects in a sequence without the developer needing to know the underlying structure of the sequence. Iterators are often called "lightweight" objects because they are cheap to create.
The Iterator function in Java is relatively simple and can only move in one direction:
(1) Use the method iterator() to ask the container to return an Iterator. The first time the Iterator's next() method is called, it returns the first element of the sequence.
(2) Use next() to get the next element in the sequence.
(3) Use hasNext() to check if there are still elements in the sequence.
(4) Use remove() to delete the element newly returned by the iterator.
Iterator is the simplest implementation of Java iterator. ListIterator designed for List has more functions. It can traverse List in two directions and can also insert and delete elements from List.
2. Functional methods of List
List(interface): Order is the most important feature of List; it ensures that the specific order of elements is maintained. List adds many methods to Collection, making it possible to insert and remove elements from the List (only recommended for LinkedList). A List can generate a ListIterator, which can be used to traverse the List in two directions and to insert and delete elements from the middle of the List.
ArrayList: List implemented by array. It allows fast random access to elements, but inserts and removes elements from the middle of the List very slowly. ListIterator should only be used to traverse ArrayList from back to front, rather than inserting and deleting elements, because this is much more expensive than LinkedList.
LinkedList: Sequential access is optimized. Inserting and deleting into the middle of the List is not expensive, but random access is relatively slow (ArrayList can be used instead). It has methods addFirst(), addLast(), getFirst(), getLast(), removeFirst(), removeLast(). These methods (not defined in any interface or base class) allow LinkedList to be used as a stack, queue and Use bidirectional queue.
3. Functional methods of Set
Set(interface): Each element stored in Set must be unique, because Set does not save duplicate elements. The Object added to the Set must define the equals() method to ensure the uniqueness of the object. Set and Collection have exactly the same interface. The Set interface does not guarantee that the order of elements is maintained.
HashSet: Set designed for quick search. Objects stored in HashSet must define hashCode().
TreeSet: A Set that maintains order, with a tree structure at the bottom. Use it to extract an ordered sequence from a Set.
LinkedHashSet: It has the query speed of HashSet, and internally uses a linked list to maintain the order of elements (the order of insertion). So when using an iterator to traverse the Set, the results will be displayed in the order in which the elements were inserted.
HashSet uses hash function to sort elements, which is specially designed for fast query; TreeSet uses red-black tree data structure to sort elements; LinkedHashSet uses hash internally to speed up query, and uses linked lists to maintain elements. Order so that it appears that the elements are stored in insertion order. It should be noted that when generating your own class, Set needs to maintain the storage order of elements, so it must implement the Comparable interface and define the compareTo() method.
The above is the analysis of two special Java container classes, List and Set. For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Interpretation of MyBatis dynamic SQL tags: Detailed explanation of Set tag usage MyBatis is an excellent persistence layer framework. It provides a wealth of dynamic SQL tags and can flexibly construct database operation statements. Among them, the Set tag is used to generate the SET clause in the UPDATE statement, which is very commonly used in update operations. This article will explain in detail the usage of the Set tag in MyBatis and demonstrate its functionality through specific code examples. What is Set tag Set tag is used in MyBati

List operation //Insert a value from the head of the list. $ret=$redis->lPush('city','guangzhou');//Insert a value from the end of the list. $ret=$redis->rPush('city','guangzhou');//Get the elements in the specified range of the list. 0 represents the first element of the list, -1 represents the last element, and -2 represents the penultimate element. $ret=$redis->l

1: JSONArray to ListJSONArray string to List//Initialize JSONArrayJSONArrayarray=newJSONArray();array.add(0,"a");array.add(1,"b");array.add(2,"c") ;Listlist=JSONObject.parseArray(array.toJSONString(),String.class);System.out.println(list.to

Methods to delete elements: 1. Use delete() to delete the specified element from the Set object, the syntax is "setObj.delete(value);"; 2. Use clear() to delete all elements in the Set object, the syntax is "setObj.delete(value);" "setObj.clear();".

How to sort a list using the List.Sort function in C# In the C# programming language, we often need to sort the list. The Sort function of the List class is a powerful tool designed for this purpose. This article will introduce how to use the List.Sort function in C# to sort a list, and provide specific code examples to help readers better understand and apply this function. The List.Sort function is a member function of the List class, used to sort elements in the list. This function receives

Method to convert list to numpy: 1. Use the numpy.array() function. The first parameter of the function is a list object, which can be a one-dimensional or multi-dimensional list; 2. Use the numpy.asarray() function, which will try its best to Use the data type of the input list; 3. Use the numpy.reshape() function to convert the one-dimensional list into a multi-dimensional NumPy array; 4. Use the numpy.fromiter() function, the first parameter of the function is an iterable object.

Example In this example, we first look at the usage of list.sort() before continuing. Here, we have created a list and sorted it in ascending order using sort() method - #CreatingaListmyList=["Jacob","Harry","Mark","Anthony"]#DisplayingtheListprint("List=",myList)#SorttheListsinAscendingOrdermyList .sort(

1. The most common way (not necessarily the best) is through Arrays.asList(strArray). After converting the array into List, you cannot add or delete the List, you can only check and modify it, otherwise an exception will be thrown. Key code: Listlist=Arrays.asList(strArray);privatevoidtestArrayCastToListError(){String[]strArray=newString[2];Listlist=Arrays.asList(strArray);//Insert a piece of data into the converted list list.add(" 1"
