`
imaginecup
  • 浏览: 87419 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

如何判断两个类之间的差异

阅读更多
代码实现
package net.mindview.util;
import java.util.*;
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> superset,Set<T> subset){
		Set<T> result=new HashSet<T>(superset);
		result.removeAll(subset);
		return result;
	}
	public static <T> Set<T> complement(Set<T> a,Set<T> b){
		return difference(union(a,b),intersection(a,b));
	}

}

 

package net.mindview.util;
import java.lang.reflect.Method;
import java.util.*;
public class ContainerMethodDifferences {

	/**
	 * 打印出java.util包中各种Collection和Map类之间的方法差异
	 */
	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);
	}
	static Set<String> object=methodSet(Object.class);
	static{
		object.add("clone");
	}
	static void difference(Class<?> superset,Class<?> subset){
		System.out.println(superset.getSimpleName()+" extends "+subset.getSimpleName()+" ,adds:");
		Set<String> comp=Sets.difference(methodSet(superset), methodSet(subset));
		comp.removeAll(object);
		System.out.println(comp);
		interfaces(superset);
	}
	public static void main(String[] args) {
		System.out.println("Collection: "+methodSet(Collection.class));
		interfaces(Collection.class);
		difference(Set.class,Collection.class);
		difference(HashSet.class,Set.class);
		difference(LinkedHashSet.class,HashSet.class);
		difference(TreeSet.class,Set.class);
		difference(List.class,Collection.class);
		difference(ArrayList.class,List.class);
		difference(LinkedList.class,List.class);
		difference(Queue.class,Collection.class);
		difference(PriorityQueue.class,Queue.class);
		System.out.println("Map: "+methodSet(Map.class));
		difference(HashMap.class,Map.class);
		difference(LinkedHashMap.class,HashMap.class);
		difference(SortedMap.class,Map.class);
		difference(TreeMap.class,Map.class);
		

	}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics