Container of Java

Source: Internet
Author: User

First, a container API framework diagram, all of the knowledge that we have learned in Java is based on this graph to learn ....

Container API:

1, Collection interface------defines a method for storing a set of objects, and its sub-interface set and list define the way to store them separately.

The data objects in ①, set are not sequential and cannot be duplicated.

The data objects in the ②, list are ordered and can be duplicated.

2. The map interface defines a method for storing "key"---value mapping pairs.

Collection interface:

The method defined in the collection interface (meaning that if you implement the collection interface, you will have all of the following methods):

    

Examples of collection methods:

The point here is that the collection can only contain reference types of data.

ImportJava.util.*; Public classtestcollection{ Public Static voidMain (String args[]) {Collection Collection=NewArrayList (); //can be put into different types of objectsCollection.add ("Hello"); Collection.add (NewPerson ("F1", 18)); Collection.add (NewInteger (100));        System.out.println (Collection.size ());    System.out.println (collection); }}classperson{PrivateString name; Private intAge ;  PublicPerson (String name,intAge ) {         This. Name =name;  This. Age =Age ; }}

Next, we continue to use the example above to talk about the use of the Remove () method in collection:

ImportJava.util.*; Public classtestcollection{ Public Static voidMain (String args[]) {Collection Collection=NewHashSet (); //can be put into different types of objectsCollection.add ("Hello"); Collection.add (NewPerson ("F1", 18)); Collection.add (NewInteger (100)); Collection.remove ("Hello"); Collection.remove (NewInteger (100)); System.out.println (Collection.remove (NewPerson ("F1", 18)));    System.out.println (collection); }}classperson{PrivateString name; Private intAge ;  PublicPerson (String name,intAge ) {             This. Name =name;  This. Age =Age ; }     PublicString GetName () {returnname; }     Public intGetage () {returnAge ; }    /*Public Boolean equals (Object obj) {if (obj instanceof person) {person person = (person) obj;        Return (Name.equals (person.name) && age = = person.age);    } return Super.equals (obj);    } public int hashcode () {return name.hashcode (); }*/}

In the example above, you will find that the data we insert "Hello" and the new Integer (100) can be deleted directly with the Remove () method, but for new person ("F1", 18) can the object be deleted directly with the Remove () method? The answer is no.

Container class objects need to compare objects for equality when calling remove, contains, and so on, which involves the Equals method and the Hashcode method of the object type, and for a custom type, You need to override the Equals method and the Hashcode method to implement custom object equality rules.

Note that equal objects should have equal hash Codes

Ieterator interface (Simply put: iterator is a unified way to traverse all the elements in our collection)

  1. All container classes that implement the collection interface have a iterator method that returns an object that implements the iterator interface.

2. The iterator object is called an iterator to facilitate the implementation of the traversal of the container element.

3, iterator implements the following methods:

Let's write a method that iterates through the collection elements using iterator. (Note: Program run information output order may be inconsistent with the order we entered, this is the effect of set set unordered)

ImportJava.util.*; Public classtestcollection{ Public Static voidMain (String args[]) {Collection Collection=NewHashSet (); Collection.add (NewPerson ("Zhang", 1)); Collection.add (NewPerson ("Gao", 2)); Collection.add (NewPerson ("Wang", 3)); Collection.add (NewPerson ("Du", 4)); Collection.add (NewPerson ("Liang", 5)); Collection.add (NewPerson ("Li", 6)); Iterator Iterator=Collection.iterator ();  while(Iterator.hasnext ()) {//The return value type of next () is type object and needs to be converted to the appropriate typePerson person =(person) iterator.next ();        System.out.println (Person.name); }    }}classperson{ PublicString name; Private intAge ;  PublicPerson (String name,intAge ) {             This. Name =name;  This. Age =Age ; }     PublicString GetName () {returnname; }     Public intGetage () {returnAge ; }}

Set interface

  1, the set interface is a sub-interface of the collection, the set interface does not provide additional methods, but the implementation of the set interface of the container class elements are not sequential, and can not be repeated

2. The set interface can correspond to the concept of "set" in mathematics.

3, the J2SDK API provides the container class has hashset, TreeSet and so on ...

Examples of Set methods:

Examples of Set methods:

List interface:

  1, the list interface is a sub-interface of the collection, the implementation of the list interface of the container class elements are sequential, and can be repeated.

2, the list container elements are corresponding to an integer type ordinal record its position in the content, you can access the container element according to the sequence number.

3, L2SDK provides the list container class has arraylist,linkedlist and so on ...

List Method Examples:

List common algorithms:

List Common algorithm examples:

Container of Java

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.