17.2.3使用抽象类

AbstractMap

注意:有哪些操作会对集合进行了遍历,即使遍历过了,map中的数据是否已经完成了初始化?并不提供修改的操作。创建只读map的操作。

  1. public class Countries {
  2. public static final String[][] DATA = {
  3. // Asia 部分国家以及首都 数据源
  4. {"CHINA", "Beijing"}, {"CYPRUS", "Nicosia"},
  5. {"INDIA", "New Delhi"}, {"INDONESIA", "Jakarta"},
  6. {"IRAN", "Tehran"}, {"IRAQ", "Baghdad"},
  7. {"ISRAEL", "Jerusalem"}, {"JAPAN", "Tokyo"},
  8. {"JORDAN", "Amman"}, {"KUWAIT", "Kuwait City"},
  9. {"LAOS", "Vientiane"}, {"LEBANON", "Beirut"},
  10. };
  11. private static class FlyweightMap extends AbstractMap<String, String> {
  12. //构建自定义Map 2,包含Entry 元组 提供对外的getKey getValue
  13. private static class Entry implements Map.Entry<String, String> {
  14. int index;
  15. Entry(int index) {
  16. this.index = index;
  17. }
  18. public boolean equals(Object o) {
  19. return DATA[index][0].equals(o);
  20. }
  21. //迭代的时候 对外暴漏数据源
  22. public String getKey() {
  23. return DATA[index][0];
  24. }
  25. //迭代的时候 对外暴漏数据源
  26. public String getValue() {
  27. return DATA[index][1];
  28. }
  29. public String setValue(String value) {
  30. throw new UnsupportedOperationException();
  31. }
  32. public int hashCode() {
  33. return DATA[index][0].hashCode();
  34. }
  35. }
  36. // Use AbstractSet by implementing size() & iterator()
  37. //3,EntrySet:元组的Set集合 提供对外遍历的能力
  38. static class EntrySet extends AbstractSet<Map.Entry<String, String>> {
  39. private int size;
  40. EntrySet(int size) {
  41. if (size < 0)
  42. this.size = 0;
  43. // Can't be any bigger than the array:
  44. else if (size > DATA.length)
  45. this.size = DATA.length;
  46. else
  47. this.size = size;
  48. }
  49. public int size() {
  50. return size;
  51. }
  52. //提供的遍历的能力
  53. private class Iter implements Iterator<Map.Entry<String, String>> {
  54. // Only one Entry object per Iterator:
  55. private Entry entry = new Entry(-1);
  56. public boolean hasNext() {
  57. return entry.index < size - 1;
  58. }
  59. public Map.Entry<String, String> next() {
  60. entry.index++;
  61. return entry;
  62. }
  63. public void remove() {
  64. throw new UnsupportedOperationException();
  65. }
  66. }
  67. //EntrySet 对外提供迭代器
  68. public Iterator<Map.Entry<String, String>> iterator() {
  69. return new Iter();
  70. }
  71. }
  72. //初始化动作1-1 执行FlyweightMap中的静态域
  73. private static Set<Map.Entry<String, String>> entries = new EntrySet(DATA.length);
  74. public Set<Map.Entry<String, String>> entrySet() {
  75. return entries;
  76. }
  77. };
  78. static Map<String, String> select(final int size) {
  79. return new FlyweightMap() {
  80. //1,匿名内部类重写 entrySet(),提供了自定义的EntrySet的实现。
  81. public Set<Map.Entry<String, String>> entrySet() {
  82. return new EntrySet(size);
  83. }
  84. };
  85. }
  86. //初始化动作1
  87. static Map<String, String> map = new FlyweightMap();
  88. //静态方法的调用 会触发静态域的初始化
  89. public static Map<String, String> capitals() {
  90. return map; // The entire map
  91. }
  92. public static Map<String, String> capitals(int size) {
  93. return select(size); // A partial map
  94. }
  95. public static void main(String[] args) {
  96. // 触发条件 调用了Countries的静态方法 ,将按照变量声明的顺序执行初始化操作
  97. // 执行完初始化动作发现capitals中的数据已经全部为二维数组中的最后一项
  98. Map<String, String> capitals = capitals();
  99. System.out.println();
  100. //在对map集合进行get的时候 调用的是抽象类中的方法 然后执行的迭代。
  101. //具体的迭代器以及entryset属于FlyweightMap内部类提供的,
  102. String s = capitals.get("CHINA");
  103. System.out.println(s);
  104. //在不输出capitals的前提下,查看capitals中的值 发现并没有进行初始化
  105. System.out.println();
  106. }
  107. }

AbstractList

创建只读的List,并提供数据源,对于基本类型的数据并不需要提供

Integer示例
会进行自增操作,should 由JVM实现


import java.util.*;

public class CountingIntegerList extends AbstractList<Integer> {
    private int size;

    public CountingIntegerList(int size) {
        this.size = size < 0 ? 0 : size;
    }

    public Integer get(int index) {
        return Integer.valueOf(index);
    }

    public int size() {
        return size;
    }

    public static void main(String[] args) {
        CountingIntegerList integerList = new CountingIntegerList(6);
        Integer integer = integerList.get(5);
        System.out.println();
        //并不支持修改
        integerList.set(0,99);
        System.out.println(integerList);
    }
}

Character示例
会进行自增操作,should 由JVM实现

public class CharacterArrayList extends AbstractList<Character> {
    int size;

    int index;

    public CharacterArrayList(int size) {
        this.size = size < 0 ? 0 : size;
    }

    public Character get(int index) {
        return Character.valueOf((char)(index+97));
    }

    public int size() {
        return this.size;
    }

    public static void main(String[] args) {
        CharacterArrayList stringList = new CharacterArrayList(3);
        System.out.println();
    }
}

String示例
会进行自增操作,should 由JVM实现

ublic class StringArrayList extends AbstractList<String> {
    //固定的数据源
    String[] array = {"one", "two", "three", "four", "five"};
    int size;

    int index;

    public StringArrayList(int size) {
        this.size = size < 0 ? 0 : size;
    }

    public String get(int index) {
        return array[index];
    }

    public int size() {
        return this.size;
    }

    public static void main(String[] args) {
        StringArrayList stringList = new StringArrayList(3);
        System.out.println();
    }
}