Comparable interface, set interface, map interface, list interface and generics

Source: Internet
Author: User
Tags comparable int size map class

Comparable interface
Classes that implement the comparable interface, which can be compared to size.
There is only one method in the comparable interface
public int CompareTo (object obj);
This method: Returns 0 indicating this = = obj
Returns a positive number indicating this > obj
Returns a negative number indicating this > obj
Classes that implement the comparable interface determine how objects are sorted by the CompareTo method.
Can be sorted by sort () method.

Selection of data structure:
Measurement criteria: efficiency of reading and efficiency of change
Array read Fast write slow
Linked change fast read slowly
Hash between the two

Map interface
Classes that implement the map interface are used to store key-value pairs.
The implementation classes of the map interface are HashMap and TreeMap.
Key-value pairs stored in the map class are identified by keys, so key values cannot be duplicated.

Object put (object Key,object value);//If key already exists, return the value of the original key
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 to compare.

Generic type:
Enhances the readability and stability of programs
Defining the types of objects in a collection while defining a collection
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 type
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);
}
}

Can be specified when defining a collection
You can also use iterator to specify when looping

List interface

The list interface is a collection sub-interface, and the elements in the container class that implement the list interface are sequential and can be duplicated.
The elements in the list container correspond to the ordinal number of an integer in the container, and the elements in the container can be accessed by ordinal.
The list container class provided by JDK has arraylist,linkedlist and so on.

Object get (int index);

Object Set (int index, object element);

void Add (int index, object element);

Object Remove (int index);

int indexof (object o);

int LastIndexOf (object o);

List common algorithms:
Class Java.util.collections provides the most commonly used algorithms in the list:

void sort (list); Sort the elements within the list container

void Shuffle (list); Random ordering of objects within the list container

void reverse (list); Sort the objects in the list container in reverse order

void Fill (List, object); Rewrite the entire list container with a specific object

void copy (list dest,list src); Copy the SRC list container contents to the Dest list container

int BinarySearch (list,object); For the Order list container, find the specific object using the binary lookup method

Set interface and its implementation subclass

The set interface is a collection sub-interface, and the set interface does not provide additional methods, but the element division in the container class that implements the set interface is not sequential and cannot be duplicated.

The set container can correspond to the concept of "set" in mathematics. The Set container class provided in the JDK API has Hashset,treeset and so on.

Example:

Import java.util.*;p ublic class testset{public static void Main (string[] args) {set S1 = new HashSet ();        Set s2 = new HashSet ();        S1.add ("a"); S1.add ("B"); S1.add ("C");                S2.add ("D"); S2.add ("a"); S2.add ("B");        Set sn = new HashSet (S1); Sn.retainall (S2);        To find the intersection set su = new HashSet (S1);   Su.addall (S2);        The System.out.println (SN) of the set is obtained;    System.out.println (SU); }}

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.