zoukankan      html  css  js  c++  java
  • Java重温学习笔记,Hashtable 类、Properties 类

    一、Hashtable是原始的java.util的一部分, 是一个Dictionary具体的实现 。

    然而,Java 2 重构的Hashtable实现了Map接口,因此,Hashtable现在集成到了集合框架中。它和HashMap类很相似,但是它支持同步。像HashMap一样,Hashtable在哈希表中存储键/值对。当使用一个哈希表,要指定用作键的对象,以及要链接到该键的值。

    import java.util.*;
    
    public class MyDemo {
    
       public static void main(String args[]) {
          Hashtable balance = new Hashtable();
          balance.put("Zara", new Double(3434.34));
          balance.put("Mahnaz", new Double(123.22));
          balance.put("Ayan", new Double(1378.00));
          balance.put("Daisy", new Double(99.22));
          balance.put("Qadir", new Double(-19.08));
    
          // Show all balances in hash table.
          Enumeration names = balance.keys();
          while(names.hasMoreElements()) {
             String str = (String) names.nextElement();
             System.out.println(str + ": " + balance.get(str));
          }
          System.out.println();
          // Deposit 1,000 into Zara's account
          double bal = ((Double)balance.get("Zara")).doubleValue();
          balance.put("Zara", new Double(bal+1000));
          System.out.println("Zara's new balance: " +
          balance.get("Zara"));
       }
    }

    输出如下:

    %JAVA_HOME%injava "MyDemo" ...
    Qadir: -19.08
    Zara: 3434.34
    Mahnaz: 123.22
    Daisy: 99.22
    Ayan: 1378.0

    Zara's new balance: 4434.34

    二、Properties 继承于 Hashtable。表示一个持久的属性集.属性列表中每个键及其对应值都是一个字符串。

    Properties 类被许多 Java 类使用。例如,在获取环境变量时它就作为 System.getProperties() 方法的返回值。

    import java.util.*;
     
    public class MyDemo {
     
       public static void main(String args[]) {
          Properties capitals = new Properties();
          Set states;
          String str;
          
          capitals.put("Illinois", "Springfield");
          capitals.put("Missouri", "Jefferson City");
          capitals.put("Washington", "Olympia");
          capitals.put("California", "Sacramento");
          capitals.put("Indiana", "Indianapolis");
     
          // Show all states and capitals in hashtable.
          states = capitals.keySet(); // get set-view of keys
          Iterator itr = states.iterator();
          while(itr.hasNext()) {
             str = (String) itr.next();
             System.out.println("The capital of " +
                str + " is " + capitals.getProperty(str) + ".");
          }
          System.out.println();
     
          // look for state not in list -- specify default
          str = capitals.getProperty("Florida", "Not Found");
          System.out.println("The capital of Florida is "
              + str + ".");
       }
    }

    输出如下:

    %JAVA_HOME%injava "MyDemo" ...
    The capital of Missouri is Jefferson City.
    The capital of Illinois is Springfield.
    The capital of Indiana is Indianapolis.
    The capital of California is Sacramento.
    The capital of Washington is Olympia.

    The capital of Florida is Not Found.

    本文参考:

    https://www.runoob.com/java/java-properties-class.html

    https://www.runoob.com/java/java-hashTable-class.html

  • 相关阅读:
    win10 下安装mysql8.0 zip
    CentOS7 安装mysql8
    linux 下 oracle 安装
    springboot restful jsonp 跨域请求
    Springboot mybatis generate 自动生成实体类和Mapper
    Spring MVC — @RequestMapping原理讲解二
    Spring MVC — @RequestMapping原理讲解一
    Android--LinearLayout中的属性baselineAligned的使用[转]
    Android--CountDownTimer倒计时工具类
    Android UI--SwipeDismissListView_直接删除
  • 原文地址:https://www.cnblogs.com/nayitian/p/14903263.html
Copyright © 2011-2022 走看看