Jimmutable Collections: Constructing durability and efficient and unchanged tools in the Java class library
Jimmutable Collections is a set of tools in the Java class library to build persistent and efficient inseparable sets.In this article, we will explore the principles, uses, and sample code of Jimmutable Collections.
The variable sets in Java (such as ArrayList and HashMap) allows modification during use, which may cause errors and inconsistency.The uncharacteristic collection refers to a collection that cannot be modified once it is created. This characteristic is particularly useful in concurrent environment.
Jimmutable Collections achieves non -variability by using persistent data structures.The durable data structure means that when modifying, the original structure will not be modified, but to create a new structure and share as many common data as possible.This method of sharing makes Jimmutable Collections very efficient when a large amount of data modification.
Jimmutable Collections supports a variety of data structures, including List, MAP and SET.Below we will show how to use Jimmutable Collections to build uncharacteristic sets through some example code.
First of all, let's create an inseparable list:
JImmutableList<String> list = JImmutables.listOf("apple", "banana", "cherry");
System.out.println (list); // Output: ["Apple", "Banana", "Cherry"]
// Add elements to the list
JImmutableList<String> newList = list.insert("date");
System.out.println (newList); // Output: ["Apple", "Banana", "Cherry", "Date"]
// Delete elements from list
JImmutableList<String> newerList = newList.delete("banana");
System.out.println (NEWERLIST); // Output: ["Apple", "Cherry", "Date"]
By calling the method of `jimmutables.listof (), we can create a variable list.We can use the `Insert ()" method to add elements to the list, and use the `delete () method to delete the element from the list.It should be noted that each modification operation will return a new list, and the original list will not be modified.
Next, let's take a look at how to create an immutable map:
JImmutableMap<String, Integer> map = JImmutables.mapOf("apple", 1, "banana", 2, "cherry", 3);
System.out.println (map); // Output: {"apple" = 1, "banana" = 2, "cherry" = 3}
// Add key values to the map
JImmutableMap<String, Integer> newMap = map.assign("date", 4);
System.out.println (newmap); // Output: {"apple" = 1, "banana" = 2, "cherry" = 3, "date" = 4}
// Delete the key value pair from the map
JImmutableMap<String, Integer> newerMap = newMap.delete("banana");
System.out.println (newsmap); // Output: {"Apple" = 1, "Cherry" = 3, "Date" = 4}
Similar to creating List, we can use `jimmutables.mapof ()` method to create a variable MAP.Use the `Assign ()" method to add key value pairs to the map, and use the `delete () method to delete the key value from the map.Similarly, each modification operation returns a new MAP, and the original Map will not be modified.
In addition to List and Map, Jimmutable Collections also supports other data structures, such as SET.Use a similar way to use the method of `jimmutables.setof ()` to create a non -variable set, and modify the set using the methods such as `Insert (),` Delete () `.
In summary, Jimmutable Collections is a tool used in the Java library to build a tool for persistent and efficient and uncharacteria.By using persistent data structure and sharing data, Jimmutable Collections can provide efficient collection operations and thread security.Whether in a concurrent environment, or in the scene where the collection is needed frequently, Jimmutable Collections is a very useful tool.
I hope this article can help you understand and use Jimmutable Collections!