Java.util (Collection interface and map interface)

Source: Internet
Author: User
Tags repetition

Tag: Targe represents a specific dictionary Tor lan blank HTTPS entry

Several main inheritance and implementation classes of 1:collection and map interfaces

1.1 Collection Interface

Collection is the most basic set interface, and a collection represents a set of object, the collection element (Elements). Some collection allow the same elements while others do not. Some can sort and others can't. The Java SDK does not provide classes that inherit directly from collection, and the Java SDK provides classes that inherit from collection, such as list and set.

How do I traverse every element in the collection? Regardless of the actual type of collection, it supports a iterator () method that returns an iteration that uses the iteration to access each element of the collection one at a time. Typical usage is as follows:
Iterator it = Collection.iterator (); Get an iteration child
while (It.hasnext ()) {
Object obj = It.next (); Get the next element
}

1.1.1 Iterator Interface

The iterator interface is also a member of the Java Collection framework, but it is not the same as the collection of the collection series, the map series: The collection collection, the Map series collection is mainly used for other objects, The iterator is used primarily to traverse (that is, iterate) the elements in the collection collection, and iterator objects are also known as iterators.

2 Map interface

The map is a separate interface and does not inherit from the collection. Map is a container that associates key objects with value objects. An object of Key->value

Features:key does not allow repetition .

Mappings are significantly different from sets or lists, and each item in the map is paired, and map is the container that associates the key object with the value object. Each object stored in the map has an associated keyword (key) object that determines where the object is stored in the map, and must be supplied with the corresponding keyword when retrieving the object, as if looking up a word in the dictionary. The keyword should be unique, meaning that the key object in the map does not allow repetition, which is to ensure consistency of the query results.

The keyword itself does not determine where the object is stored, it needs to be processed over a hashing (hashing) technique, producing an integer value called a hash code, which is usually used as a bias, relative to the starting position of the memory region assigned to the map. This determines where the keyword/object pair is stored. Ideally, hash processing should result in a uniformly distributed value within a given range, and each keyword should have a different hash code.

Implementation class:

HashMap implements a key-to-value mapping hash table, which takes a key to the value object, does not have a sequence, gets the value through get (key), allows the storage of an empty object, and allows the key to be empty (since the key must be unique, of course there is only one);

HashTable implements an image, all keys must be non-empty . In order to work efficiently, the class defining the key must implement the Hashcode () method and the equal () method . This class is an inheritance of the previous Java implementation and is usually better used in other classes that implement the image.

Select TreeMap when the order of the elements is important, and use hashmap when the elements do not have to be stored in a particular order. The use of Hashtable is not recommended because HashMap provides all the similar features and is faster. HashMap can also be converted to synchronous when you need to use it in a multithreaded environment.

2.1 Map.entry

The map is an interface in Java, and Map.entry is an internal interface to the map.

Map provides a number of common methods, such as keyset (), EntrySet (), and so on.

The KeySet () method return value is a collection of key values in map; The return value of EntrySet () also returns a set collection of type Map.entry.

Map.entry is an internal interface to the map declaration, which is generic and defined as entry<k,v>. It represents an entity (a Key-value pair) in a map. The interface has a getkey (), GetValue method.

public class Maptest {public static void main (string[] args) {map<string, string> map = Null;map = new Hashmap<string,string> (), Map.put ("name", "Xiaoming"), Map.put ("Age", "+"), Map.put (" Heigh "," 175 ");//method of traversing Map 1set<map.entry<string,string>> Set1 = Map.entryset ();iterator<entry< String, string>> iterator = Set1.iterator (); while (Iterator.hasnext ()) {entry<string, string> Entry = Iterator.next (); Object key = Entry.getkey (); SYSTEM.OUT.PRINTLN (key);} The way to traverse the map is 2set<string> Set2 = Map.keyset ();iterator<string> Iterator1 = Set2.iterator (); while ( Iterator1.hasnext ()) {Object key = Iterator1.next (); Object value = Map.get (key); System.out.println ("key=" + key + "" + "value=" + value);}    Traverse the values of value in map collection<string> c = map.values ();iterator<string> Iterator3 = C.iterator ();     while (Iterator3.hasnext ()) {Object value = Iterator3.next ();    System.out.println (value); }}}

Java.util (Collection interface and map interface)

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.