Common Set Operations in Java and common set operations in Java

Source: Internet
Author: User

Common Set Operations in Java and common set operations in Java
1. Map value pairs are stored. Add the HashMap class of a common derived class:

Put (key, value) to add data to the set

Delete:

Clear () delete all

Remove (key) to clear a single object. Find it based on k.

Obtain:

Size () gets the number of elements

Get (key) obtains the data based on the key

ContainsKey (key) is used to search for this content in the set based on the key. If yes, true is returned, and false is not returned.

ContainsValue (value) checks whether the content in the set has been changed based on the value. If yes, true is returned, and false is not returned.

Public static void main (String [] args) {Map map = new HashMap (); map. put (1, "1"); map. put (2, "2"); map. put (3, "3"); map. put (4, "4"); map. remove (1); // delete System based on the key. out. println ("with" + map. size () + "element"); System. out. println (map. get (4); // search for System based on the key. out. println (map. containsKey (1); System. out. println (map. containsValue ("2 "));

 

Strongly typed set

<> Add a type in.

Map <Tkey, Tvalue> map = new HashMap <Tkey, Tvalue> ();

2. List is stored with an index number. The Derived classes are ArrayList and sorted List. Add:

Add (obj)

Delete:

Clear () delete all

Remove (int) remove according to index number

Remove (obj) remove based on content

Obtain:

Size () gets the number of elements

Get (int) obtains the specified Element Based on the index number.

Contains (obj) is used to find whether the data source exists in the set. If yes, trus is returned, and false is not returned.

Traversal:

1. List can be directly used for loop to traverse the set using Index Numbers

For (int I = 0; I <list. size (); I ++ ){

System. out. println (list. get (I ));

}

2. You can also use the iterator to traverse

Iterator)

An iterator is a design pattern. It is an object that can traverse and select objects in a sequence. Developers do not need to understand the underlying structure of the sequence. An iterator is usually called a "lightweight" object because it is easy to create.

The Iterator function in Java is relatively simple and can only be moved one way:

(1) The method iterator () requires the container to return an Iterator. When the next () method of Iterator is called for the first time, it returns the first element of the sequence. Note: The iterator () method is a java. lang. Iterable interface inherited by Collection.

(2) Use next () to obtain the next element in the sequence.

(3) Use hasNext () to check whether there are any elements in the sequence.

(4) use remove () to delete the elements returned by the iterator.

Iterator is the simplest implementation of the Java Iterator. The ListIterator designed for List has more functions. It can traverse the List in two directions, or insert and delete elements from the List.

 

ArrayList <String> list = new ArrayList <> (); list. add ("jackie"); // The index is 0 //. add (e) add element list. add ("peter"); // The index is 1 list. add ("annie"); // The index is 2 list. add ("martin"); // The index is 3 list. add ("marry"); // The index is 4 System. out. println ("Total" + list. size () + "element ");//. size () queries the total number of element lists. remove (3 );//. remove (index) deletes the list according to the index number. remove ("marry ");//. remove (Object o) deletes System according to the content. out. println (list. contains ("martin "));//. contains (obj) query whether the System element exists in the collection. out. println ("the index number is 2:" + list. get (2 ));//. get (index) searches for System Based on the index number. out. println ("Total" + list. size () + "element"); // use the for loop to traverse for (int I = 0; I <list. size (); I ++) {System. out. println (list. get (I); // for Loop use index number to traverse list} // use Iterator to traverse Iterator it = list. iterator (); // create an iterator while (it. hasNext ()){//. hasNext () checks whether there is any element in the sequence object obj = it. next ();//. next () to obtain the next element in the sequence System. out. println (obj );}

Strongly typed set

<> Add a type in.

List <T> list = new ArrayList <T> ();

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.