JAVA programming ideology (version 4) Study Notes ---- 11.4 container printing and programming ideology ---- 11.4

Source: Internet
Author: User

JAVA programming ideology (version 4) Study Notes ---- 11.4 container printing and programming ideology ---- 11.4

 1 import static java.lang.System.out; 2  3 import java.util.ArrayList; 4 import java.util.Collection; 5 import java.util.HashMap; 6 import java.util.HashSet; 7 import java.util.LinkedHashMap; 8 import java.util.LinkedHashSet; 9 import java.util.LinkedList;10 import java.util.Map;11 import java.util.TreeMap;12 import java.util.TreeSet;13 14 public class ContainerFramework {15 16     static Collection fill(Collection<String> collection) {17         collection.add("rat");18         collection.add("cat");19         collection.add("dog");20         collection.add("dog");21         return collection;22     }23 24     static Map fill(Map<String, String> map) {25         map.put("rat", "Fuzzy");26         map.put("cat", "Rags");27         map.put("dog", "Bosco");28         map.put("dog", "Spot");29         return map;30     }31     public static void main(String[] args) {32         out.println(fill(new ArrayList<String>()));33         out.println(fill(new LinkedList<String>()));34         out.println(fill(new HashSet<String>()));35         out.println(fill(new TreeSet<String>()));36         out.println(fill(new LinkedHashSet<String>()));37         out.println(fill(new HashMap<String, String>()));38         out.println(fill(new TreeMap<String, String>()));39         out.println(fill(new LinkedHashMap<String, String>()));40     }41 }

The running 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, you can see that the content printed by the Collection is enclosed by square brackets [], and each element is separated by commas. The content printed by Map is enclosed by braces, the key and value are connected by equal signs as an element (Key = value). Each element is separated by commas.

Java container classes include Collection classes with Collection interfaces as the root and associated array classes with Map as the root.

  • The Collection interface has three important subtypes:List, Set, and Queue)
    • All the implementation classes of the List interface ensure that the elements can be saved in the insertion order, so the List is an ordered collection. Here, ArrayList has the advantage of efficient random access to its elements. Its disadvantage is that it is slow to insert or remove elements at a specified position. The slow list is slow in random access, but it is more efficient to insert and remove elements at the specified position.

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

    • All the implementation classes of the Set interface ensure that their elements are not repeated. HashSet uses a hash algorithm to store elements in a data set. Its elements are unordered, but the efficiency of element acquisition is the fastest. TreeSet is an ordered set that stores the elements in the Set in ascending order of the comparison results. LinkedHashSet is also an ordered set. It stores objects in the order of element insertion and has the query speed of HashSet.

3. Queue

    • Queue allows data insertion at one end of the container and Data removal at the other end.
  • The Map interface has three important sub-types: HashMap, TreeMap, and LinkedHashMap. You can use keys to find values. It is a container for "key-value" pairs.

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.