Java class set (List,vector,map ...) (1)

Source: Internet
Author: User

1.1 before introducing the class-set framework, consider the question that if you want to save a set of objects now, you can use only an array of objects as a beginner, but using an object array operation itself has a limitation of the length of the array, and the manipulation of some data structures, such as linked lists, You can complete the operation of a dynamic array of objects, but it would be cumbersome if it were all done by the developer.

The class set framework solves the above problem, the so-called class set is a dynamic object array, is the implementation of some good data structure is packaged, so it is very convenient to use, and most importantly, the class set framework itself is not limited by the length of the object array.

The class set framework is designed to have several features:

1. This framework is high-performance, and the implementation of basic class sets (dynamic arrays, lists, trees, and hashes) is highly efficient, and it is seldom manual to write code for these ' Data engines '.

2. The framework must allow different types of class sets to operate in the same way and in a highly interoperable manner.

3. The class set must be easy to expand and modify. To achieve this goal, the class set framework is designed to contain a set of standard interfaces.

Explanations of the same manner and height:

An array of dynamic objects implemented as a class in the operation of a class set, so that all forms of operation are the same for any object. For example, add content must use the Add () method.

Height generally refers to the unity of the element types in the class set, either the A object in a collection or the B object.

1.2. the primary interface of the class set framework

The most common use in the entire Java class set is: Collection,list,set,map,iterator,listiterator,enumeration,sortedset,sortedmap,quene, Map.entry, the specific features of these interfaces are detailed in the following steps.

Their inheritance relationship is as follows:

Collection Map

List Set quene SortedSet sortedmap

Collection under four, map below for SortedMap

The interfaces defined on SORTEDXX are sort interfaces.

1.2.1 Collection

About collection, in general development, basically not directly used, are using its sub-interface, such as List,set ...

List everyone is familiar with, here will not repeat.

Let me introduce you to a salvage subclass, vector!.

In the list interface there is also a subclass vector, which belongs to a saved subclass, in the history of the entire Java collection, the vector is an elder class, In the JDK1.0 already exist, to Java1.2 after the emphasis on the concept of the set framework, so the definition of many new interfaces, and considering that a large number of users have become accustomed to the vector, so the Java designers let vector implementation of a list interface preserved. Because the list interface is implemented, there is not much difference between the use of vectors and the list.

public class Vector<e>extends Abstraclist<e>

Implements List<e>,randomaccess,cloneable,serializable

From the definition, like ArrayList inherited the Abstraclist class

The code is as follows:

1  PackageCom.zhengyu.java;2 3 Importjava.util.List;4 ImportJava.util.Vector;5 6  Public classVector_demo {7 8      Public Static voidMain (string[] args) {9List<string> alllist=NULL;Tenalllist=NewVector<string>(); OneAlllist.add ("Hello"); AAlllist.add (0, "before Hello"); -Alllist.add ("LaStone"); -          for(inti = 0; I < alllist.size (); i++) { theSystem.out.println (Alllist.get (i) + ""); -         } -  -     } +  -}

The above code and list operation is no different, but, because it is the elder class, there are many methods that list does not, for example, the addelement (E O) method, is the earliest increase operation, after JDK1.2 this method and add is consistent.

The code is as follows:

1  PackageCom.zhengyu.java;2 3 4 ImportJava.util.Vector;5 6  Public classVector_demo {7 8      Public Static voidMain (string[] args) {9 TenVector<string> alllist =NewVector<string>(); OneAlllist.addelement ("Hello"); AAlllist.addelement ("World"); -          for(inti = 0; I < alllist.size (); i++) { - System.out.println (Alllist.get (i)); the         } -  -     } -  +}

So, the difference between subclasses: where is the difference between ArrayList and vector?

Which sub-class to use, the main differences are as follows:

1. At the time of launch, JDK1.2 launched arraylist,jdk1.0 to launch the vector.

2. In performance, ArrayList is asynchronous processing, performance is higher, so,vertor is synchronous processing, performance is low.

3. Thread-Safe, ArrayList is non-thread-safe because it is generally unsafe to have faster asynchronous performance. Then vector is thread-safe.

4. For output, ArrayList can only be used with Iterator,foreach. A vector can be one more enumeration.

1.2.2 LinkedList sub-class and Quene interface.


Let's talk about the linked list and the Quene interface.

LinkedList represents a linked list of operation classes, that is, Java has developed a linked list program, you directly use the line.

Defined as follows:

public class Linkedlist<e> extends abstractsequentialist<e>

Implements List<e>, quene<e>,cloneable,serializable

Can be found, although the implementation of the list interface, but also implemented the Quene interface, Quene represents the queue operation interface, using FIFO (first-out), is not very familiar with the features of stack stack memory. Quene mentioned above, is collection's sub-interface

Public interface Quene<e> extends collection<e>

1  PackageCom.zhengyu.java;2 3 Importjava.util.LinkedList;4 5  Public classLinkedlist_demo {6 7      Public Static voidMain (string[] args) {8linkedlist<string> link =NewLinkedlist<string>();9Link.add ("Demo1");TenLink.add ("Demo2"); OneLink.add ("Demo3"); ASystem.out.println ("Element () method found table header:" +link.element ()); -          //The Element () method finds the table header: A -System.out.println ("The contents of the list after the search:" +link); the      //after finding the contents of the list: A,b,c -System.out.println ("Peek () method found table header:" +Link.peek ()); -        //The Peek () method finds the table header: A -System.out.println ("The contents of the list after the search:" +link); + //after finding the contents of the linked list: -System.out.println ("poll () method found table header:" +link.element ()); + //The poll () method finds the table header: A ASystem.out.println ("The contents of the list after the search:" +link); at //after finding the contents of the list: B,c -     } -  -}

Several methods differ as follows:

Find table header, public E element ()

Table header not deleted: Public E-Peek ()

Find table Header Delete header: Public E poll ()

Then using the poll method, use the For loop to fetch the data, take a delete one, and finally delete the light is all taken out.

For example, the above code changes the final output as;

for (int i=0,i<link.size (), i++) {

System.out.print (Link.poll ());

}

The results are also a,b,c

This is a bit of a pre-talk today, and the next time you add another set of frame interfaces.

Java class set (List,vector,map ...) (1)

Related Article

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.