search
Home类库下载java类库JAVA programming ideas----11.4 Container printing

JAVA Programming Thoughts (Fourth Edition) Study Notes----11.4 Printing of Containers

import static java.lang.System.out;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;

public class ContainerFramework {

    static Collection fill(Collection<String> collection) {
        collection.add("rat");
        collection.add("cat");
        collection.add("dog");
        collection.add("dog");
        return collection;
    }

    static Map fill(Map<String, String> map) {
        map.put("rat", "Fuzzy");
        map.put("cat", "Rags");
        map.put("dog", "Bosco");
        map.put("dog", "Spot");
        return map;
    }
    public static void main(String[] args) {
        out.println(fill(new ArrayList<String>()));
        out.println(fill(new LinkedList<String>()));
        out.println(fill(new HashSet<String>()));
        out.println(fill(new TreeSet<String>()));
        out.println(fill(new LinkedHashSet<String>()));
        out.println(fill(new HashMap<String, String>()));
        out.println(fill(new TreeMap<String, String>()));
        out.println(fill(new LinkedHashMap<String, String>()));
    }
}
[rat, cat, dog, dog]
[rat, cat, dog, dog]
[cat, dog, rat]
[cat, dog, rat]
[rat, cat, dog]
{cat=Rags, dog=Spot, rat=Fuzzy}
{cat=Rags, dog=Spot, rat=Fuzzy}
{rat=Fuzzy, cat=Rags, dog=Spot}

After running the code, you can see from the results that the content printed by Collection is enclosed in square brackets [], each element Separated by commas; the content printed by Map is enclosed in curly brackets {}, the key and value are connected with an equal sign as one element (key = value), and each element is separated by a comma.

Java container classes include two types: collection classes with Collection interface as the root, and associative array classes with Map as the root.

Collection interface has three important subtypes: List (list), Set (collection), Queue ( Queue)

List has two important implementations, namely ArrayList and LinkedList


All implementation classes of the List interface ensure that its elements can be saved in the order of insertion, so List is an ordered collection. The advantage of ArrayList is that it can efficiently access its elements randomly, but the disadvantage is that the performance of inserting and removing elements at specified locations is relatively slow. LinkedList is slower in terms of random access, but it is more efficient in inserting and removing elements at specified locations.

2. Set has three important implementations, namely HashSet, TreeSet, and LinkedHashSet


All implementation classes of the Set interface ensure that their elements will not be repeated. HashSet uses the hash algorithm to store the elements in the set. Its elements are unordered, but the efficiency of obtaining elements is the fastest. TreeSet is an ordered set that stores the elements in the set in ascending order of comparison results. LinkedHashSet is also an ordered collection. It saves objects in the order in which elements are inserted, and at the same time has the query speed of HashSet.

  3. Queue


Queue allows data to be inserted at one end of the container and data to be removed at the other end.

The Map interface has three important subtypes: HashMap, TreeMap, and LinkedHashMap. Values ​​can be found by key. It is a container of "key-value" pairs.

HashMap is unordered and has the fastest search speed.

TreeMap is in order, and is saved in ascending order of the results of comparison keys.

LinkedHashMap is in order, and is saved in the order of inserted elements, while also retaining the query speed of HashMap.



Statement
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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment