java基础---->java的新特性(一)

java

  通过简单的实例来感觉一下java7和java8的新特性。当那条唯捷径省略了朝拜者,我便在一滴花露中瞬间彻悟。

 一、java7中switch中可以字符串

@Test

public void jdk7Switch() {

String name = "huhx";

switch (name) {

case "huhx":

System.out.println("name is = " + name);

break;

case "linux":

System.out.println("name is = " + name);

break;

default:

System.out.println("my name is huhx.");

}

}

二、二进制和数字下划线的分割的支持

@Test

public void jdk7Binary() {

int bNum = 0b11110001;

System.out.println(bNum); // 241

int num = 12_234_34_45;

System.out.println(num); // 122343445

}

三、关于异常和自动的资源管理

@Test

public void jdk7CatchExceptions() {

try {

Integer.parseInt("aa");

} catch (NumberFormatException | MaxRunTimeException e) {

e.printStackTrace();

}

}

@Test

public void jdk7TryWithResource() throws IOException {

try (BufferedReader reader = new BufferedReader(new FileReader("C:/Users/76801/Desktop/huhx.txt"))) {

StringBuilder builder = new StringBuilder();

String line = null;

while ((line = reader.readLine()) != null) {

builder.append(line + "\n");

}

System.out.println(builder.toString());

}

}

 java8代码实例

一、Lambda的简单使用

package com.tomhu.huhx.java8;

import org.junit.Test;

import java.util.function.Function;

/**

* @Author: huhx

* @Date: 2017-09-21 上午 9:10

*/

public class LambdaTest {

@Test

public void lambdaThread1() {

Thread thread = new Thread(() -> {

System.out.println("hello wolrd");

});

thread.start();

}

@Test

public void lambaFunction1() {

// Function是1.8里面的函数式接口

Function<Integer, String> function1 = new Function<Integer, String>() {

@Override

public String apply(Integer integer) {

return null;

}

};

// lambda expression

Function<Integer, String> function2 = (t) -> String.valueOf(t);

// 引用的方式

Function<Integer, String> function3 = String::valueOf;

}

}

二、函数式接口

  • 定义一个函数式接口:DefaultFunInterface]

package com.tomhu.huhx.java8;

/**

* @Author: huhx

* @Date: 2017-09-21 上午 9:35

*/

public interface DefaultFunInterface {

default int count() {

return 1;

}

public static int find() {

return 1;

}

}

  • 对上述的接口测试:InterfaceSuper

package com.tomhu.huhx.java8;

import org.junit.Test;

/**

* @Author: huhx

* @Date: 2017-09-21 上午 9:34

*/

public class InterfaceSuper {

@Test

public void defaultMethond() {

DefaultFunInterface funInterface = new SubDefaultFunClass();

System.out.println(funInterface.count()); // 1

}

// 接口可以直接调用自己的static方法

@Test

public void staticMethod() {

System.out.println(DefaultFunInterface.find()); // 1

}

}

class SubDefaultFunClass implements DefaultFunInterface {

@Override

public int count() {

return 0;

}

}

三、java中nio的改进

package com.tomhu.huhx.java8;

import org.junit.Test;

import java.io.File;

import java.io.IOException;

import java.nio.file.Files;

import java.time.LocalDate;

import java.util.ArrayList;

import java.util.List;

/**

* @Author: huhx

* @Date: 2017-09-21 上午 9:39

*/

public class StreamTest {

@Test

public void streamTest1() {

List<String> stringList = new ArrayList<>();

stringList.add("linux");

stringList.add("liuling");

stringList.add("liuli");

stringList.add("tomhu");

stringList.add("zhoucf");

stringList.stream().filter((s -> s.startsWith("l"))).forEach(System.out::print); // linuxliulingliuli

}

@Test

public void findAllFiles() throws IOException {

Files.list(new File(".").toPath()).forEach(System.out::println);

}

@Test

public void localDateTest() {

LocalDate localDate = LocalDate.now(); // 2017-09-21

System.out.println(localDate);

localDate = LocalDate.ofYearDay(2017, 333); // 2017-11-29

System.out.println(localDate);

}

}

四、更加简单的遍历Collection类型

public void listForEachTest() {

List<String> lists = Arrays.asList("huhx", "linux", "liuling");

lists.forEach(item -> {

if (item.startsWith("l")) {

System.out.println(item);

}

});

lists.forEach(System.out::println);

lists.stream().filter(item -> item.contains("l")).forEach(System.out::println);

}

public void mapForEachTest() {

Map<String, String> map = new HashMap<>();

map.put("name", "huhx");

map.put("pass", "1234");

map.forEach((key, val)-> {

if (key.equals("name")) {

System.out.println(val);

}

});

}

友情链接

  • 比较详细的java8介绍:https://www.ibm.com/developerworks/cn/java/j-lo-jdk8newfeature/index.html

  • 详细的java8介绍:http://www.importnew.com/11908.html

以上是 java基础---->java的新特性(一) 的全部内容, 来源链接: utcz.com/z/393213.html

回到顶部