Java连载91-Map常用方法、Hashtable、SortedMap

2020-03-02 02:34:54来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

Java连载91-Map常用方法、Hashtable、SortedMap

一、Map常用方法简介

 

package com.bjpowernode.java_learning;

?

import java.util.HashMap;

import java.util.*;

?

public class D91_1_MapCommonMethod {

  public static void main(String[] args) {

    Map persons = new HashMap();//HashMap的默认初始化容量为16,默认加载因子时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.判断集合中是否包含这样的value

    //注意:如果Map中的key重复了,value采用的是“覆盖”

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

    //2.通过key来获取value

    String k = "10001";

    Object v = persons.get(k);

    System.out.println(v);

    //3.通过key删除键值对

    persons.remove("10002");

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

    //4.获取所有的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("--------------------------------------");

    //6.entrySet将Map集合key和value都打印出来,中间使用等号进行连接

    Set s2 = persons.entrySet();

    Iterator i3 = s2.iterator();

    while(i3.hasNext()) {

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

    }

  }

}

二、Hashtable

1.HashMap默认初始化容量是16,默认加载因子为0.75

2.Hashtable默认初始化容量是11,默认加载因子为0.75

 

package com.bjpowernode.java_learning;

import java.util.*;

?

public class D91_2_Hashtable_Propet {

  public static void main(String[] args) {

    //1.创建属性类对象

    Properties p = new Properties();

    //2.存

    //注意key是不能重复,如果重复就会造成value覆盖

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

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

    p.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);

   

  }

?

}

?

三、SortedMap

1.SortedMap中的key特点:无序不可以重复,但是存进去的元素可以按照大小进行排列。

2.如果想要自动排序,key部分的元素需要?:(1)实现Comparable接口或者(2)单独写一个比较器

 

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

 

 


原文链接:https://www.cnblogs.com/ruigege0000/p/12393188.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:多线程笔记 - 伪异步IO

下一篇:【视频+图文】带你快速掌握Java中含continue语句的双重for循环