Comparable interface, map interface, generic

Source: Internet
Author: User
Tags map class
Comparable Interface

Class that implements the comparable interface. objects can be compared between different sizes.
The comparable interface has only one method.
Public int compareto (Object OBJ );
This method: returns 0 indicating this = OBJ
Returns a positive value indicating this> obj.
Returns a negative number, which indicates this> obj.
The class implementing the comparable interface uses the compareto method to determine the object sorting method.
You can use the sort () method to sort data.

Data structure selection:
Measurement criteria: Read efficiency and change Efficiency
Slow array read/write speed
Linked changed to fast-read and slow
Hash

 

Map Interface

The class that implements the map interface is used to store key-value pairs.
Implementation classes of the map interface include hashmap and treemap.
Key-value pairs stored in the map class are identified by keys. Therefore, key-value pairs cannot be repeated.
Object put (Object key, object value); // if the key already exists, the value of the original key is returned.
Object get (Object key );
Object remove (Object key );
Boolean containskey (Object key );
Boolean containsvalue (object value );
Int size ();
Boolean isempty ();
Void putall (MAP t );
Void clear ();
Use the hashcode () method for comparison.

 

Generic:

Enhance program readability and Stability
Define the object type in the collection at the same time when defining the set
Example:
Import java. util .*;
Public class testargswords {
Private Static final int one = 1;
Public static void main (string [] ARGs ){
Map <string, integer> M = new hashmap <string, integer> (); // generic
For (INT I = 0; I <args. length; I ++ ){
Int freq = (integer) M. Get (ARGs [I]) = NULL? 0 :( integer) M. Get (ARGs [I]);
M. Put (ARGs [I], freq + 1 );
}
System. Out. println (M. Size () + "distinct words detected :");
System. Out. println (m );
}
}
You can specify
You can also use iterator to specify

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.