JAVA Collection Class 2-map interface and iterator

Source: Internet
Author: User
Tags set set

I. Generics (GENERIC)

Because the elements put into the collection by default are object types, it is not possible to see their own types, when the operation of these elements need to be transformed to be available, so inefficient and error-prone, the introduction of generics after the JDK1.5 version, specifies the type of elements in this collection, enhanced the readability and stability of the program.

Format: In <>, the defined type must be a reference type. The following <> can be omitted.

New Arraylist<book> ();

Two. Common methods for map interfaces

The map interface provides an object that maps keys to values. A map cannot contain duplicate keys, and each key can be mapped to at most one value, and the elements in the map are stored in the form of key (key) value pairs.

The map interface also has two subclass implementations of class HashMap (hash table indexed) and treemap (binary tree indexing).

HashMap Common methods:

 Public classtest{ Public Static voidMain (string[] args) {Map<Object,Object> Map1 =NewHashmap<object,object> ();//Create a collection of HashMap classes;Map1.put (Object key, object value);//adds the mapping of the specified key to value to the collection, and its return value is the value that is replaced;Map1.get (Object key);//gets the value corresponding to the specified key;Map1.remove (Object key);//deletes the value corresponding to the specified key; The return value is the value;Map1.remove (Object key,object value);//deletes the specified key and its corresponding value, or returns False if the delete succeeds;Map1.containskey (Object key);//determines whether a key is contained, returns True, and does not return false;Map1.containsvalue (Object value);//determines whether a value is included, returns True, and does not return false;Map1.size ();//gets a few pairs of mappings in the collection;Map1.isempty ();//determines whether there are elements in the set;Map1.putall (Map m);//places the elements in another collection into the collection;Map1.clear ();//clears all elements in the collection;Map1.keyset ();//gets a set set of all the keys in the collection;Map1.values ();//Gets the collection collection of all the value objects in the collection;//getting the key-value pairs in the Map collection can also be done through traversal methods, because the Set class collection has no order and needs to call the constructor method in listSet<object> Set1 =Map1.keyset (); List<Object> List1 =NewArrayList (SET1);  for(inti = 0; I < list1.size (); i++) {Object O1=List1.get (i); Object O2=Map1.get (O1); }    }}

Three. iterators

In order to be able to traverse elements in all collection types, the iterator interface is preferred to create iterators, and the iterator interface is the parent class for all collections.

There are only 3 methods in the iterator interface:

1.hashNext (): Returns True if there are still elements that can iterate;

2.next (): Returns the next element of the iteration;

3.remove (): Removes the last element returned by the iterator from the collection pointed to by the iterator;

Note: The Remove method in the iterator must never be mixed with the Remove (Object) method in list, which throws the concurrentmodificationexception exception.

To illustrate:

 Public classtest{ Public Static voidMain (string[] args) {List<String> list =NewArrayList (); List.add ("QQQQ"); List.add ("Wwww"); List.add ("Eeee"); Iterator<String> iter =List.iterator ();  while(Iter.hasnext ()) {String s=Iter.next ();            System.out.println (s); if(S.equals ("Wwww") ) {iter.remove ();            }} System.out.println (list); }}

The output is:

QQQQWWWWEEEE[QQQQ, Eeee]

Four. Collections Class

 Public classtest{ Public Static voidMain (string[] args) {List<Integer> List1 =NewArraylist<integer>(); List<Integer> List2 =NewArraylist<integer>(); Collections.sort (list1);//default from small to large sort;Collections.shuffle (List1);//random sorting;Collections.reverse (List1);//sort from large to small;Collections.copy (List1, List2);//copies the elements in the List2 to List1, provided that the number of elements in the list1 is not greater than the number of elements in the List2;Collections.fill (List1, Integer);//fills all the elements in the list1 with the same element of the same type;Collections.sort (List1);//sorts all the elements in the List1;Collections.binarysearch (List1, key);//To find the key element position in List1, we need to sort the elements first.    }}

JAVA Collection Class 2-map interface and iterator

Related Article

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.