Android kotlin 与java的对比方法大全1
2017-09-07 本文已影响42人
sweetheast
1.打印对比:
java
System.out.print("Hello, World!");
System.out.println("Hello, World!");
kotlin
print("Hello, World!")
println("Hello, World!")
2.变量定义1:
java
final int x;
final int y = 1;
kotlin
val x: Int
val y = 1
3.变量定义2:
java
int w;
int z = 2;
z = 3;
w = 1;
kotlin
var w: Int
var z = 2
z = 3
w = 1
4.初始化变量
java
final String name = null;
String lastName;
lastName = null
kotlin
val name: String? = null
var lastName: String?
lastName = null
var firstName: String
firstName = null //编译错误
5.判断空
java
if(text != null){
int length = text.length();
}
kotlin
val length = text?.length// text为空时不执行.length
val length = text!!.length // text为空时抛异常
6.字符串1:
java
String name = "John";
String lastName = "Smith";
String text = "My name is: " + name + " " + lastName;
String otherText = "My name is: " + name.substring(2);
kotlin
val name = "John"
val lastName = "Smith"
val text = "My name is: $name $lastName"
val otherText = "My name is: ${name.substring(2)}"
7.字符串2:
java
String text = "First Line\n" +
"Second Line\n" +
"Third Line";
kotlin
val text = """
|First Line
|Second Line
|Third Line
""".trimMargin()
8.三元运算符:
java
String text = x > 5 ? "x > 5" : "x <= 5";
kotlin
val text = if (x > 5)
"x > 5"
else "x <= 5"
9.位操作:
java
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;
kotlin
val andResult = a and b
val orResult = a or b
val xorResult = a xor b
val rightShift = a shr 2
val leftShift = a shl 2
10.比较类型和范围表达
java
if(x instanceof Integer){ }
final String text = (String) other;
if(x >= 0 && x <= 10 ){}
kotlin
if (x is Int) { }
val text = other as String
if (x in 0..10) { }
11.分割字符串
java
if(a instanceof String){
final String result = ((String) a).substring(1);
}
kotlin
if (a is String) {
val result = a.substring(1)
}
12.Switch / When语句
java
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";
}
kotlin
val x = // value
val xResult = when (x) {
0, 11 -> "0 or 11"
in 1..10 -> "from 1 to 10"
!in 12..14 -> "not from 12 to 14"
else -> if (isOdd(x)) { "is odd" } else { "otherwise" }
}
val y = // value
val yResult = when {
isNegative(y) -> "is Negative"
isZero(y) -> "is Zero"
isOdd(y) -> "is odd"
else -> "otherwise"
}
13.for循环
java
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()) { }
kotlin
for (i in 1 until 11) { }
for (i in 1..10 step 2) {}
for (item in collection) {}
for ((index, item) in collection.withIndex()) {}
for ((key, value) in map) {}
14.集合操作1:
java
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 9
final List<Integer> numbers = List.of(1, 2, 3);
final Map<Integer, String> map = Map.of(1, "One",
2, "Two",
3, "Three");
kotlin
val numbers = listOf(1, 2, 3)
val map = mapOf(1 to "One",
2 to "Two",
3 to "Three")
15.集合操作2:
java
for (int number : numbers) {
System.out.println(number);
}
for (int number : numbers) {
if(number > 5) {
System.out.println(number);
}
}
kotlin
numbers.forEach {
println(it)
}
numbers.filter { it > 5 }
.forEach { println(it) }
16.集合操作3:
java
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);
}
kotlin
val groups = numbers.groupBy {
if (it and 1 == 0) "even" else "odd"
}
17.集合操作4:
java
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);
}
}
kotlin
val (evens, odds) = numbers.partition { it and 1 == 0 }
18.集合操作5:
java
final List<User> users = getUsers();
Collections.sort(users, new Comparator<User>(){
public int compare(User user, User otherUser){
return user.lastname.compareTo(otherUser.lastname);
}
});
// 或
users.sort(Comparator.comparing(user -> user.lastname));
kotlin
val users = getUsers()
users.sortedBy { it.lastname }