Skip to content

gersonkm/kotlin-vs-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 

Repository files navigation

kotlin vs Java

Collection Initialization

Immutable List

// kotlin
val nums = listOf(1, 2, 3)
// Java + Guava
ImmutableList<Integer> nums = ImmutableList.of(1, 2, 3);

######P.S. Collections.unmodifiableList(..) is not really immutable.

Mutable List

// kotlin
val nums = mutableListOf(4, 5, 6)
// Java
List<Integer> nums = Arrays.asList(1, 2, 3);

// Java + Guava
List<Integer> nums = Lists.newArrayList(4, 5, 6);

Empty List

// kotlin
val nums = emptyList()
// Java
List<Integer> nums = Collections.emptyList();

// Java + Guava
ImmutableList<Integer> nums = ImmutableList.of();

Immutable Set

// kotlin
val nums = setOf(1, 2, 3)
// Java + Guava
ImmutableSet<Integer> nums = ImmutableSet.of(1, 2, 3);

######P.S. Collections.unmodifiableSet(..) is not really immutable.

Mutable Set

// kotlin
val nums = mutableSetOf(4, 5, 6)
// Java + Guava
Set<Integer> nums = Sets.newHashSet(4, 5, 6);

Empty Set

// kotlin
val nums = emptySet()
// Java
Set<Integer> nums = Collections.emptySet();

// Java + Guava
ImmutableSet<Integer> nums = ImmutableSet.of();

Immutable Map

// kotlin
val map = mapOf(1 to "a", 2 to "b", 3 to "c")
// Java + Guava
ImmutableMap<Integer, String> map = ImmutableMap.of(1, "a", 2, "b", 3, "c");

######P.S. Collections.unmodifiableMap(..) is not really immutable.

Mutable Map

// kotlin
val map = mutableMapOf(4 to "d", 5 to "e", 6 to "f")
// Java + Guava
ImmutableMap<Integer, String> map = ImmutableMap.of(1, "a", 2, "b", 3, "c");

Empty Map

// kotlin
val map = emptyMap()
// Java + Guava
ImmutableMap<Integer> map = ImmutableMap.of();

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published