The container in Java (I) is an important foundation and an important idea.

Source: Internet
Author: User

Container API
Before talking about containers, I 'd like to talk about arrays first. Array features: once an array is determined, it cannot be changed. If we want to change the array, add or delete one, we can only create a new array and copy the previous one. Here is an image:

Therefore, we hope that we can make it grow, decrease, and decrease if we want it to grow.
Next, let's take a look at the container API:
1. The Container API provided by J2SDK is located in the java. util package.
2. the class diagram structure of container API is as follows:

Collection interface: defines the methods for accessing a group of objects. Its Sub-interfaces Set and List define the storage methods respectively.
The data objects in the Set are unordered and cannot be duplicated (that is, arrays in mathematics ).
The data objects in the List are ordered and can be repeated.
The Map interface defines the method for storing "key-value ing pairs.
Example
[Java]
Import java. util. *; // introduce the package
 
Public class basicContainer
{
Public static void main (String [] args)
{
// The parent class references a subclass object.
Collection c = new HashSet ();
C. add ("hellow ");
C. add (new Name ("f1", "l1 "));
C. add (new Integer (100 ));
C. remove ("hellow"); // remove calls the equals method of the object.
C. remove (new Integer (100 ));
System. out. println (c. remove (new Name ("f1", "l1"); // If the deletion is successful, true is returned. Otherwise, false is returned.
System. out. println (c); // print c
}
}
 
// Define a Name class
Class Name
{
Private String firstName, lastName;
Name (String firstName, String lastName)
{
This. firstName = firstName;
This. lastName = lastName;
}
Public String GetFirstName ()
{Return firstName ;}
Public String GetLastName ()
{Return lastName ;}
Public String toString ()
{Return firstName + "" + lastName ;}
}

Note:
1. When the remove method is called, it is the called equals method. If it is equal, it can be removed.
2. The Name does not overwrite the toString () method of the object, so the deletion fails.
Running result:

 
When calling the remove and contains methods, the container class object must compare whether the objects are equal. This involves the equals and hashCode methods of the object type. For custom types, you need to override the equals and hashCode methods to implement custom object equality rules.
Note: equal objects (equals) should have equal hashCode.
We add the equals and hashCode methods to the Name class above.
[Java]
Import java. util. *; // introduce the package
 
Public class basicContainer
{
Public static void main (String [] args)
{
// The parent class references a subclass object.
Collection c = new HashSet ();
C. add ("hellow ");
C. add (new Name ("f1", "l1 "));
C. add (new Integer (100 ));
C. remove ("hellow"); // remove calls the equals method of the object.
C. remove (new Integer (100 ));
System. out. println (c. remove (new Name ("f1", "l1"); // If the deletion is successful, true is returned. Otherwise, false is returned.
System. out. println (c); // print c
}
}
 
// Define a Name class
Class Name
{
Private String firstName, lastName;
Name (String firstName, String lastName)
{
This. firstName = firstName;
This. lastName = lastName;
}
Public String GetFirstName ()
{Return firstName ;}
Public String GetLastName ()
{Return lastName ;}
Public String toString ()
{Return firstName + "" + lastName ;}

// Rewrite the equals method. It is best to copy the object's method declaration.
Public boolean equals (Object obj)
{
If (obj instanceof Name)
{
Name name = (Name) obj;
Return (firstName. equals (name. firstName ))&&
(LastName. equals (name. lastName ));
}
Return super. equals (obj );
}

// Override the hashCode Method
Public int hashCode ()
{
Return firstName. hashCode ();
}
}

Running result:

 
Note:
1. Override the hashCode method: the two objects equals, firstName must be equal, firstName must be equal, and its hashCode must be equal, because the string class has overwritten the hashCode method.
2. When to use hashCode: when an object of this class is used as a key value. For example, there are many people in a human affairs management system and many people-related materials. If you put everything in the memory, it will be hard to figure out ,, in this way, you can put the name in the index. When looking for relevant information, find its name first and then find the relevant information.
3. Two objects, equals, must have the same hashCode. Therefore, to override equals, you must override the hashCode method. For example, when we look up a dictionary, we first look up an index, find an entry, and then find the corresponding word. In this sense, the index is an object, the word is a value. It cannot be said that the index is the same and different values are found. Therefore, if the two objects are the same, the hashCode must be the same.

 

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.