码迷,mamicode.com
首页 > 编程语言 > 详细

java 容器深入研究之使用Abstract类定制

时间:2015-03-28 11:36:16      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:map   集合   java容器   

技术分享



package org.rui.collection3.map;

import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

/***
 * 集合
 * 使用Abstract类  
 * 
 * @author lenovo
 *
 */
public class Countries
{
	public static final String[][] DATA =
	{
			// Africa
			{ "A", "a" },
			{ "B", "b" },
			{ "C", "c" },
			{ "D", "d" },
			{ "E", "e" },
			{ "F", "f" },
			{ "G", "g" },
			{ "H", "h" },
			{ "I", "i" },
			{ "J", "j" },
			{ "K", "k" },
			{ "L", "l" },
			{ "M", "m" },
			{ "N", "n" },
			{ "O", "o" },
			{ "P", "p" },
			{ "Q", "q" } };

	private static class FlyweightMap extends AbstractMap<String, String>
	{

		private static class Entry implements Map.Entry<String, String>
		{
			int index;

			Entry(int index)
			{
				this.index = index;
			}

			@Override
			public String getKey()
			{
				return DATA[index][0];
			}

			@Override
			public String getValue()
			{
				return DATA[index][1];
			}

			@Override
			public String setValue(String value)
			{
				throw new UnsupportedOperationException();
			}

			public int hashCode()
			{
				return index;
			}

			public boolean equals(Object o)
			{
				return DATA[index][0].equals(o);
			}
		}

		// use abstractset by implementing size & iterator 通过实现尺寸& iterato使用套抽象
		static class EnterSet extends AbstractSet<Map.Entry<String, String>>
		{
			private int size;

			EnterSet(int size) // 享元部分 只存储它的索引 而不是实际的健和值
			{
				if (size < 0)
				{
					this.size = 0;
				} else if (size > DATA.length)
				{
					this.size = DATA.length;
				} else
				{
					this.size = size;
				}
			}

			@Override
			public int size()
			{
				return size;
			}

			private class Iter implements Iterator<Map.Entry<String, String>>
			{
				// 每个迭代器只有一个条目对象
				private Entry entry = new Entry(-1);

				@Override
				public boolean hasNext()
				{
					return entry.index < size - 1;
				}

				@Override
				public java.util.Map.Entry<String, String> next()
				{
					entry.index++;
					return entry;
				}

				@Override
				public void remove()
				{
					throw new UnsupportedOperationException();

				}
			}

			@Override
			public Iterator<java.util.Map.Entry<String, String>> iterator()
			{

				return new Iter();
			}

		}

		//
		public static Set<java.util.Map.Entry<String, String>> entries = new EnterSet(
				DATA.length);

		@Override
		public Set<java.util.Map.Entry<String, String>> entrySet()
		{

			return entries;
		}

	}

	// create a partial map of size countries 创建一个部分大小国家的地图
	static Map<String, String> select(final int size)
	{
		return new FlyweightMap()
		{
			@Override
			public Set<java.util.Map.Entry<String, String>> entrySet()
			{

				return new FlyweightMap.EnterSet(size);
			}
		};
	}

	static Map<String, String> map = new FlyweightMap();

	public static Map<String, String> capitals()
	{
		return map;
	}

	public static Map<String, String> capitals(int size)
	{
		return select(size);// a partial map
	}

	static List<String> names = new ArrayList<String>(map.keySet());

	// all the names;
	public static List<String> names()
	{
		return names;
	}

	// A partial list
	public static List<String> names(int size)
	{
		return new ArrayList<String>(select(size).keySet());
	}

	/********************main******************************/
	public static void main(String[] args)
	{
		System.out.println(capitals(10));
		System.out.println(names(10));
		System.out.println(new HashMap<String, String>(capitals(3)));
		System.out.println(new TreeMap<String, String>(capitals(3)));
		System.out.println(new LinkedHashMap<String, String>(capitals(3)));
		System.out.println(new Hashtable<String, String>(capitals(3)));

		System.out.println(new HashSet<String>(names(6)));
		System.out.println(new TreeSet<String>(capitals(6).keySet()));
		System.out.println(new LinkedHashSet<String>(names(6)));
		// System.out.println(new Hashtable<String, String>(capitals(3)));

		System.out.println(new ArrayList<String>(names(6)));
		System.out.println(new LinkedList<String>(names(6)));

		System.out.println(capitals().get("F"));

	}

}
/**
output:
{A=a, B=b, C=c, D=d, E=e, F=f, G=g, H=h, I=i, J=j}
[A, B, C, D, E, F, G, H, I, J]
{A=a, B=b, C=c}
{A=a, B=b, C=c}
{A=a, B=b, C=c}
{A=a, C=c, B=b}
[D, E, F, A, B, C]
[A, B, C, D, E, F]
[A, B, C, D, E, F]
[A, B, C, D, E, F]
[A, B, C, D, E, F]
f
*/



技术分享

package org.rui.collection3.map;

import java.util.AbstractList;

/***
 * 它可以具有任意尺寸,并且用Integer数据 有效的 进行了预初始化
 * @author lenovo
 *
 */
public class CountingIntegerList extends AbstractList<Integer>
{
	private int size;
	
	public CountingIntegerList(int size){
		this.size=size<0?0:size;
	}
	

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

	@Override
	public int size()
	{
		return size;
	}
	
	public static void main(String[] args)
	{
		System.out.println(new CountingIntegerList(30));
	}

}
/**output:
 * [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
 *  16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]

 * */


技术分享

package org.rui.collection3.map;

import java.util.AbstractMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
/**
 * 这里使用的是LinkedHashSet,而不是定制的Set类,因此享元未完全实现
 * @author lenovo
 *
 */
public class CountingMapData extends AbstractMap<Integer, String>
{

	private int size;
	private static String[] chars = "A B C D E F G".split(" ");

	public CountingMapData(int size)
	{
		if (size < 0)
			this.size = 0;
		this.size = size;
	}

	private static class Entry implements Map.Entry<Integer, String>
	{

		private int index;

		Entry(int index)
		{
			this.index = index;
		}

		@Override
		public Integer getKey()
		{
			return index;
		}

		@Override
		public String getValue()
		{
			return chars[index % chars.length]
					+ Integer.toString(index / chars.length);
		}

		@Override
		public String setValue(String value)
		{
			throw new UnsupportedOperationException();
		}

		@Override
		public boolean equals(Object obj)
		{
			return Integer.valueOf(index).equals(obj);
		}

		@Override
		public int hashCode()
		{

			return Integer.valueOf(index).hashCode();
		}
	}

	@Override
	public Set<java.util.Map.Entry<Integer, String>> entrySet()
	{
		// linedhashset 保留初始化顺序
		Set<Map.Entry<Integer, String>> entries = new LinkedHashSet<Map.Entry<Integer, String>>();
		for (int i = 0; i < size; i++)
		{
			entries.add(new Entry(i));
		}

		return entries;
	}

	public static void main(String[] args)
	{
		System.out.println(new CountingMapData(60));
	}

}
/**
 * {0=A0, 1=B0, 2=C0, 3=D0, 4=E0, 5=F0, 6=G0, 7=A1, 
 * 8=B1, 9=C1, 10=D1, 11=E1, 12=F1, 13=G1, 14=A2, 15=B2, 
 * 16=C2, 17=D2, 18=E2, 19=F2, 20=G2, 21=A3, 22=B3, 23=C3,
 *  24=D3, 25=E3, 26=F3, 27=G3, 28=A4, 29=B4, 30=C4, 31=D4, 
 *  32=E4, 33=F4, 34=G4, 35=A5, 36=B5, 37=C5, 38=D5, 39=E5,
 *   40=F5, 41=G5, 42=A6, 43=B6, 44=C6, 45=D6, 46=E6, 47=F6, 
 *   48=G6, 49=A7, 50=B7, 51=C7, 52=D7, 53=E7, 54=F7, 55=G7, 
 *   56=A8, 57=B8, 58=C8, 59=D8}

 * */


java 容器深入研究之使用Abstract类定制

标签:map   集合   java容器   

原文地址:http://blog.csdn.net/liangrui1988/article/details/44699477

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!