性能文章>【译】JAVA11功能简单解释>

【译】JAVA11功能简单解释转载

2年前
317946
public class Test {

	public static void main(String[] args) {

		List<String> sampleList = Arrays.asList("LoveToLearn", "Anil");

		String[] sampleArray = sampleList.toArray(String[]::new);

		System.out.println(Arrays.toString(sampleArray));

	}

}
 
public class Repeat {

	public static void main(String[] args) {

	   String s = "java11";
	   
	   String str = s.repeat(10);
		
	   System.out.println(str);
		
	}

}
 
public class Lines {

	public static void main(String[] args) {

		String str = "JD\nJD\nJD"; 
        System.out.println(str);
        System.out.println(str.lines().collect(Collectors.toList()));
		
	}

}
 
public class Blank {

	public static void main(String[] args) {

		String str = "";
		String str1 = " ";
        System.out.println(str.isBlank());
        System.out.println(str1.isBlank());
		
	}

}
 
public class Check {

	public static void main(String[] args) {

		String str = " java ";		
		System.out.println("before using strip-leading method");
		System.out.print("+");
        System.out.print(str);
    	System.out.println("+");
        System.out.println("after using strip-leading method");
        System.out.print("+");
        System.out.print(str.stripLeading());
    	System.out.print("+");
        	
	}
}
 
public class Check {

	public static void main(String[] args) {

		String str = " java ";		
		System.out.println("before using strip-trailing method");
		System.out.print("+");
        System.out.print(str);
    	System.out.println("+");
        System.out.println("after using strip-trailing method");
        System.out.print("+");
        System.out.print(str.stripTrailing());
    	System.out.print("+");
        	
	}
}
 
public class StripCheck {

	public static void main(String[] args) {

		String str = " java ";		
		System.out.println("before using strip-trailing method");
		System.out.print("+");
        System.out.print(str);
    	System.out.println("+");
        System.out.println("after using strip-trailing method");
        System.out.print("+");
        System.out.print(str.strip());
    	System.out.print("+");
        	
	}
}
 

(var s1,s2)-> s1 + s2:

interface Add{
	int sum(int a, int b);
}

public class Check {

	public static void main(String[] args) {

		//before java11 only below two are possible
			 Add Add = ( a, b)-> a+b;		
			 int result = Add.sum(10, 20);
			 System.out.println(result);
			 
			 Add Add1 = (int a, int b)-> a+b;		
			 int result1 = Add.sum(10, 20);
			 System.out.println(result1);
		
	    //after java11 we can also  var 
			 Add Add2 = (var a, var b)-> a+b;		
			 int result2 = Add.sum(10, 20);
			 System.out.println(result2);
		        	
			 
	}
}

(var s1,字符串y)-> s1 + y //不允许混合:

切勿将var与变量类型混合,如果你将其混合,将获得以下错误:

var s1 -> s1 //不允许。如果在lambda中使用var,则需要括号

Files.writeString(Path.of(“D:\test.txt”), “AnilAppana”)
String s1 = Files.readString(Path.of("D:\\test.txt"));
Files.isSameFile(Path.of(“example1.txt”), Path.of(“example1.txt”));
var str = Pattern.compile("aba").asMatchPredicate();
System.out.println(str.test("aba"));
//it returns true if pattern matched else it returns false
Output: true
Optional str = Optional.empty();
System.out.println(str.isEmpty());
Output: true
TimeUnit c1 = TimeUnit.MINUTES;
System.out.println(c1.convert(Duration.ofDays(3)));
Output:4320
点赞收藏
分类:标签:
Pyqdzzj

怀抱着空杯心态,接纳、学习新技术、新领域!

请先登录,查看4条精彩评论吧
快去登录吧,你将获得
  • 浏览更多精彩评论
  • 和开发者讨论交流,共同进步

为你推荐

随机一门技术分享之Netty

随机一门技术分享之Netty

MappedByteBuffer VS FileChannel:从内核层面对比两者的性能差异

MappedByteBuffer VS FileChannel:从内核层面对比两者的性能差异

6
4