Comparison of Java 7 to Kotlin for various simple code snippets. Files with the same name do the same thing.
Last active
August 17, 2020 19:42
-
-
Save wispborne/5d7bde79a3967b27d9e25a74bccfdad7 to your computer and use it in GitHub Desktop.
Comparison of Java to Kotlin for various simple methods.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Person cloneWithNewName(Person original, String newName) { | |
return new Person( | |
original.id, | |
newName, | |
original.role, | |
original.gender, | |
original.seatingPosition, | |
original.title | |
) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun cloneWithNewName(orignal: Person, newName: String): Person = | |
original.copy(name = newName) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Returns a subset of {@code list} containing only the items where {@code someBool == true} */ | |
public final List<MyClass> filterList(List<MyClass> list) { | |
List<MyClass> ret = new List<>(list.size()); | |
for(MyClass item : list) { | |
if(item.someBool) { | |
ret.add(item); | |
} | |
} | |
return ret; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Returns a subset of `list` containing only the items where `someBool == true` */ | |
fun filterList(items:List<MyClass>) = items.filter { item -> item.someBool } // or { it.someBool } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Example of a {@link Class} declaration */ | |
public final class MyClass { | |
private boolean someBool; | |
public MyClass() { | |
} | |
public MyClass(boolean someBool) { | |
this.someBool = someBool; | |
} | |
public boolean getSomeBool() { | |
return someBool; | |
} | |
public void setSomeBool(@NonNull boolean value) { | |
someBool = value; | |
} | |
@Override | |
public String toString() { | |
return "someBool:" + someBool; | |
} | |
// not shown: .copy override | |
// not shown: .hashcode override | |
// not shown: .equals override | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Example of a [Class] declaration. */ | |
data class MyClass(var someBool: Boolean = false) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Gets the second character in a string or, if not possible, returns some default value (which may be {@code null}) */ | |
@Nullable | |
public final String getSecondCharOrDefault(@NonNull String str, @Nullable String defaultVal) { | |
return str.length() > 2 ? str.get(1) : defaultVal; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Gets the second character in a string or, if not possible, returns some default value (which may be `null`) */ | |
fun getSecondCharOrDefault(str: String, defaultVal: String?) = if(str.length > 2) str[1] else defaultVal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** First prints "one dish", then "two dish" */ | |
public final void printRxJavaStuff() { | |
Observable.from(Arrays.asList("one fish", "two fish", "red fish", "blue fish")) | |
.filter(new Func1<String, Boolean>() { | |
@Override | |
public Boolean call(String s) { | |
return s.contains("one") || s.contains("two"); | |
} | |
}) | |
.map(new Func1<String, String>() { | |
@Override | |
public String call(String s) { | |
return s.replace("fish", "dish"); | |
} | |
}) | |
.subscribe(new Action1<String>() { | |
@Override | |
public void call(String s) { | |
Timber.v(s); | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** First prints "one dish", then "two dish" */ | |
fun printRxJavaStuff() { | |
Observable.from(asList("one fish", "two fish", "red fish", "blue fish")) | |
.filter { it.contains("one") || it.contains("two") } | |
.map { it.replace("fish", "dish") } | |
.subscribe( { Timber.v(s) } ) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Returns {@code "yes"} if {@code value == true} or {@code "no"} if {@code value == false}. */ | |
public boolean yesOrNo(boolean value) { | |
return value ? "yes" : "no"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Returns `"yes"` if `value == true` or `"no"` if `value == false`. */ | |
fun yesOrNo(value: Boolean) = if(value) "yes" else "no" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment