Use Ka Commons Collections framework to perform MAP and Dictionary operation
Use Ka Commons Collections framework to perform MAP and Dictionary operation
Introduction:
Ka Commons Collections is a powerful Java open source framework that provides many practical collection classes and algorithms that can simplify MAP and Dictionary operations in Java programming.This article will introduce how to use Ka Commons Collections framework to perform Map and Dictionary operation, and provide some Java code examples.
Map operation:
MAP is a collection of Key-Value pairs and one of the very common data structures.Using Ka Commons Collections framework, we can perform MAP operations more conveniently.
1. Create a Map object:
import org.apache.commons.collections4.MapUtils;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
// Create an empty MAP object with Ka Commons Collections
Map<String, Integer> map = MapUtils.emptyIfNull(new HashMap<>());
// Add key value and go to MAP
map.put("key1", 1);
map.put("key2", 2);
map.put("key3", 3);
// Output MAP size
System.out.println ("MAP size:" + map.size ());
}
}
2. Check whether the map is empty:
import org.apache.commons.collections4.MapUtils;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Integer> map = MapUtils.emptyIfNull(new HashMap<>());
// Check whether the map is empty
boolean isEmpty = MapUtils.isEmpty(map);
System.out.println ("Map is empty:" + ISEMPTY);
}
}
3. Get the key value in the map:
import org.apache.commons.collections4.MapUtils;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Integer> map = MapUtils.emptyIfNull(new HashMap<>());
map.put("key1", 1);
map.put("key2", 2);
map.put("key3", 3);
// Get the value of the specific key in the map
int value1 = MapUtils.getIntValue(map, "key1");
System.out.println ("Key1 corresponding value:" + value1);
// Get all the key values in the map
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
Dictionary operation:
Dictionary is an abstract class for storing key values pairs and access values through keys.Ka Commons Collections framework provides support for Dictionary.
1. Create Dictionary object:
import org.apache.commons.collections4.MapUtils;
import java.util.Dictionary;
import java.util.Hashtable;
public class Main {
public static void main(String[] args) {
// Create an empty Dictionary object with Ka Commons Collections
Dictionary<String, Integer> dictionary = MapUtils.emptyIfNull(new Hashtable<>());
// Add key value pairs to Dictionary
dictionary.put("key1", 1);
dictionary.put("key2", 2);
dictionary.put("key3", 3);
// Get the size of Dictionary
System.out.println ("Dictionary size:" + dictionary.size ());
}
}
2. Check whether dictionary is empty:
import org.apache.commons.collections4.MapUtils;
import java.util.Dictionary;
import java.util.Hashtable;
public class Main {
public static void main(String[] args) {
Dictionary<String, Integer> dictionary = MapUtils.emptyIfNull(new Hashtable<>());
// Check if dictionary is empty
boolean isEmpty = MapUtils.isEmpty(dictionary);
System.out.println ("Dictionary Is it empty:" + ISEMPTY);
}
}
3. Get the key value in Dictionary:
import org.apache.commons.collections4.MapUtils;
import java.util.Dictionary;
import java.util.Hashtable;
public class Main {
public static void main(String[] args) {
Dictionary<String, Integer> dictionary = MapUtils.emptyIfNull(new Hashtable<>());
dictionary.put("key1", 1);
dictionary.put("key2", 2);
dictionary.put("key3", 3);
// Get the value of a specific key in Dictionary
int value1 = MapUtils.getIntValue(dictionary, "key1");
System.out.println ("Key1 corresponding value:" + value1);
// Get all the key values in Dictionary
for (Dictionary.Entry<String, Integer> entry : Collections.list(dictionary.elements())) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
in conclusion:
Ka Commons Collections framework provides practical methods and classes that simplify MAP and Dictionary operations.By using this framework, you can more conveniently perform operations, query and traversal operations such as MAP and Dictionary.Through the code examples provided here, you can use the Ka Commons Collections framework to perform MAP and Dictionary operations more easily.