Java Programming Ideas (fourth Edition) Learning notes----11.4 Printing of containers

Source: Internet
Author: User

1 Import Staticjava.lang.System.out;2 3 Importjava.util.ArrayList;4 Importjava.util.Collection;5 ImportJava.util.HashMap;6 ImportJava.util.HashSet;7 ImportJava.util.LinkedHashMap;8 ImportJava.util.LinkedHashSet;9 Importjava.util.LinkedList;Ten ImportJava.util.Map; One ImportJava.util.TreeMap; A ImportJava.util.TreeSet; -  -  Public classContainerframework { the  -     StaticCollection Fill (collection<string>collection) { -Collection.add ("Rat"); -Collection.add ("Cat"); +Collection.add ("Dog"); -Collection.add ("Dog"); +         returncollection; A     } at  -     StaticMap Fill (map<string, string>map) { -Map.put ("Rat", "Fuzzy"); -Map.put ("Cat", "Rags"); -Map.put ("Dog", "Bosco"); -Map.put ("Dog", "Spot")); in         returnmap; -     } to      Public Static voidMain (string[] args) { +Out.println (Fill (NewArraylist<string>())); -Out.println (Fill (NewLinkedlist<string>())); theOut.println (Fill (NewHashset<string>())); *Out.println (Fill (NewTreeset<string>())); $Out.println (Fill (NewLinkedhashset<string>()));Panax NotoginsengOut.println (Fill (NewHashmap<string, string>())); -Out.println (Fill (NewTreemap<string, string>())); theOut.println (Fill (NewLinkedhashmap<string, string>())); +     } A}

The result of the above code is:

[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, the results can be seen, collection printed content is enclosed in square brackets [], each element is separated by commas; The map prints the contents with curly braces {}, and the keys and values are joined as an element (key = value), each element separated by commas.

Java container classes include two types: Collection Class with collection interface as root, and associative array class with map root

    • The collection interface has three important sub-types: List, set (set), Queue (queues)
    1. The list has two important implementations, ArrayList and LinkedList, respectively.
      • all implementation classes of the list interface guarantee that their elements can be saved in the order in which they are inserted, so the list is an ordered collection. The advantage of ArrayList is that its elements can be accessed efficiently and randomly, and the disadvantage is that the performance of inserting and removing elements at a specified location is slow. LinkedList is slower in random access, but it is more efficient to insert and remove elements at a specified location.

2. Set has three important implementations, respectively, Hashset,treeset,linkedhashset

      • all implementation classes of the set interface ensure that their elements are not duplicated . HashSet uses a hashing algorithm to store elements in a set, its elements are unordered, but the efficiency of acquiring elements is the fastest. TreeSet is an ordered collection that stores the elements in the collection in ascending order of comparison results. Linkedhashset is also an ordered set that saves objects in the order in which they are inserted, while at the same time having a hashset query speed.

3. Queue

      • The queue allows the insertion of data at one end of the container and the removal of data at the other end.
    • The map interface has three important subtypes: Hashmap,treemap,linkedhashmap, which can be used to find values by key, which is a container for "key-value" pairs
    1. The HashMap is unordered and has the fastest search speed.
    2. TreeMap is ordered and saved in ascending order of the results of the comparison key
    3. Linkedhashmap are ordered and stored in the order in which they are interpolated, while retaining the HashMap query speed.

Java Programming Ideas (fourth Edition) Learning notes----11.4 Printing of containers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.