java--(ii) Generics and containers

Source: Internet
Author: User

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

First, the basic concept

1) Collection: A sequence of independent elements that obey one or more rules. List must be in the order in which they are inserted

Save the element, and set cannot have duplicate elements. Queue rules are used to determine the order in which objects are produced (usually

In the same order).

2) Map: A pair of "key-value pairs" objects that allow you to use keys to look up values. ArrayList allows you to use numbers to find values

, so in a sense he speaks of numbers associated with objects. The mapping table allows us to use another object to find a

Object, which is also referred to as an "associative array" because it associates some objects with other objects, or is called a "word

Code, because you can use the key object to find the value object, just as you would use a word in a dictionary to define it. Map is a powerful programming

Tools.

Collection vs Collections

  First, Collection and collections are two different concepts. The reason why they are put together is to make a better comparison.

Collection is the root interface in the container hierarchy. And collections is a class that provides some static methods for handling container classes.

The JDK does not provide a concrete implementation of the collection interface, but rather provides a more specific sub-interface (such as set and list) implementations.

What is the function of the collection interface? Existence is the truth.

The reason for this is that the implementation class for all containers (such as ArrayList implements the list interface, HashSet implements the set interface) provides

Two ' standard ' constructors to implement: 1, an argument-free construction Method (void) 2, a single with collection type

The constructor method is used to create a new collection with its parameters of the same element and its implementation class. In fact: because the

There is a generic container class that complies with the collection interface, and the second method of construction is to allow the container to replicate with each other.

The Conllection interface outlines the concept of a sequence-a way to hold a set of objects. The following populates an integer object with an

Collection (represented here in ArrayList) example, and then prints all the elements in the resulting container:

1Import java.util.*;2 3 4 5  Public classsimplecollection {6 7      Public Static voidMain (string[] args) {8 9collection<integer> C =NewArraylist<integer>();Ten  One        for(inti =0; I <Ten; i++) { A  - C.add (i);  -  the } -  -        for(Integer i:c) { -  +System. out. print (i +", ");  -  + } A  at } -  -}

Because this example uses only the collection method, any objects that inherit from the collection class are OK

Work, but ArrayList is the most basic sequence type.

The name of the Add () method indicates that it is to place a new element into the collection. However, the documentation is very carefully

To make sure that the collection contains the specified element. "This is due to the fact that the set

is added only if the element does not exist in the When using ArrayList, or any kind of list, the Add () is always a table

"Put it in" because list does not care if there is a repetition.

Second, the printing of the container

Examples of Treeset,hashset,linkedlist,arraylist,treemap,hashmap are as follows:

1 ImportJava.util.*;2 3  Public classCollectionall4 {5 6      Public Static voidMain (string[] args)7     {8 printlists ();9 Ten printsets (); One  A printmaps (); -     } -  the     Private Static voidprintlists () -     { -List<string> A1 =NewArraylist<string>(); -A1.add ("List"); +A1.add ("Set"); -A1.add ("Queue"); +A1.add ("Map"); AA1.add ("List"); atSystem.out.println ("ArrayList Elements:"); -System.out.println ("" +A1); -  -list<string> L1 =NewLinkedlist<string>(); -L1.add ("List"); -L1.add ("Set"); inL1.add ("Queue"); -L1.add ("Map"); toL1.add ("List"); +System.out.println ("LinkedList Elements:"); -System.out.println ("" +L1); the     } *     Private Static voidprintsets () $     {Panax Notoginsengset<string> H1 =NewHashset<string>(); -H1.add ("List"); theH1.add ("Set"); +H1.add ("Queue"); AH1.add ("Map"); theH1.add ("List"); +System.out.println ("HashSet Elements:"); -System.out.println ("" +H1); $  $set<string> T1 =NewTreeset<string>(); -T1.add ("List"); -T1.add ("Set"); theT1.add ("Queue"); -T1.add ("Map");WuyiT1.add ("List"); theSystem.out.println ("TreeSet Elements:"); -System.out.println ("" +t1); Wu     } -  About     Private Static voidprintmaps () $     { -map<string, string> h1 =NewHashmap<string, string>(); -H1.put ("List", "ArrayList"); -H1.put ("Set", "HashSet"); AH1.put ("Queue", "Priorityqueue"); +H1.put ("Map", "HashMap"); theH1.put ("List", "ArrayList"); -System.out.println ("HashMap Elements:"); $System.out.println ("" +H1); the  themap<string, string> t1 =NewTreemap<string,string>(); theT1.put ("List", "ArrayList"); theT1.put ("Set", "HashSet"); -T1.put ("Queue", "Priorityqueue"); inT1.put ("Map", "HashMap"); theT1.put ("List", "ArrayList"); theSystem.out.println ("TreeMap Elements:"); AboutSystem.out.println ("" +t1); the  the     } the}

Operation Result:

ArrayList Elements:    [List, Set, Queue, Map, List] LinkedList Elements:    [List, set, Queue, map, List]hashset Elements:    [Map, Queue, set, List]treeset Elements:    [List, Map, Queue, Set]hashmap Elements:    {Map=hashmap, Queue=priorityqueue, Set=hashset, list=arraylist}treemap Elements:    {List=arraylist , Map=hashmap, Queue=priorityqueue, Set=hashset}

The two main types in the Java Container Class library are shown here, the difference being that each "slot" in the container holds an element

Number. Collection only one element can be saved in each slot. This type of container contains:

List: It saves a set of elements in a particular order;

Set: element cannot be duplicated;

Queue: The value allows the object to be inserted on one "side" of the container and removed from the other "side". Map saves two in each slot.

Object, which is the key and the value associated with it.

From the print results, ArrayList and LinkedList are list types, and they all hold elements in the order in which they are inserted

。 The difference between the two is not only performance when performing certain types of operations, but also the LinkedList contains more operations than

ArrayList.

HashSet, TreeSet, and Linkedhashset are set types, and each of the same items is saved only once, but the output

It also shows that different set implementations store elements in different ways. HashSet uses a fairly complex way to store elements

, so the order of storage doesn't seem to make any sense. If the order of storage is important, you can use TreeSet, which

Save the object in ascending order of comparison, or use Linkedhashset to save the object in the sequence that is added.

This example uses three basic styles of Map:hashmap, TreeMap, and Linkedhashset. and HashSet A

Like, HashMap also provides the fastest search technique, and does not follow any apparent order to Cao's elements. TreeMap

The keys are saved in ascending order of the comparison, while Linkedhashmap saves the keys in the insertion sequence while preserving the HashMap

The query speed.

java--(ii) Generics and 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.