Fishing at high speed, these Map operations in Guava have reduced my code size by 50%

As a popular Java library, Guava provides many useful tools and features, one of which is the ability to manipulate Maps. In this post, we'll explore some of the operations that can be performed on Maps using Guava.
insert image description here

Create custom maps with MapMaker

Guava's MapMaker class allows you to create Maps with various customization options. For example, you can set the size of the Map, set the concurrency level, enable weak references, and more. The following is a sample code that demonstrates how to use MapMaker to create a Map with a maximum value, once this maximum value is exceeded, the oldest entry is deleted.

Map<String, String> map = new MapMaker()
   .maximumSize(1000)
   .expireAfterWrite(10, TimeUnit.MINUTES)
   .makeMap();

In the above code, we use the maximumSize() method to set the maximum size of the Map to 1000, and use the expireAfterWrite() method to set the expiration time of each entry in the Map to 10 minutes. Once the maximum size is exceeded, MapMaker will automatically delete the oldest entries.
insert image description here
Using BiMap for bidirectional mapping
Guava's BiMap class provides the function of bidirectional mapping, that is, you can get the value through the key, and you can also get the key through the value. Here is a sample code that demonstrates how to use BiMap:

BiMap<String, Integer> bimap = HashBiMap.create();
bimap.put("one", 1);
bimap.put("two", 2);
bimap.put("three", 3);
System.out.println(bimap.get("one")); // Output: 1
System.out.println(bimap.inverse().get(2)); // Output: two

In the above code, we use the HashBiMap.create() method to create a new BiMap and use the put() method to add keys and values ​​to that map. We can then use the get() method to get the value by key and the inverse() method to get the inverse of that map to get the key by value.

Multi-value mapping using Multimap

Guava's Multimap class allows you to map multiple values ​​onto a single key. This is useful for situations where you need to associate multiple values ​​with a single key. Here is a sample code that demonstrates how to use Multimap:

ListMultimap<String, Integer> multimap = ArrayListMultimap.create();
multimap.put("one", 1);
multimap.put("one", 2);
multimap.put("two", 3);

System.out.println(multimap.get("one")); // Output: [1, 2]

In the above code, we use the ArrayListMultimap.create() method to create a new Multimap and use the put() method to associate multiple values ​​with a single key. We can then use the get() method to get all the values ​​associated with a given key.

Use ImmutableMap to create an immutable Map

Guava's ImmutableMap class allows you to create immutable Maps where all key-value pairs cannot be modified. Here is a sample code demonstrating how to use ImmutableMap:

ImmutableMap<String, Integer> immutableMap = ImmutableMap.of(
"one", 1,
"two", 2,
"three", 3
);
System.out.println(immutableMap); // Output: {one=1, two=2, three=3}

In the above code, we use the ImmutableMap.of() method to create a new immutable Map and provide some key-value pairs. We can then use any method of that Map to access and query its contents, but not to modify it.
insert image description here

Create a 2D map using Table

Guava's Table class allows you to create a map that resembles a two-dimensional table, where each key can map to multiple values. Here is a sample code that demonstrates how to use Table:

Table<Integer, Integer, String> table = HashBasedTable.create();
table.put(1, 1, "one");
table.put(1, 2, "two");
table.put(2, 1, "three");

System.out.println(table.get(1, 1)); // Output: one
System.out.println(table.row(1)); // Output: {1=one, 2=two}

In the above code, we use the HashBasedTable.create() method to create a new Table and use the put() method to map each key to a value. We can then use the get() method to get the specified value for the specified key, or use the row() method to get all the values ​​associated with the specified key.

Use MapDifference to compare the differences between two Maps

Guava's MapDifference class allows you to compare the difference between two Maps, and get the difference between them. Here is a sample code demonstrating how to use MapDifference:

Map<String, Integer> map1 = ImmutableMap.of(
"one", 1,
"two", 2,
"three", 3
);

Map<String, Integer> map2 = ImmutableMap.of(
"one", 1,
"four", 4,
"five", 5
);

MapDifference<String, Integer> diff = Maps.difference(map1, map2);

System.out.println(diff.entriesInCommon()); // Output: {one=1}
System.out.println(diff.entriesOnlyOnLeft()); // Output: {two=2, three=3}
System.out.println(diff.entriesOnlyOnRight()); // Output: {four=4, five=5}

In the code above, we create two different Maps and then look for the different items in the two maps.

These are some examples of using Guava to manipulate Maps. Guava also provides many other useful Map operations, such as using the Table class to create a two-dimensional map, using the MapDifference class to compare the differences between two Maps, and more.

Guess you like

Origin blog.csdn.net/KRYST4L123/article/details/129857159