Basic usage of MAP, Set, list in Java __java

Source: Internet
Author: User
Tags delete key map class set set
Java Collection class is divided into three kinds
Set (set)
: Objects in the collection are not sorted in a particular way. And there is no duplicate object, but some of the objects in the implementation class are sorted in a specific way. -Disordered, cannot repeat
list (listing): The objects in the collection are sorted by retrieval location, can have duplicate objects, and allow objects to be retrieved by object in the central index, and the list and array are somewhat similar. --orderly, can repeat

map (map): Each element in a collection contains a pair of key objects and value objects, there are no duplicate key objects in the collection, and value objects can be duplicated, and some of its implementation classes can sort the key objects in the collection.

Collection is the most basic collection interface, declaring a common method for Java collections, which include only set and list.
Both the Set and the list inherit conllection, but the map does not.

Methods for collection interfaces:
boolean Add (Object o): Add a reference to an object to the collection
void Clear (): Deletes all objects in the collection, that is, they no longer hold references to these objects
Boolean isempty (): Determines whether the collection is empty
Boolean contains (Object o): Determines whether a reference to a particular object is held in the collection
Iterartor iterator (): Returns a iterator object that can be used to traverse the elements in the collection
boolean remove (Object o): Deletes a reference to an object from the collection
int size (): Returns the number of elements in the collection
object[] ToArray (): Returns an array that includes all the elements in the collection

About: the iterator () and ToArray () methods are all elements of a collection that return a iterator object, which returns an array that contains all the elements in the collection.


Use of Set:
Using set in Java makes it easy to save the type you need in a variable as a collection type. Main application in the display list is a reference to an object, no duplicate objects

    Set set=new hashset ();
    String S1=new string ("Hello");
    String s2=s1;
    String S3=new string ("World");
    Set.add (S1);
    Set.add (S2);
    Set.add (S3);
    System.out.println (Set.size ());//The number of objects in the Print collection is 2.
    the set's Add () method determines whether the object is already stored in the collection.
    Boolean isexists=false;
    Iterator Iterator=set.iterator ();
    while (It.hasnext ()) {
        String oldstr=it.next ();
        if (Newstr.equals (OLDSTR)) {
        isexists=true;
    }

Use of list:

list[< data Type] List = new arraylist[< data type;]
The common methods of list are:
Get the number of elements in the set: List.size ();
Add element: Default add: List.add (e);

Specifies that the subscript be added (after adding the subscript element to move backward): List.add (index,e);
Delete element: Returns whether Delete: List.remove (e);

Deletes the element that specifies the subscript directly (deletes only the first matching element found): List.remove (index);

Replace element (replace the element with specified subscript): List.set (index,e);

remove element: List.get (index);

Empty the Set: List.clear ();

Determines whether an element exists in the collection (there is a return of true, no return false): List.contains (e);
Compares all elements in two sets: two objects must be equal: List.equals (LIST2);
Two objects are not necessarily equal: list.hashcode () = = List2.hashcode (); (The Equals method for two equal objects must be true, but two hashcode equal objects are not necessarily equal objects.)
Gets the element subscript: The element existence returns the subscript of the first element found, and returns -1:list.indexof (e) If it does not exist;

The element exists returns the subscript of the last element found, and returns -1:list.lastindexof (e) If it does not exist;

Determines whether the collection is empty (null returns TRUE, Non-null returns false): List.isEmpty ();

Returns the Iterator Collection object: List.iterator ();

Converts a collection to a string: list.tostring ();

Intercepting the collection (beginning with Fromindex, ending before Toindex, [Fromindex,toindex)): List.sublist (Fromindex,toindex);
Convert a collection to an array: default type: List.toarray (); Specifies the type (objects is an array object of the specified type and assigns the converted array to the objects array): List.toarray (objects);

Use of Map:

map<object,object> map = new Hashmap<object, object> ();

Map.put (Object key, Object value): Adding elements to the collection
Map.Remove (Object key): Delete key-related elements
Map.putall (Map t): Add all elements from a specific image to the image
Map. Clear (): Remove all mappings from the image

Several ways to traverse a map:

1, the most verbose way to traverse the map:

Set keys = Map.keyset ();
if (keys!= null) {
    iterator iterator = Keys.iterator ();
    while (Iterator.hasnext ()) {
    Object key = Iterator.next ();
    Object value = Map.get (key);
    }
}
2, using the Map.entry class, you can get all the information at the same time.

The map class provides a method called EntrySet () that returns a map.entry set of objects that are instantiated. Next, the Map.entry class provides a Getkey () method and a GetValue () method, so the above code can be organized more logically.

Map.entry also provides a SetValue () method

map<integer,string> map = new Hashmap<integer, string> ();
Map.put (001, "World");
Map.put (002, "Hello");
For (Map.entry<integer, string> entry:map.entrySet ()) {
    System.out.println (Entry.getkey () + "= =" + Entry.getvalue ());
}
Output:
1===world
2===hello
3, the easiest way to use the enhanced for loop:
for (int key:map.keySet ()) {
    System.out.println (key + ":" + map.get (key));
}




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.