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

JAVA programming ideas----11.4 Container printing

高洛峰
高洛峰Original
2016-10-17 09:21:201621browse

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
Previous article:spring annotationNext article:spring annotation