![]() ![]() The unmodifiable Map returned by Collections.unmodifiableMap, is a wrapper on source Map and if source Map goes to change, the unmodifiable Map will also change. The Map.of and Map.ofEntries methods are different from Collections.unmodifiableMap method. The iteration order is not fixed, it may change.Ĥ. They will be serializable if keys and values are serializable. The unmodifiable Map disallow null keys and values. In an immutable Map, we cannot change anything.ģ. The object reference of key and value cannot be changed but their values can be changed if they are not immutable. After its creation, the keys and values cannot be added, deleted and updated and if we try to these operation, UnsupportedOperationException will be thrown. The Map.of and Map.ofEntries methods create unmodifiable Map. As always, the code samples are available over on GitHub. For these cases, we also have access to more specific factory functions. On the other hand, some use cases (such as legacy code bases) may require specific maps. The most commonly used are the mapOf and mutableMapOf. It consumes less memory and performs faster.Ģ. Kotlin has a variety of functions to create maps. An immutable Map is automatically thread-safe. An unmodifiable Map containing immutable objects is called immutable Map. The unmodifiable Map and immutable Map are not same. The main difference between them is that a LinkedHashMap maintains insertion order when iterating over its entries. The two primary types are LinkedHashMap and HashMap. The unmodifiable Map cannot add, delete element and we can also not update the reference of key and value but we can change the values of these objects. Kotlin includes several Map implementations in the standard library. The Map.of and Map.ofEntries are the static factory methods of and has been introduced in Java 9.ġ. Java 9The Map.of and Map.ofEntries are static factory methods that return unmodifiable Map containing specified mapping. ![]()
0 Comments
Leave a Reply. |