国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
1. What is a collection framework?
2. Collection interface
1. Specify the object type in the corresponding collection through generics
2.Collection common methods use
3. Map interface
Map common methods use
4、具體的實現(xiàn)類
Home Java javaTutorial What is the data structure of Java collection framework

What is the data structure of Java collection framework

May 28, 2023 pm 03:58 PM
java

    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 directly To use it, you only need to create the relevant objects to use, without the need to implement its internal structure.

    A collection is a data structure that stores and manages multiple elements, where these elements are placed in a single unit and can be processed through operations such as addition, deletion, modification, and query. For example, a set of playing cards (a collection of cards), an address book (a set of mapping relationships between names and phone numbers), etc. can be used as examples.

    The picture below is very important! ! ! You need to remember its commonly used interfaces and classes! !

    What is the data structure of Java collection framework

    What is the data structure of Java collection framework

    Because the map interface does not implement the Iterable interface, how to traverse the elements in it?

    		Map<Integer,String> map = new HashMap();
            map.put(1,"jack");
            map.put(2,"tom");
            Set<Map.Entry<Integer, String>> entries = map.entrySet();
            // 使用迭代器進行遍歷 ,增強 for同理
            Iterator<Map.Entry<Integer, String>> iterator = entries.iterator();
            while (iterator.hasNext()) {
                Map.Entry<Integer, String> 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 the map into a Map.Entry object. Because the Set interface is used to receive it, you can use iterators or for-each() to traverse, and each entry object has getKey() and getValue() methods to obtain the key value and value value respectively.

    Basic relationship (simplified version)

    What is the data structure of Java collection framework

    2. Collection interface

    is generally accepted by an interface or class that implements the Collection interface Specifically implement the object of the class, because as can be seen from the above figure, the Collection interface is the parent interface of a series of interfaces and classes. It has relatively few internally implemented methods, so it cannot call some common methods of subclasses.

    1. Specify the object type in the corresponding collection through generics

    Note: The type passed in here can only be a reference type. If it is a basic data type, its wrapper class should be used. Specify

    		Collection<String> collection1 = new ArrayList();
            collection1.add("haha");
            collection1.add("world");
            Collection<Integer> collection2 = new ArrayList();
            collection2.add(1);
            collection2.add(2);
            //collection2.add("hh");// 這里會報錯,不符合傳入的指定類型Integer

    2.Collection common methods use

    methodfunction
    void clear()Delete all elements in the collection
    boolean isEmpty()Determine whether the collection does not have any elements, Commonly known as the empty set
    boolean remove(Object e)If element e appears in the set, delete one of them
    boolean add(E e)Put element e into the collection
    int size()Return the number of elements in the collection
    Object[] toArray()Returns an array containing all elements in the collection

    Note: In the last Object[] toArray() method, an array of type Object[] is returned. The underlying method is: take out the elements in the collection one by one, convert them into Object objects, and store them in the array to be returned. , and finally returns an array of type Object[]. A type conversion exception is thrown when converting to a String[] array.

    What is the data structure of Java collection framework

    Because there is no guarantee that every element in the array is converted to String, but it is only forced to be converted into an array of type String[], so if you have to To convert, you need to first traverse the returned results, convert them to String type one by one, and finally assign them to an array of String[] type. It is not recommended to convert the array type 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];// 一個一個轉(zhuǎn),但是沒啥必要
            }

    3. Map interface

    stores data in the form of < k, v > key-value pairs. The key value here is unique, and each key value can correspond to its The corresponding value value. Different key values ??can correspond to the same value. HashMap: When storing elements, the internal hashCode function is called based on its key value to find the location where the element should be placed. Therefore, the elements in the hash table are not stored in the order in which they are stored.

    Map common methods use

    ##Set> entrySet()Return all key-value pairsboolean isEmpty()Determine whether it is emptyint size()Return the number of key-value pairs
    		HashMap<Integer, String> map = new HashMap<>();
    
            // put()
            map.put(1,"張飛");// 這里的 key 值唯一
            map.put(1,"宋江");// 如果二次插入的 key 值之前有,則替換其 value值
            map.put(2,"Jack");
            System.out.println(map);
    
            // get()
            String s1 = map.get(1);// 返回 宋江
            String s3 = map.getOrDefault(3,"三團");// 未找到,返回 三團
    
            // entrySet()
            // 該方法返回一個 Set<Map.Entry<Integer, String>> 對象
            Set<Map.Entry<Integer, String>> entries = map.entrySet();
            for (Map.Entry<Integer, String> entry : entries) {
                // 通過 entry.getKey() 和 entry.getValue() 獲取每個entry對應的 k, v值
                System.out.println(entry.getKey() + " " + entry.getValue());
            }

    What is the data structure of Java collection framework

    4、具體的實現(xiàn)類

    What is the data structure of Java collection framework

    The above is the detailed content of What is the data structure of Java collection framework. For more information, please follow other related articles on the PHP Chinese website!

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Tips for Writing PHP Comments Tips for Writing PHP Comments Jul 18, 2025 am 04:51 AM

    The key to writing PHP comments is to clarify the purpose and specifications. Comments should explain "why" rather than "what was done", avoiding redundancy or too simplicity. 1. Use a unified format, such as docblock (/*/) for class and method descriptions to improve readability and tool compatibility; 2. Emphasize the reasons behind the logic, such as why JS jumps need to be output manually; 3. Add an overview description before complex code, describe the process in steps, and help understand the overall idea; 4. Use TODO and FIXME rationally to mark to-do items and problems to facilitate subsequent tracking and collaboration. Good annotations can reduce communication costs and improve code maintenance efficiency.

    Writing Effective PHP Comments Writing Effective PHP Comments Jul 18, 2025 am 04:44 AM

    Comments cannot be careless because they want to explain the reasons for the existence of the code rather than the functions, such as compatibility with old interfaces or third-party restrictions, otherwise people who read the code can only rely on guessing. The areas that must be commented include complex conditional judgments, special error handling logic, and temporary bypass restrictions. A more practical way to write comments is to select single-line comments or block comments based on the scene. Use document block comments to explain parameters and return values at the beginning of functions, classes, and files, and keep comments updated. For complex logic, you can add a line to the previous one to summarize the overall intention. At the same time, do not use comments to seal code, but use version control tools.

    Improving Readability with Comments Improving Readability with Comments Jul 18, 2025 am 04:46 AM

    The key to writing good comments is to explain "why" rather than just "what was done" to improve the readability of the code. 1. Comments should explain logical reasons, such as considerations behind value selection or processing; 2. Use paragraph annotations for complex logic to summarize the overall idea of functions or algorithms; 3. Regularly maintain comments to ensure consistency with the code, avoid misleading, and delete outdated content if necessary; 4. Synchronously check comments when reviewing the code, and record public logic through documents to reduce the burden of code comments.

    Effective PHP Commenting Effective PHP Commenting Jul 18, 2025 am 04:33 AM

    The key to writing PHP comments is clear, useful and concise. 1. Comments should explain the intention behind the code rather than just describing the code itself, such as explaining the logical purpose of complex conditional judgments; 2. Add comments to key scenarios such as magic values, old code compatibility, API interfaces, etc. to improve readability; 3. Avoid duplicate code content, keep it concise and specific, and use standard formats such as PHPDoc; 4. Comments should be updated synchronously with the code to ensure accuracy. Good comments should be thought from the perspective of others, reduce the cost of understanding, and become a code understanding navigation device.

    Understanding PHP Variables Understanding PHP Variables Jul 17, 2025 am 04:11 AM

    PHP variables start with $, and the naming must follow rules, such as they cannot start with numbers and are case sensitive; the scope of the variable is divided into local, global and hyperglobal; global variables can be accessed using global, but it is recommended to pass them with parameters; mutable variables and reference assignments should be used with caution. Variables are the basis for storing data, and correctly mastering their rules and mechanisms is crucial to development.

    PHP Development Environment Setup PHP Development Environment Setup Jul 18, 2025 am 04:55 AM

    The first step is to select the integrated environment package XAMPP or MAMP to build a local server; the second step is to select the appropriate PHP version according to the project needs and configure multiple version switching; the third step is to select VSCode or PhpStorm as the editor and debug with Xdebug; in addition, you need to install Composer, PHP_CodeSniffer, PHPUnit and other tools to assist in development.

    PHP Commenting Syntax PHP Commenting Syntax Jul 18, 2025 am 04:56 AM

    There are three common ways to use PHP comments: single-line comments are suitable for briefly explaining code logic, such as // or # for the explanation of the current line; multi-line comments /*...*/ are suitable for detailed description of the functions or classes; document comments DocBlock start with /** to provide prompt information for the IDE. When using it, you should avoid nonsense, keep updating synchronously, and do not use comments to block codes for a long time.

    Exploring Basic PHP Syntax Exploring Basic PHP Syntax Jul 17, 2025 am 04:11 AM

    The basic PHP syntax includes: 1. Use wrapping code; 2. Use echo or print to output content, where echo supports multiple parameters; 3. Variables do not need to declare types, start with $. Common types include strings, integers, floating-point numbers, booleans, arrays and objects. Mastering these key points can help you get started with PHP development quickly.

    See all articles
    MethodFunction
    V get(Object k)Find the corresponding v according to the specified k
    V getOrDefault(Object k, V defaultValue)According to the specified k Search for the corresponding v, if not found, return the default value
    V put(K key, V value)Put the specified k-v into the Map
    boolean containsKey(Object key)Judge whether it contains key
    boolean containsValue(Object value)Judge whether it contains value