java中的語法糖有:1.lambda表達式;2.可變參數;3.條件編譯;4.switch;5.增強for循環;6.泛型;
java中的語法糖有以下幾種
1.lambda表達式
public void lambdaTest() {
List
list = new ArrayList<>(); list = list.stream().distinct().collect(Collectors.toCollection(LinkedList::new));
}
2.可變參數
public void strings(String... strings) {
for (String s : strings) {
System.out.println(s);
}
}
3.條件編譯
public void ifTest() {
if (true) {
System.out.println("1");
} else {
System.out.println("2");
}
}
4.switch
public int switchString(String str) {
switch (str){
case "A":
return 1;
case "B":
return 2;
default:
return 0;
}
}
5.增強for循環
public void forTest() {
List
stringList = Arrays.asList("A", "B"); for (String str :stringList) {
System.out.println(str);
}
}
6.泛型
public void listTest() {
List
list = new ArrayList (); int i = list.size();
}