Lambda表达式中类型推断

   Lambda表达式中类型推断,是java7中引入目标类型推断的拓展。

   java7中的菱形操作符,使javac推断处反省参数的类型。

	/**
	 * lambda表达式中类型推断
	 */
	public static void targetType() {
		// new HashMap<String, Integer>指定泛型类型
		Map<String, Integer> wordcounts = new HashMap<String, Integer>();
		// 使用菱形操作符<>,new HashMap<>不用明确泛型类型,编译器就可以推断出。
		Map<String, Integer> diamond = new HashMap<>();
		// 传递方法参数,根据形式参数做类型推断
		targerTpyeMethod(new HashMap());
		
		// lambda表达式类型推断,函数接口必须指明泛型类型,其他类型可以不用指明,可以进行推断。
		Predicate<Integer> atLeast = x -> x > 5;
		
		// The operator > is undefined for the argument type(s) Object, int
		Predicate wrongLeast1 = x -> x > 5;
		// Lambda expression's parameter x is expected to be of type Object
		Predicate wrongLeast2 = (Integer x) -> x > 5;
		
		
	}
	
	private static void targerTpyeMethod(Map<String, String> values){
		
	}
  

猜你喜欢

转载自blog.csdn.net/neutron129/article/details/51478755