java8之工具类边角料工具

public class OtherTools {

	// java8支持Nashorn JavaScript引擎
	// java8程序中执行js脚本
	@Test
	public void test1() throws Exception {
		ScriptEngineManager manager = new ScriptEngineManager();
		ScriptEngine engine = manager.getEngineByName("JavaScript");

		System.out.println(engine.getClass().getName());
		System.out.println("Result:"
				+ engine.eval("function f() { return 1; }; f() + 1;"));
	}

	// java8支持base64编码
	// 今后使用base64编码不需要引用第三方包了
	@Test
	public void test2() {
		final String text = "java8 support base64";

		final String encoded = Base64.getEncoder().encodeToString(
				text.getBytes(StandardCharsets.UTF_8));
		System.out.println(encoded);

		final String decoded = new String(Base64.getDecoder().decode(encoded),
				StandardCharsets.UTF_8);
		System.out.println(decoded);
	}

	// 并行数组
	// parallelSort可以加速数组排序
	@Test
	public void test3() {
		long[] arrayOfLong = new long[20000];

		Arrays.parallelSetAll(arrayOfLong, index -> ThreadLocalRandom.current()
				.nextInt(1000000));
		Arrays.stream(arrayOfLong).limit(10)
				.forEach(i -> System.out.print(i + " "));
		System.out.println();

		Arrays.parallelSort(arrayOfLong);
		Arrays.stream(arrayOfLong).limit(10)
				.forEach(i -> System.out.print(i + " "));
		System.out.println();
	}

}

你可能感兴趣的:(java8)