Java serialized 91-Map usual method, Hashtable, SortedMap

A, Map common methods Introduction

 

Package com.bjpowernode.java_learning; 


Import the java.util.HashMap; 

Import Classes in java.util *. ; 


public  class D91_1_MapCommonMethod { 

  public  static  void main (String [] args) { 

    the Map persons = new new the HashMap (); // the HashMap of The default initial capacity of 16, a default loading factor 0.75 

    persons.put ( "10000", "JACK" ); 

    persons.put ( "10001", "ZHANGXINLEI" ); 

    persons.put ( "10002", "Yaokun" ); 

    persons.put ( "10003", "ZHAOZEKUN" );

    persons.put("10004", "Liduo" ); 

    persons.put ( "10005", "DIAOBI" ); 

    // 1. determining whether the collection contains such a value 

    // Note: Map of repeated key, value is employed "cover" 

    System.out.println (persons.containsValue ( "LUCK" )); 

    // 2. value acquired by Key 

    String K = "10001" ; 

    Object V = persons.get (K); 

    System.out.println (v); 

    // 3. delete the key by key on 

    persons.remove ( "10002" ); 

    System.out.println (persons.get ( "10002" )); 

    // 4.Get all value 

    Collection values = persons.values ();

    Iterator it = values.iterator();

    while(it.hasNext()) {

      System.out.println(it.next());

    }

    //5.获取所有的key

    //以下程序演示遍历Map集合

    Set keys = persons.keySet();

    Iterator it2 = keys.iterator();

    while(it2.hasNext()) {

      Object k2 = it2.next();

      Object v2 = persons.get(k2);

      System.out.println(k2+"-->"+v2);

    }

    System.out.println("--------------------------------------");

    //Map 6.entrySet the key and value are set to print out, an equal sign connected intermediate 

    the Set S2 = persons.entrySet (); 

    the Iterator I3 = s2.iterator (); 

    the while (i3.hasNext ()) { 

      the System.out. the println (i3.next ()); 

    } 

  } 

}

二、Hashtable

1.HashMap default initial capacity is 16, the default load factor is 0.75

2.Hashtable default initial capacity is 11, default load factor is 0.75

 

Package com.bjpowernode.java_learning; 

Import Classes in java.util *. ; 


public  class D91_2_Hashtable_Propet { 

  public  static  void main (String [] args) { 

    // 1. Create a property class object 

    the Properties P = new new the Properties (); 

    // 2. deposit 

    // Note that key can not be repeated, if repetition will result in value coverage 

    p.setProperty ( "Driver", "oracle.jdbc.driver.OracleDriver" ); 

    p.setProperty ( "username", "scott" ); 

    the p-. the setProperty ( "password", "Tiger" ); 

    p.setProperty ("url","jdbc:oracle:thin:@192.167.1.100:1521:bjpowernode");

    //3.取出来,通过key获取value

    String v1 = p.getProperty("driver");

    String v2 = p.getProperty("username");

    String v3 = p.getProperty("password");

   

    System.out.println(v1);

    System.out.println(v2);

    System.out.println(v3);

   

  }

​

}

​

Three, SortedMap

1.SortedMap key features of: disorder not repeat, but the deposit into the elements can be arranged according to size.

2. If you want to automatically sort, the key portion of the required elements: (1) implement the Comparable interface or (2) writing a single comparator

 

package com.bjpowernode.java_learning;

import java.util.*;

​

public class D91_3_TreeMap {

  public static void main(String[] args) {

    //Map,key存储Product91,value存储个数

    SortedMap products = new TreeMap();

    /**

     * 匿名内部类(单独写一个比较器的方法)

     * SortedMap products = new TreeMap(new Comparator(){

     *     public int compareTo(Object o) {

     *      double price1 = this.price;

     *      double price2 = ((Product91)o).price;

     *      if(price1<price2) {

     *        return -1;

     *      }else if(price1>price2) {

     *        return 1;

     *     

     *      }else {

     *        return 0;

     *      }

     *    }

       *})

     */

    //准备对象

    Product91 p1 = new Product91("西瓜",1.0);

    Product91 p2 = new Product91("黄瓜",2.0);

    Product91 p3 = new Product91("南瓜",3.0);

    Product91 p4 = new Product91("冬瓜",4.0);

    //添加

    products.put(p1,8);//后面这个value无所谓,我们暂且认为是斤数

    products.put(p2,4);

    products.put(p3,4);

    products.put(p4,4);

    //遍历

    Set keys = products.keySet();

    Iterator it = keys.iterator();

    while(it.hasNext()) {

      Object k = it.next();

      Object v =products.get(k);

      System.out.println(k+"-->"+v);

    }

   

   

  }

}

class Product91 implements Comparable{

  String name;

  double price;

  Product91(String name,double price){

    this.name = name;

    this.price = price;

  }

  public String toString() {

    return "Poduct91(name=" + name + ",price=" + price +")";

  }

  public int compareTo(Object o) {

    double price1 = this.price;

    double price2 = ((Product91

        )o).price;

    if(price1<price2) {

      return -1;

    }else if(price1>price2) {

      return 1;

   

    }else {

      return 0;

    }

  }

}

四、源码:

D91_1_MapCommonMethod.java

D91_2_Hashtable_Propet.java

D91_3_TreeMap.java

https://github.com/ruigege66/Java/blob/master/D91_1_MapCommonMethod.java

https://github.com/ruigege66/Java/blob/master/D91_2_Hashtable_Propet.java

https://github.com/ruigege66/Java/blob/master/D91_3_TreeMap.java

2.CSDN:https://blog.csdn.net/weixin_44630050

3.博客园:https://www.cnblogs.com/ruigege0000/

4.欢迎关注微信公众号:傅里叶变换,个人公众号,仅用于学习交流,后台回复”礼包“,获取大数据学习资料

 

 

Guess you like

Origin www.cnblogs.com/ruigege0000/p/12393188.html