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

JAVA programming ideas----11.4 Container printing

Oct 17, 2016 am 09:21 AM

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>()));
    }
}
Copy after login
[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}
Copy after login

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 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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)