国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

使用Maps

2019-11-18 13:22:33
字體:
供稿:網(wǎng)友

  
使用Maps


  Map(接口) 維持“鍵-值”對(duì)應(yīng)關(guān)系(對(duì)),以便通過一個(gè)鍵查找相應(yīng)的值
  HashMap* 基于一個(gè)散列表實(shí)現(xiàn)(用它代替Hashtable)。針對(duì)“鍵-值”對(duì)的插入和檢索,這種形式具有最穩(wěn)定的性能。可通過構(gòu)建器對(duì)這一性能進(jìn)行調(diào)整,以便設(shè)置散列表的“能力”和“裝載因子”
  ArrayMap 由一個(gè)ArrayList后推得到的Map。對(duì)反復(fù)的順序提供了精確的控制。面向非常小的Map設(shè)計(jì),非凡是那些需要經(jīng)常創(chuàng)建和刪除的。對(duì)于非常小的Map,創(chuàng)建和反復(fù)所付出的代價(jià)要比HashMap低得多。但在Map變大以后,性能也會(huì)相應(yīng)地大幅度降低
  TreeMap 在一個(gè)“紅-黑”樹的基礎(chǔ)上實(shí)現(xiàn)。查看鍵或者“鍵-值”對(duì)時(shí),它們會(huì)按固定的順序排列(取決于Comparable或Comparator,稍后即會(huì)講到)。TreeMap最大的好處就是我們得到的是已排好序的結(jié)果。TreeMap是含有subMap()方法的唯一一種Map,利用它可以返回樹的一部分
  
  下例包含了兩套測試數(shù)據(jù)以及一個(gè)fill()方法,利用該方法可以用任何兩維數(shù)組(由Object構(gòu)成)填充任何Map。這些工具也會(huì)在其他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()方法并不只是有用的工具,它們也清楚地揭示了一個(gè)Map的Collection“景象”的產(chǎn)生過程。keySet()方法會(huì)產(chǎn)生一個(gè)Set,它由Map中的鍵后推得來。在這兒,它只被當(dāng)作一個(gè)Collection對(duì)待。values()也得到了類似的對(duì)待,它的作用是產(chǎn)生一個(gè)List,其中包含了Map中的所有值(注重鍵必須是獨(dú)一無二的,而值可以有重復(fù))。由于這些Collection是由Map后推得到的,所以一個(gè)Collection中的任何改變都會(huì)在相應(yīng)的Map中反映出來。
  print()方法的作用是收集由entries產(chǎn)生的Iterator(反復(fù)器),并用它同時(shí)打印出每個(gè)“鍵-值”對(duì)的鍵和值。程序剩余的部分提供了每種Map操作的簡單示例,并對(duì)每種類型的Map進(jìn)行了測試。
  當(dāng)創(chuàng)建自己的類,將其作為Map中的一個(gè)鍵使用時(shí),必須注重到和以前的Set相同的問題。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 大理市| 曲阜市| 连城县| 柳州市| 泰顺县| 登封市| 怀柔区| 茂名市| 剑川县| 浮梁县| 临武县| 花垣县| 芜湖市| 平山县| 微山县| 河东区| 清涧县| 靖州| 萨嘎县| 专栏| 阳春市| 攀枝花市| 绍兴县| 腾冲县| 左贡县| 九龙城区| 綦江县| 确山县| 三江| 汝阳县| 韩城市| 肇庆市| 泰和县| 平湖市| 大名县| 临泽县| 上林县| 囊谦县| 长丰县| 普宁市| 磐安县|