Created
October 22, 2012 08:00
-
-
Save ngsw-taro/3930225 to your computer and use it in GitHub Desktop.
ImmutableCollection in Kotlin
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
// 不変コレクションを作る場合は ImmutableArrayListBuilderクラス を使おう | |
// このテストは失敗する | |
package com.taroid.kt.sample | |
import kotlin.test.assertEquals | |
import org.junit.Test as test | |
class ImmutableCollectionTest() { | |
test fun testImmutableCollection() { | |
// 可変コレクションを生成し、要素を1つ追加 | |
val mutableCollection: MutableCollection<String> = java.util.ArrayList() | |
mutableCollection.add("hoge") | |
// 不変コレクション型変数に、可変コレクションのインスタンスへの参照を代入 | |
val immutableCollection: Collection<String> = mutableCollection | |
// 可変コレクションに要素を1つ追加 | |
mutableCollection.add("fuga") | |
// 不変コレクションの要素数が変更されていないことを期待 | |
assertEquals(1, immutableCollection.size) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment