Print输出
|
|
Java
12 System.out.print("Hello, World!");System.out.println("Hello, World!");
常量
|
|
Java
12 final int x;final int y = 1;
变量
|
|
Java
1234 int w;int z = 2;z = 3;w = 1;
可空变量
|
|
Java
123 final String name = null;String lastName;lastName = null
空值检查
|
|
Java
123 if(text != null){int length = text.length();}
String
|
|
Java
1234 String name = "John";String lastName = "Smith";String text = "My name is: " + name + " " + lastName;String otherText = "My name is: " + name.substring(2);
三元运算符
|
|
Java
1 String text = x > 5 ? "x > 5" : "x <= 5";
位运算
|
|
Java
12345 final int andResult = a & b;final int orResult = a | b;final int xorResult = a ^ b;final int rightShift = a >> 2;final int leftShift = a << 2;
is/as/in
|
|
Java
123 if(x instanceof Integer){ }final String text = (String) other;if(x >= 0 && x <= 10 ){}
when
|
|
Java
123456789101112131415161718192021222324252627282930313233343536373839404142 final int x = // value;final String xResult;switch (x){case 0:case 11:xResult = "0 or 11";break;case 1:case 2://...case 10:xResult = "from 1 to 10";break;default:if(x < 12 && x > 14) {xResult = "not from 12 to 14";break;}if(isOdd(x)) {xResult = "is odd";break;}xResult = "otherwise";}final int y = // value;final String yResult;if(isNegative(y)){yResult = "is Negative";} else if(isZero(y)){yResult = "is Zero";}else if(isOdd(y)){yResult = "is Odd";}else {yResult = "otherwise";}
for
|
|
Java
1234567 for (int i = 1; i < 11 ; i++) { }for (int i = 1; i < 11 ; i+=2) { }for (String item : collection) { }for (Map.Entry<String, String> entry: map.entrySet()) { }
集合
|
|
Java
1234567891011121314 final List<Integer> numbers = Arrays.asList(1, 2, 3);final Map<Integer, String> map = new HashMap<Integer, String>();map.put(1, "One");map.put(2, "Two");map.put(3, "Three");// Java 9final List<Integer> numbers = List.of(1, 2, 3);final Map<Integer, String> map = Map.of(1, "One",2, "Two",3, "Three");
forEach
|
|
Java
123 for (int number : numbers) {System.out.println(number);}
filter
|
|
Java
12345 for (int number : numbers) {if(number > 5) {System.out.println(number);}}
groupBy
|
|
Java
1234567891011121314151617181920212223 final Map<String, List<Integer>> groups = new HashMap<>();for (int number : numbers) {if((number & 1) == 0){if(!groups.containsKey("even")){groups.put("even", new ArrayList<>());}groups.get("even").add(number);continue;}if(!groups.containsKey("odd")){groups.put("odd", new ArrayList<>());}groups.get("odd").add(number);}// orMap<String, List<Integer>> groups = items.stream().collect(Collectors.groupingBy(item -> (item & 1) == 0 ? "even" : "odd"));
partition
|
|
Java
123456789 final List<Integer> evens = new ArrayList<>();final List<Integer> odds = new ArrayList<>();for (int number : numbers){if ((number & 1) == 0) {evens.add(number);}else {odds.add(number);}}
sortedBy
|
|
Java
1234567891011 final List<User> users = getUsers();Collections.sort(users, new Comparator<User>(){public int compare(User user, User otherUser){return user.lastname.compareTo(otherUser.lastname);}});// orusers.sort(Comparator.comparing(user -> user.lastname));