Skip to content

Instantly share code, notes, and snippets.

@sys1yagi
Last active December 14, 2015 20:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sys1yagi/5141091 to your computer and use it in GitHub Desktop.
Save sys1yagi/5141091 to your computer and use it in GitHub Desktop.
sample of java 7 and 8 new feature.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
// java8
lambda();
methodReference();
// java7
switchString("つらぽよ");
numUnderScore();
binaryLiteral();
diamondExpression();
multiCatch();
tryWithResources();
try {
safeReThrow();
} catch (Exception e) {
}
}
// java8
public static void lambda(){
List<Integer> list = Arrays.asList(10, 12, 8, 5);
Collections.sort(list, (o1, o2) -> o2 - o1);
System.out.println(list.toString());
}
interface Duck {
public String gaaaaa();
}
private static void honk(Duck duck) {
System.out.println(duck.gaaaaa());
}
static class Man {
public String yes() {
return "yes!!";
}
}
public static void methodReference(){
honk(new Man()::yes);
}
// java7
public static void switchString(String str) {
switch (str) {
case "つらぽよ":
System.out.println("(´・ω・`)");
break;
case "イヤーッ!":
System.out.println("グワーッ!");
break;
}
}
public static void numUnderScore() {
int n = 123_456_1222;
System.out.println(n);
}
public static void binaryLiteral() {
int n = 0b11110111;
System.out.println(n);
}
public static void diamondExpression() {
List<String> list = new ArrayList<>();
list.add("moge");
}
public static void tryWithResources() {
try (InputStream in = new FileInputStream("soge"); InputStreamReader isr = new InputStreamReader(in); BufferedReader br = new BufferedReader(isr);) {
// 処理
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void multiCatch() {
try {
InputStream in = new FileInputStream("moge");
} catch (IOException | NullPointerException e) {
e.printStackTrace();
}
}
public static void safeReThrow() {
try {
String a = null;
a.toUpperCase();
} catch (Exception e) {
throw e;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment