Skip to content

Commit

Permalink
update JavaExample and KotlinExample
Browse files Browse the repository at this point in the history
  • Loading branch information
BetterZhang committed Jun 27, 2017
1 parent a0424de commit 9cbde92
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/com/betterzhang/learnkotlin/java/JavaExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,15 @@ public static void main(String[] args) {
List<String> collection = new ArrayList<>();
for (String item : collection) {}
Map<String, String> map = new HashMap<>();
for (Map.Entry<String, String> entry : map.entrySet()) {}
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + ", " + entry.getValue());
}
// or
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
System.out.println(entry.getKey() + ", " + entry.getValue());
}

final List<Integer> listOfNumber = Arrays.asList(1, 2, 3, 4);
final Map<Integer, String> keyValue = new HashMap<Integer, String>();
Expand Down Expand Up @@ -135,10 +143,20 @@ public static void main(String[] args) {
e.printStackTrace();
}

Util.getScore(5);

int result = Util.triple(3);

Developer developer4;

List<String> list = Arrays.asList("java", "c++", "Android", "Kotlin", "iOS");
for (int i = 0; i < list.size(); i++) {
list.set(i, "Hello " + list.get(i));
if (!list.get(i).contains("c")) {
System.out.println(list.get(i));
}
}

}

void doSomething1() {
Expand Down
20 changes: 20 additions & 0 deletions src/com/betterzhang/learnkotlin/java/Manager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.betterzhang.learnkotlin.java;

/**
* Created by IntelliJ IDEA.
* Author : Andrew Zhang
* Email : betterzhang.dev@gmail.com
* Time : 2017/06/27 下午 5:09
* Desc : description
*/
public class Manager {

private Manager() {

}

public static Manager getInstance() {
return new Manager();
}

}
24 changes: 24 additions & 0 deletions src/com/betterzhang/learnkotlin/java/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.betterzhang.learnkotlin.java;

/**
* Created by IntelliJ IDEA.
* Author : Andrew Zhang
* Email : betterzhang.dev@gmail.com
* Time : 2017/06/27 下午 5:05
* Desc : description
*/
public final class Person {

private String name = null;
private int age = 25;

private Person() {

}

public Person(String name, int age) {
this.name = name;
this.age = age;
}

}
16 changes: 16 additions & 0 deletions src/com/betterzhang/learnkotlin/kotlin/KotlinExample.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@ fun main(args: Array<String>) {
for ((key, value) in map) {}

val listOfNumber = listOf(1, 2, 3, 4)
listOfNumber.forEach { println(it) }
val keyValue = mapOf(1 to "one", 2 to "two", 3 to "three")
for ((key, value) in keyValue) {
println("$key, $value")
}
// or
keyValue.forEach { key, value -> println("$key, $value") }

val developers = listOf(Developer("John", 20), Developer("Lucy", 24))
developers.forEach { println(it.name) }
Expand Down Expand Up @@ -116,13 +122,23 @@ fun main(args: Array<String>) {
// in case you only want to copy selected properties
val dev3 = dev.copy(age = 25)

var (name, age) = dev
println("$name, $age")

Util.getScore(5)

fun Int.triple(): Int {
return 3 * this
}

val result = 3.triple()
val developer4: Developer

var list = listOf("java", "c++", "Android", "Kotlin", "iOS")
list.map { "Hello $it" }
.filter { !it.contains("c") }
.forEach { it }

}

class Util private constructor() {
Expand Down
16 changes: 16 additions & 0 deletions src/com/betterzhang/learnkotlin/kotlin/Manager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.betterzhang.learnkotlin.kotlin

/**
* Created by IntelliJ IDEA.
* Author : Andrew Zhang
* Email : betterzhang.dev@gmail.com
* Time : 2017/06/27 下午 5:10
* Desc : description
*/
class Manager private constructor() {

companion object {
fun getInstance(): Manager = Manager()
}

}
20 changes: 20 additions & 0 deletions src/com/betterzhang/learnkotlin/kotlin/Person.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.betterzhang.learnkotlin.kotlin

/**
* Created by IntelliJ IDEA.
* Author : Andrew Zhang
* Email : betterzhang.dev@gmail.com
* Time : 2017/06/27 下午 5:06
* Desc : description
*/
class Person private constructor() {

private var name: String? = null
private var age: Int = 25

constructor(name: String, age: Int): this() {
this.name = name
this.age = age
}

}

0 comments on commit 9cbde92

Please sign in to comment.