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

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

3周前
219646
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条精彩评论吧
快去登录吧,你将获得
  • 浏览更多精彩评论
  • 和开发者讨论交流,共同进步

为你推荐

讲透JAVA Stream的collect用法与原理,远比你想象的更强大

讲透JAVA Stream的collect用法与原理,远比你想象的更强大

没有二十年功力,写不出Thread.sleep(0)这一行“看似无用”的代码!

没有二十年功力,写不出Thread.sleep(0)这一行“看似无用”的代码!

填坑来了!关于“Thread.sleep(0)这一行‘看似无用’的代码”里面留下的坑。

填坑来了!关于“Thread.sleep(0)这一行‘看似无用’的代码”里面留下的坑。

【译】记一次数据库连接泄漏导致的响应迟缓

【译】记一次数据库连接泄漏导致的响应迟缓

【全网首发】微服务10:系统服务熔断、限流

【全网首发】微服务10:系统服务熔断、限流

【全网首发】MQ-消息堆积-JDK Bug导致线程阻塞案例分析

【全网首发】MQ-消息堆积-JDK Bug导致线程阻塞案例分析

6
4