The Map collection in Java

Source: Internet
Author: User
Tags set set

import java.util.collection;import java.util.hashmap;import java.util.iterator;import  java.util.map;import java.util.set;public class maptest {/** common methods for Map collection  * void  clear ();  empties Mapboolean isempty (), determines whether the collection is empty int size ();  Gets the number of key-value pairs in the map.  object put (Object key, object value);  adds a key-value pair Object get (Object key) to the collection , get Value  boolean containskey (Object key) through key;  determine if the map contains such key boolean  Containsvalue (object value);  determine if the map contains such value   object remove (Object key) ;  deletes key-value pairs by key.   collection values ();  gets all the Value  set keyset () in the map collection;   Get all the Key  set entryset () in the map; returns the  Set  view of the mappings contained in this map.      Note: The elements stored in the key section of the map collection need to be rewritten simultaneously with the Hashcode+equals method . *  @param  args */public  static void main (String[] args)  {//&nbsP TODO AUTO-GENERATED METHOD STUB//1. Creating a Map collection map personmap = new hashmap ();// 2. Add element Personmap.put ("10000",  "Nihao") to the Map collection;p ersonmap.put ("10002",  "Wobuhao");p ersonmap.put ("1000", "Wohenhao");p ersonmap.put ("10000", "Shabi"),//3. Determine the number of key-value pairs, the key in the map is unordered and non-repeatable, if repeated, overwrites the original value value System.out.println ( Personmap.size ());//4. Interprets whether a key value System.out.println (Personmap.containskey ("10000")) is included in the collection, and//5. Determines whether a value is contained in the collection. Note that the duplicate in the map, value is overridden. System.out.println (Personmap.containsvalue ("Shabi"))//6. Get valuestring key1= "10000" via key; System.out.println (Personmap.get (key1))//7. Delete the key value pair System.out.println (Personmap.size ()) by Key;p Ersonmap.remove ( Key1); System.out.println (Personmap.size ());//8. Get all value values Collection c = personmap.values (); Iterator  it = c.iterator (); while (It.hasnext ()) {System.out.println (It.next ());} 9. Get all the keys and traverse the Map Collection set key = personmap.keyset (); It = key.iterator (); object id  = null;object&Nbsp;name = null;while (It.hasnext ()) {id = it.next (); Name = personmap.get (ID); System.out.println (id+ "----->" +name);} 10. Replace the map with Set set Set maptoset = personmap.entryset (); It = maptoset.iterator (); while ( It.hasnext ()) {System.out.println (It.next ());} 11.properties//hashmap default initialization capacity is 16, the default load factor 0.75//hashtable default initialization capacity is 11, the default loading factor is 0.75//java.util.properties; It is also composed of key and value, but both key and value are string Types Properties p = new properties ();p. SetProperty ("username",   "Zhangdan");p. SetProperty ("id",  "");p. SetProperty ("Grade",  "137");//3. Fetch, get valuestring via key  v1 = p.getproperty ("Driver"); String v2 = p.getproperty ("username"); String v3 = p.getproperty ("password"); String v4 = p.getproperty ("url"); System.out.println (v1); System.out.println (v2); System.out.println (v3); SYSTEM.OUT.PRINTLN (v4);}}


This article is from the "Gaogaozi" blog, make sure to keep this source http://hangtiangazi.blog.51cto.com/8584103/1669595

The Map collection in Java

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.