17.2.3使用抽象类
AbstractMap
注意:有哪些操作会对集合进行了遍历,即使遍历过了,map中的数据是否已经完成了初始化?并不提供修改的操作。创建只读map的操作。
public class Countries {public static final String[][] DATA = {// Asia 部分国家以及首都 数据源{"CHINA", "Beijing"}, {"CYPRUS", "Nicosia"},{"INDIA", "New Delhi"}, {"INDONESIA", "Jakarta"},{"IRAN", "Tehran"}, {"IRAQ", "Baghdad"},{"ISRAEL", "Jerusalem"}, {"JAPAN", "Tokyo"},{"JORDAN", "Amman"}, {"KUWAIT", "Kuwait City"},{"LAOS", "Vientiane"}, {"LEBANON", "Beirut"},};private static class FlyweightMap extends AbstractMap<String, String> {//构建自定义Map 2,包含Entry 元组 提供对外的getKey getValueprivate static class Entry implements Map.Entry<String, String> {int index;Entry(int index) {this.index = index;}public boolean equals(Object o) {return DATA[index][0].equals(o);}//迭代的时候 对外暴漏数据源public String getKey() {return DATA[index][0];}//迭代的时候 对外暴漏数据源public String getValue() {return DATA[index][1];}public String setValue(String value) {throw new UnsupportedOperationException();}public int hashCode() {return DATA[index][0].hashCode();}}// Use AbstractSet by implementing size() & iterator()//3,EntrySet:元组的Set集合 提供对外遍历的能力static class EntrySet extends AbstractSet<Map.Entry<String, String>> {private int size;EntrySet(int size) {if (size < 0)this.size = 0;// Can't be any bigger than the array:else if (size > DATA.length)this.size = DATA.length;elsethis.size = size;}public int size() {return size;}//提供的遍历的能力private class Iter implements Iterator<Map.Entry<String, String>> {// Only one Entry object per Iterator:private Entry entry = new Entry(-1);public boolean hasNext() {return entry.index < size - 1;}public Map.Entry<String, String> next() {entry.index++;return entry;}public void remove() {throw new UnsupportedOperationException();}}//EntrySet 对外提供迭代器public Iterator<Map.Entry<String, String>> iterator() {return new Iter();}}//初始化动作1-1 执行FlyweightMap中的静态域private static Set<Map.Entry<String, String>> entries = new EntrySet(DATA.length);public Set<Map.Entry<String, String>> entrySet() {return entries;}};static Map<String, String> select(final int size) {return new FlyweightMap() {//1,匿名内部类重写 entrySet(),提供了自定义的EntrySet的实现。public Set<Map.Entry<String, String>> entrySet() {return new EntrySet(size);}};}//初始化动作1static Map<String, String> map = new FlyweightMap();//静态方法的调用 会触发静态域的初始化public static Map<String, String> capitals() {return map; // The entire map}public static Map<String, String> capitals(int size) {return select(size); // A partial map}public static void main(String[] args) {// 触发条件 调用了Countries的静态方法 ,将按照变量声明的顺序执行初始化操作// 执行完初始化动作发现capitals中的数据已经全部为二维数组中的最后一项Map<String, String> capitals = capitals();System.out.println();//在对map集合进行get的时候 调用的是抽象类中的方法 然后执行的迭代。//具体的迭代器以及entryset属于FlyweightMap内部类提供的,String s = capitals.get("CHINA");System.out.println(s);//在不输出capitals的前提下,查看capitals中的值 发现并没有进行初始化System.out.println();}}
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();
}
}
