Java--map interface, variable parameter, collections (Collection implementation Class)

Source: Internet
Author: User
Tags key string set set shuffle

one, map interface

A collection in a map is a two-column collection (a key-value pair), and a collection in amap cannot contain duplicate keys , and values can be duplicated; each key can only correspond to one value.

    • hashmap<k,v>: The hash table structure in which the data is stored, the access order of the elements is not guaranteed to be consistent. The hashcode () method,equals () method, which requires overriding the key, is required to ensure that the key is unique and non-repeatable.
    • linkedhashmap<k,v>:HashMap has sub-class Linkedhashmap, storing data using the hash table structure + linked list structure. The structure of the linked list ensures that the access order of the elements is consistent; the hashcode () method, theequals () method, which can guarantee the uniqueness and non-repetition of keys through the hash table structure.

Note: The collection in theMap interface has two generic variable <k,v>, which , when used, assigns a data type to two generic variables. The data type of the two generic variables <K,V> can be the same or different.

Map Common methods

1. Store key-value pairs in the collection
/* *  Store the key-value pairs in the collection *  v put (k,v) K as the Key object, V as the value of the object *  store a duplicate key, the original value, overwrite *  return value generally return NULL, *  when storing duplicate keys, Returns the value before being overwritten */public static void function () {//Create collection object, HashMap, store object, key is string, value is integer map<string, integer> map = new hashmap& Lt String, integer> (); Map.put ("A", 1), Map.put ("B", 2); Map.put ("C", 3); SYSTEM.OUT.PRINTLN (map);}
2. Get the value
/* * Through Key object, Get value Object * V get (K) * If there is no this key in the collection, return null */public static void Function_1 () {//Create collection object as Key object integer, value object store string Map<integ er,string> map = new Hashmap<integer, string> (), Map.put (1, "a"), Map.put (2, "B"), Map.put (3, "C"); SYSTEM.OUT.PRINTLN (map); String value = Map.get (4); System.out.println (value);}
3. Remove key value pairs
/*  Remove a key-value pair from the collection, return the value before removal *  V Remove (K) */public static void Function_2 () {map<integer,string> Map = new Hash Map<integer, String> () map.put (1, "a"), Map.put (2, "B"), Map.put (3, "C"); SYSTEM.OUT.PRINTLN (map); String value = Map.Remove (33); System.out.println (value); SYSTEM.OUT.PRINTLN (map);}
4. Map set traversal--get key by value
import java.util.hashmap;import java.util.iterator;import Java.util.Map; Import java.util.set;/* * Map collection Traversal * Using keys to get values * Map Interface Definition Method keyset * All keys, stored in Set set */public class MapDemo1 {public S tatic void Main (string[] args) {/* * 1. Call the method of the Map collection keyset, all keys are stored in the Set collection * 2. Iterate through the set collection to get all the elements in the set set (the key in the map) * 3. Call the map set */map<string,integer> Map = new hashmap<string,integer> () map.put ("A", one by one), Map.put ("B", 12 ); Map.put ("C", Map.put ("D", 14);//1. Call the Map collection method KeySet, all keys are stored in the Set collection set<string> set = Map.keyset ();//2. Iterates through the set collection to get all the elements in the set set (the keys in the map) iterator<string> it = Set.iterator (); while (It.hasnext ()) {//it.next Returns a set collection element. That is, the key//3 in the map. Call the Map collection method get, get to the value by key string key = It.next (); Integer value = Map.get (key); System.out.println (key+ "....." +value); System.out.println ("======================="); for (String Key:map.keySet ()) {Integer value = Map.get (key); System.out.println (key+ "....." +value);}} 
5, Map set traversal--EntrySet Method
Import Java.util.hashmap;import java.util.iterator;import java.util.map;import java.util.set;/* * MAP collection Get Way * EntrySet method, key-value pair mapping relationship (marriage certificate) Get * Implementation steps: * 1. Call the Map Collection method EntrySet () to store the mapping relationship object in the collection to the Set collection * Set<entry <K,V> > * 2. Iterate Set set * 3. Gets the element of the set set, which is the mapping relation object * 4. By mapping the Relational object method Getket, GetValue gets the key value pair * * * Creates an inner class object outer class. Inner class = new */public class MapDemo2 {public static void main (String [] args) {map<integer,string> Map = new Hashmap<integer, string> () map.put (1, "abc"); Map.put (2, "BCD"); Map.put (3, "CDE");//1. Call the Map Collection method EntrySet () to store the mapping relationship objects in the collection to the Set collection Set<map.entry <Integer,String> > Set = Map.entryset ();//2. Iterate set collection Iterator<map.entry <Integer,String> > It = Set.iterator (); while (It.hasnext ()) {//3. Gets the elements of the set set, Is the mapping relational object//It.next What object is obtained, is also Map.entry object Map.entry<integer, string> Entry = It.next ();//4. By mapping the Relational object method Getket, GetValue gets the key value pair integer key = Entry.getkey (); String value = Entry.getvalue (); System.out.println (key+ "...." +value);} System.out.println ("========================="); for (Map.entry<integer, string> entry:map.entrySet ()) { System.out.println (Entry.getkey () + "..." +entry.getvalue ());}}
Hashtable implementation Class
Import Java.util.hashtable;import java.util.map;/* *  Map Interface Implementation class Hashtable *  Underlying data result hash table, features and HashMap is the same *  Hashtable Thread-Safe collection, running slow *  HASHMAP thread Unsafe collection, running fast *   *  Hashtable fate and vectors are the same, starting from JDK1.2, replaced by more advanced HASHMAP *   *  HashMap allow null values to be stored, NULL key *  Hashtable does not allow null values to be stored, NULL key *   *  Hashtable his child, sub-category Properties remain active in the development stage */public Class Hashtabledemo {public static void main (string[] args) {map<string,string> Map = new Hashtable<string,strin G> (); Map.put (null, NULL); SYSTEM.OUT.PRINTLN (map);}}
Linkedhashmap Implementation Class
Import java.util.linkedhashmap;/* *  linkedhashmap inherit HashMap *  ensure the order of iterations */public class Linkedhashmapdemo {public static void Main (string[] args) {linkedhashmap<string, string> link = new linkedhashmap<string, string> (); Link.put ("1", "a"), Link.put ("a"), Link.put ("a"), Link.put ("n", "a"); System.out.println (link);}}
two, variable parameters
/* *  JDK1.5 new features, variable parameters of method *  Premise: Method parameter data type OK, number of parameters arbitrary *  variable parameter syntax: Data type ... Variable name *  variable parameter, essentially an array */public class Varargumentsdemo {public static void main (string[] args) {//Call a method with a variable parameter, pass the parameter, can be arbitrarily//getsum (); int sum = Getsum (5,34,3,56,7,8,0); SYSTEM.OUT.PRINTLN (sum); function (n/a);} /* * Variable parameter considerations * 1. In one method, a mutable parameter can have only one * 2. Variable parameter, must be written in the last one of the parameter list */public static void function (OBJECT...O) {  }/* * Definition method, calculates the variable parameter implementation of 10 integers and * methods */public static int get Sum (int ... a) {int sum = 0; for (int i:a) {sum = sum + i;} return sum;}}
Third, collections--collection implementation class

Are all under the list implementation class

1. Sorting
/* *  collections.sort static method *  for list collection, ascending order */public static void function () {//Create List collection List<string> list = new arraylist<string> (); List.add ("Ewrew"); List.add ("qwesd"); List.add ("qwesd"); List.add ("BV"); List.add (" Wer "); SYSTEM.OUT.PRINTLN (list);//method of invoking the Collection tool class Sortcollections.sort (list); SYSTEM.OUT.PRINTLN (list);}
2, two-point search
/* * Collections.binarysearch static method * Binary search for list collection, method parameters, pass the list collection, pass the found element */public static void function_1 () {list< integer> list = new arraylist<integer> (); List.add (1); List.add (5); List.add (8); List.add (+); List.add (15); List.add (20);//Call Tool class static method Binarysearchint index = Collections.binarysearch (list, 16); SYSTEM.OUT.PRINTLN (index);}
3. Random sorting
/* * Collections.shuffle method * Random arrangement of elements in the list collection */public static void Function_2 () {list<integer> list = new ArrayList <Integer> (); List.add (1); List.add (5); List.add (9); List.add (one); List.add (8); List.add (+); List.add (15); List.add (20); SYSTEM.OUT.PRINTLN (list);//Call tool class method shuffle to set randomly collections.shuffle (list); SYSTEM.OUT.PRINTLN (list);}

Java--map interface, variable parameter, collections (Collection implementation Class)

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.