package cn.tedu.struect.generic;

import java.util.Comparator;

public class Demo02_genericLimit {
	public static void main(String[] args) {
		
		String[] arr = {
			"cZaZEBRA",
			"alligator",
			"crocodile"
		};
		System.out.println(
				GenericMemoryCell.findMax(
						arr, 
						new Comparator<String>() {
							@Override
							public int compare(String o1, String o2) {
								return o1.compareToIgnoreCase(o2);
							}
							}
						)
				);
		
	}
}

class CaseInsensitiveCompare implements Comparator<String>{

	@Override
	public int compare(String o1, String o2) {
		// TODO Auto-generated method stub
		return o1.compareToIgnoreCase(o2);
	}
	
}
package cn.tedu.struect.generic;

import java.util.Comparator;

public class GenericMemoryCell <AnyType> {
	private AnyType storedValue;
	public AnyType read() {
		return storedValue; 
	};  
	public void write(AnyType x) {
		storedValue = x ;
	}
	//泛型方法 static 
	public static <AnyType> boolean contains(AnyType[] arr , AnyType x ) { //泛型方法中的类型参数位于返回类型之前
		for(AnyType val : arr) {
			if(x.equals(val)) {
				return true ;
			}
		}
		return false ;
	}
	/* 星期1-4 8.45~8:30 星期5 8.45~5:30 星期5 8.45~5:30(自习) */
	/** * 静态方法调用 * AnyType IS-A ?,that super AnyType and IS-A Comparable * AnyType 是一个父类实现了Comparable接口的类 * @param arr * @return */
	public static <AnyType extends Comparable<? super AnyType>> AnyType findMax(AnyType[] arr) {
		int maxIndex =0 ; 
		for(int i=1 ; i<arr.length ; i++) {
			if(arr[i].compareTo(arr[maxIndex])>0) {
				maxIndex = i ; 
			}
		}
		return arr[maxIndex] ; 
	}
	
	/** * 使用FunctionInterface(功能接口,函数对象) * @param arr * @param cmp * @return */
	public static <AT> AT findMax(AT[] arr , Comparator<? super AT> cmp ) {
		int maxIndex = 0 ; 
		
		for(int i=1 ; i<arr.length ; i++) {
			if(cmp.compare(arr[maxIndex], arr[i]) < 0) {
				maxIndex = i ;
			}
		}
		
		return arr[maxIndex] ;
	}
}