java 泛型深入之Set实用工具 各种集合泛型深入使用示例,匿名内部类、内部类应用于泛型探讨

java 泛型深入之Set实用工具 各种集合泛型深入使用示例,匿名内部类、内部类应用于泛型探讨

//Sets.java
package org.rui.generics.set;

import java.util.HashSet;
import java.util.Set;
/**
 * 一个Set实用工具
 * @author lenovo
 *
 */
public class Sets {
	
	public static<T> Set<T> union(Set<T> a,Set<T> b)
	{
		Set<T> result=new HashSet<T>(a);
		result.addAll(b);
		return result;
	}
	
	//保留 相同
	public static <T> Set<T> intersection(Set<T> a,Set<T> b)
	{
		Set<T> result=new HashSet<T>(a);
		result.retainAll(b);
		return result;
	}
	// 差值 //去掉相同
	public static <T> Set<T> difference(Set<T> a,Set<T> b)
	{
		Set<T> result=new HashSet<T>(a);
		result.removeAll(b);
		return result;
	}
	
//除了交集之外的所有过犹元素
	public static <T> Set<T> complement(Set<T> a,Set<T> b)
	{
		return difference(union(a,b),intersection(a,b));
	}
	
	public static void main(String[] args) {
		Set<String> result=new HashSet<String>();
		result.add("a");
		result.add("b");
		Set<String> result2=new HashSet<String>();
		result2.add("b");
		
		Set<String> results=complement(result,result2);
		for(String s:results){
			System.out.println(s);
		}
	}
}
 

//Watercolors.java
package org.rui.generics.set;

//水彩画
public enum Watercolors {
	ZINC,LEMON_TYLLOW,MEDIUM_YELLOW,DEEP_YELLOW,ORANGE,
	BRILLIANT_RED

}

//WatercolorSets.java
package org.rui.generics.set;

import java.util.EnumSet;
import java.util.Set;
/**
 * EnumSet 使用示例
 * @author lenovo
 *
 */
public class WatercolorSets {
	
	public static void main(String[] args) {
		Set<Watercolors> set1=EnumSet.range(
				Watercolors.LEMON_TYLLOW,
				Watercolors.ORANGE
				);
		
		Set<Watercolors> set2=EnumSet.range(
				Watercolors.ZINC,
				Watercolors.MEDIUM_YELLOW);
		System.out.println(set1);
		System.out.println(set2);
		//union
		System.out.println(" union 1 2:" +Sets.union(set1, set2));
		//intersection
		System.out.println("intersection:"+Sets.intersection(set1, set2));
		//difference 差异
		System.out.println("difference1:"+Sets.difference(set1, set2));
		System.out.println("difference2:"+Sets.difference(set2, set1));
		
		//complement补足
		System.out.println("complement:"+Sets.complement(set1, set2));
		
		
	}

}

//Watercolors.java
package org.rui.generics.set;
import java.lang.reflect.Method;
import java.util.*;
/**
 * 我们可以从输出中看到各种关系运算的结果
 * 
 * @author lenovo
 *
 */
public class ContainerMethodDifferences {
	static Set<String> methodSet(Class<?> type)
	{
		Set<String> result =new TreeSet<String>();
		for(Method m:type.getMethods())
			result.add(m.getName());
		
		return result;
	}
	
	static void interfaces(Class<?> type)
	{
		System.out.println("interfaces in:"+type.getSimpleName());
		List<String> result=new ArrayList<String>();
		for(Class<?> c:type.getInterfaces())
			result.add(c.getSimpleName());
		System.out.println("result:"+result);
	}

	static Set<String> object=methodSet(Object.class);
	static{object.add("clone");}
	//difference
	static void difference(Class<?> superset,Class<?> subset)
	{
		System.out.println(superset.getSimpleName()+
				" extends:"+subset.getSimpleName());
		
		Set<String> comp=Sets.difference(
				methodSet(superset), methodSet(subset));
		
		comp.removeAll(object);
		System.out.println("object:"+comp);
		interfaces(superset);
	}
	
	
	//mian
	public static void main(String[] args) {
		/*System.out.println("collection:"+
				methodSet(Collection.class));
		
		interfaces(Collections.class);*/
		System.out.println("----Set-----------------------------");
		difference(Set.class,Collections.class);
		System.out.println("----HashSet-----------------------------");
		difference(HashSet.class,Set.class);
		System.out.println("----LinkedHashSet-----------------------------");
		difference(LinkedHashSet.class,HashSet.class);
		System.out.println("----TreeSet-----------------------------");
		difference(TreeSet.class,Set.class);
		System.out.println("-----List----------------------------");
		difference(List.class,Collection.class);
		System.out.println("------ArrayList---------------------------");
		difference(ArrayList.class,List.class);
		System.out.println("------LinkedList---------------------------");
		difference(LinkedList.class,List.class);
		System.out.println("------Queue---------------------------");
		difference(Queue.class,Collection.class);
		System.out.println("------PriorityQueue---------------------------");
		difference(PriorityQueue.class,Queue.class);
		
		System.out.println("Map:"+methodSet(Map.class));
		System.out.println("------HashMap---------------------------");
		difference(HashMap.class,Map.class);
		System.out.println("------LinkedHashMap---------------------------");
		difference(LinkedHashMap.class,HashMap.class);
		System.out.println("------TreeMap---------------------------");
		difference(TreeMap.class,Map.class);
		//分类
		System.out.println("------SortedMap---------------------------");
		difference(SortedMap.class,Map.class);
	}
	
}/*output:
----Set-----------------------------
Set extends:Collections
object:[toArray, iterator, remove, containsAll, contains, add, size, clear, isEmpty, retainAll, removeAll]
interfaces in:Set
result:[Collection]
----HashSet-----------------------------
HashSet extends:Set
object:[]
interfaces in:HashSet
result:[Set, Cloneable, Serializable]
----LinkedHashSet-----------------------------
LinkedHashSet extends:HashSet
object:[]
interfaces in:LinkedHashSet
result:[Set, Cloneable, Serializable]
----TreeSet-----------------------------
TreeSet extends:Set
object:[lower, last, higher, descendingIterator, subSet, pollLast, comparator, pollFirst, floor, headSet, ceiling, tailSet, first, descendingSet]
interfaces in:TreeSet
result:[NavigableSet, Cloneable, Serializable]
-----List----------------------------
List extends:Collection
object:[get, set, listIterator, lastIndexOf, indexOf, subList]
interfaces in:List
result:[Collection]
------ArrayList---------------------------
ArrayList extends:List
object:[trimToSize, ensureCapacity]
interfaces in:ArrayList
result:[List, RandomAccess, Cloneable, Serializable]
------LinkedList---------------------------
LinkedList extends:List
object:[offerFirst, removeFirstOccurrence, pop, peekLast, push, descendingIterator, poll, peek, removeFirst, pollLast, getFirst, offerLast, element, removeLast, offer, pollFirst, addLast, addFirst, peekFirst, getLast, removeLastOccurrence]
interfaces in:LinkedList
result:[List, Deque, Cloneable, Serializable]
------Queue---------------------------
Queue extends:Collection
object:[element, offer, poll, peek]
interfaces in:Queue
result:[Collection]
------PriorityQueue---------------------------
PriorityQueue extends:Queue
object:[comparator]
interfaces in:PriorityQueue
result:[Serializable]
Map:[clear, containsKey, containsValue, entrySet, equals, get, hashCode, isEmpty, keySet, put, putAll, remove, size, values]
------HashMap---------------------------
HashMap extends:Map
object:[]
interfaces in:HashMap
result:[Map, Cloneable, Serializable]
------LinkedHashMap---------------------------
LinkedHashMap extends:HashMap
object:[]
interfaces in:LinkedHashMap
result:[Map]
------TreeMap---------------------------
TreeMap extends:Map
object:[pollLastEntry, firstKey, floorEntry, ceilingEntry, lowerEntry, lastEntry, subMap, tailMap, navigableKeySet, higherEntry, lowerKey, headMap, firstEntry, comparator, descendingKeySet, descendingMap, pollFirstEntry, lastKey, higherKey, floorKey, ceilingKey]
interfaces in:TreeMap
result:[NavigableMap, Cloneable, Serializable]
------SortedMap---------------------------
SortedMap extends:Map
object:[tailMap, firstKey, headMap, comparator, lastKey, subMap]
interfaces in:SortedMap
result:[Map]
*/

---------------------------------------------------------------------------------------------------------------------------------------


//Generator.java
package org.rui.generics.anonymity;

public interface Generator<T> {

	//返回泛型的内型对象
    T next();
	
}

//Generators.java
package org.rui.generics.anonymity;

import java.util.Collection;
/**
 * 利用生成器很方便的填充一个collection
 * @author lenovo
 *
 */
public class Generators 
{
  public static<T> Collection<T> fill(Collection<T> coll,Generator<T> gen,int n)
  {
	  System.out.println("gen.next():"+gen.next());
	  for(int i=0;i<n;i++)
	  {
		  coll.add(gen.next());
	  }
	return coll;
	
  }
}

//BankTeller.java
package org.rui.generics.anonymity;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Random;
/**
 * 匿名内部类 内部类 就用于泛型
 *  generator 都生明成了static的,所以它们无法作为接口的一部分,
 *  因为无法用接口这种特定的惯用法来泛化这二者。
 *  尽管如此,它们fill()方法中都工作的很好
 *  
 * @author lenovo
 *
 */

class Customer
{
	private static long counter=1;
	private final long id=counter++;
	private Customer(){}
	public String toString(){return "Customer:"+id;}
	
	//每次会创建一个新的客户对象
	public static Generator<Customer> generator(){
		return new Generator<Customer>(){
			public Customer next(){return new Customer();}
		};
	}
	
}

//// 出纳员
 class Teller
 {
	 private static long counter=1;
	 private final long id=counter++;
	 private Teller(){}
	 public String toString(){return "Teller"+id;}
	 
	 //Teller就只创建了一个public 的generator对象
	 public static Generator<Teller> generator=new  Generator<Teller>()
			 {
				public Teller next() 
				{
					return new Teller();
				}
		 
			 };
 }

//////////////  出纳员
public class BankTeller {
	
	public static void serve(Teller t,Customer c)
	{
		System.out.println(t+" serves "+c);
	}
	
  public static void main(String[] args) 
  {
	Random random=new Random(47);
	//生成客户对象15个
	Queue<Customer> line=new LinkedList<Customer>();
	Generators.fill(line, Customer.generator(), 15);
	
	//出纳员对象4个 
	List<Teller> tellers=new ArrayList<Teller>();
	Generators.fill(tellers, Teller.generator, 4);
	
	for(Customer c:line)
		serve(tellers.get(random.nextInt(tellers.size())),c);
	
  }

}
	/*output:
	Teller3 serves Customer1
	Teller2 serves Customer2
	Teller3 serves Customer3
	Teller1 serves Customer4
	Teller1 serves Customer5
	Teller3 serves Customer6
	Teller1 serves Customer7
	Teller2 serves Customer8
	Teller3 serves Customer9
	Teller3 serves Customer10
	Teller2 serves Customer11
	Teller4 serves Customer12
	Teller2 serves Customer13
	Teller1 serves Customer14
	Teller1 serves Customer15
	 * *///:~~~


java 泛型深入之Set实用工具 各种集合泛型深入使用示例,匿名内部类、内部类应用于泛型探讨,布布扣,bubuko.com

java 泛型深入之Set实用工具 各种集合泛型深入使用示例,匿名内部类、内部类应用于泛型探讨

上一篇:JS-Math函数的常用方法


下一篇:网络爬虫(蜘蛛)Scrapy,Python安装!