Java API Interface Chapter (II)

Source: Internet
Author: User
Tags hash int size

Map interface

A map is an object that maps a key to a value. A map cannot contain duplicate keys: Each key can map a value at most. The map interface looks like this:

public interface Map {
// Basic Operations
Object put(Object key, Object value);
Object get(Object key);
Object remove(Object key);
boolean containsKey(Object key);
boolean containsValue(Object value);
int size();
boolean isEmpty();

// Bulk Operations
void putAll(Map t);
void clear();

// Collection Views
public Set keySet();
public Collection values();
public Set entrySet();
// Interface for entrySet element
public interface Entry {
Object getKey();
Object getValue();
Object setValue(Object value);
}
}
 

The JDK contains two new universal map implementations, one is HashMap, it stores its items in a hash table, is the best implementation, and the other is TreeMap, which stores its items in a red-black tree, which guarantees the order of iterations. In addition, Hashtable has been improved to implement the map.

Comparison with a hash table

If you have used Hashtable, you should already be familiar with the general style of map (of course map is an interface, and Hashtable is a concrete implementation). Here are the main differences:

Map provides a collection view that supports iterative processes directly as an alternative to enumeration objects. The collection view greatly enhances the expressiveness of the interface, as the following course will describe.

The map allows you to iterate over a key, value, or key-value pair; Hashtable does not provide a third option.

Map provides a secure way to delete items during an iteration; Hashtable is not.

Further, map fixes some minor flaws on the Hashtable interface. Hashtable has a method called contains that returns True if Hashtable contains a given value. Understanding from its name, you may expect that if Hashtable contains a given key, this method will return a true, because the key is a Hashtable primary access mechanism. By renaming this method to Containsvalue, the map interface eliminates the source of confusion and also improves interface consistency: Containsvalue and containskey can be well matched for parallelism.

Basic operations

The functions of the basic operations (put, get, remove, ContainsKey, Containsvalue, S, a, and isempty) are very similar to their counterparts in Hashtable. The following simple program generates a frequency table for the words in the argument list. The frequency table maps each word and the number of times it appears in the parameter list.

import java.util.*;
public class Freq { private static final Integer ONE = new Integer(1);
public static void main(String args[]) {
Map m = new HashMap();
// Initialize frequency table from command line
for (int i=0; i$#@60; args.length; i++) {
Integer freq = (Integer) m.get(args[i]);
m.put(args[i], (freq==null ? ONE :
new Integer(freq.intValue() + 1)));
}

System.out.println(m.size()+" distinct words detected:");
System.out.println(m);
}
}
 

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.