当前位置:七道奇文章资讯编程技术Java编程
日期:2011-03-22 16:17:00  来源:本站整理

操纵Maps[Java编程]

赞助商链接



  本文“操纵Maps[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

Map(接口) 保持“键-值”对应关系(对),以便通过一个键查找呼应的值
HashMap* 基于一个散列表实现(用它替换Hashtable).针对“键-值”对的插入和检索,这种情势具有最安定的性能.可通过构建器对这一性能举行调整,以便设置散列表的“本领”和“装载因子”
ArrayMap 由一个ArrayList后推得到的Map.对反复的次序供应了切确的掌握.面向非常小的Map计划,分外是那些需求常常成立和删除的.关于非常小的Map,成立和反复所付出的代价要比HashMap低得多.但在Map变大今后,性能也会呼应地大幅度降低
TreeMap 在一个“红-黑”树的底子上实现.查看键大概“键-值”对时,它们会按固定的次序布列(取决于Comparable或Comparator,稍后即会讲到).TreeMap最大的好处就是我们得到的是已排好序的后果.TreeMap是含有subMap()办法的唯一一种Map,操纵它可以返回树的一部份.

Map (interface)

Maintains key-value associations (pairs), so you can look up a value using a key.

HashMap*

Implementation based on a hash table. (Use this instead of Hashtable.) Provides constant-time performance for inserting and locating pairs. Performance can be adjusted via constructors that allow you to set the capacity and load factor of the hash table.

TreeMap

Implementation based on a red-black tree. When you view the keys or the pairs, they will be in sorted order (determined by Comparable or Comparator, discussed later). The point of a TreeMap is that you get the results in sorted order. TreeMap is the only Map with the subMap() method, which allows you to return a portion of the tree.

下例包含了两套测试数据以及一个fill()办法,操纵该办法可以用任何两维数组(由Object构成)填充当何Map.这些工具也会在其他Map例子顶用到.

//: Map1.java
// Things you can do with Maps
package c08.newcollections;
import java.util.*;

public class Map1 {
  public final static String[][] testData1 = {
    { "Happy", "Cheerful disposition" },
    { "Sleepy", "Prefers dark, quiet places" },
    { "Grumpy", "Needs to work on attitude" },
    { "Doc", "Fantasizes about advanced degree"},
    { "Dopey", "'A' for effort" },
    { "Sneezy", "Struggles with allergies" },
    { "Bashful", "Needs self-esteem workshop"},
  };
  public final static String[][] testData2 = {
    { "Belligerent", "Disruptive influence" },
    { "Lazy", "Motivational problems" },
    { "Comatose", "Excellent behavior" }
  };
  public static Map fill(Map m, Object[][] o) {
    for(int i = 0; i < o.length; i++)
      m.put(o[i][0], o[i][1]);
    return m;
  }
  // Producing a Set of the keys:
  public static void printKeys(Map m) {
    System.out.print("Size = " + m.size() +", ");
    System.out.print("Keys: ");
    Collection1.print(m.keySet());
  }
  // Producing a Collection of the values:
  public static void printValues(Map m) {
    System.out.print("Values: ");
    Collection1.print(m.values());
  }
  // Iterating through Map.Entry objects (pairs):
  public static void print(Map m) {
    Collection entries = m.entries();
    Iterator it = entries.iterator();
    while(it.hasNext()) {
      Map.Entry e = (Map.Entry)it.next();
      System.out.println("Key = " + e.getKey() +
        ", Value = " + e.getValue());
    }
  }
  public static void test(Map m) {
    fill(m, testData1);
    // Map has 'Set' behavior for keys:
    fill(m, testData1);
    printKeys(m);
    printValues(m);
    print(m);
    String key = testData1[4][0];
    String value = testData1[4][1];
    System.out.println("m.containsKey(\"" + key +
      "\"): " + m.containsKey(key));
    System.out.println("m.get(\"" + key + "\"): "
      + m.get(key));
    System.out.println("m.containsValue(\"" 
      + value + "\"): " + 
      m.containsValue(value)); 
    Map m2 = fill(new TreeMap(), testData2);
    m.putAll(m2);
    printKeys(m);
    m.remove(testData2[0][0]);
    printKeys(m);
    m.clear();
    System.out.println("m.isEmpty(): " 
      + m.isEmpty());
    fill(m, testData1);
    // Operations on the Set change the Map:
    m.keySet().removeAll(m.keySet());
    System.out.println("m.isEmpty(): " 
      + m.isEmpty());
  }
  public static void main(String args[]) {
    System.out.println("Testing HashMap");
    test(new HashMap());
    System.out.println("Testing TreeMap");
    test(new TreeMap());
  }
} ///:~
printKeys(),printValues()以及print()办法并不只是有效的工具,它们也清楚地揭露了一个Map的Collection“气象”的产生历程.keySet()办法会产生一个Set,它由Map中的键后推得来.在这儿,它只被当作一个Collection对待.values()也得到了近似的对待,它的作用是产生一个List,此中包含了Map中的全部值(注意键必须是举世无双的,而值可以有反复).由于这些Collection是由Map后推得到的,所以一个Collection中的任何改变城市在呼应的Map中反映出来.
print()办法的作用是汇集由entries产生的Iterator(反复器),并用它同时打印出每个“键-值”对的键和值.程序剩余的部份供应了每种Map操作的简单示例,并对每种范例的Map举行了测试.
当成立自己的类,将其作为Map中的一个键利用时,必须注意到和从前的Set相同的问题.
  以上是“操纵Maps[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • 操纵Maps
  • Struts1.x系列教程(21):操纵MappingDispatchAction类调用差别
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .