我们知道在java编程中,定义常量时必须用到:static final,既是静态的,又是不可修改的;
普通类型的常量可以这么定义,但是对于集合类型的常量,我们该怎么定义才好用呢?
本文就来讲一下常量Map的定义,如有错误欢迎大家指点批评!
一、普通类型常量定义
举例如下:
private staic final String SERVICE_URL="XXXXXXXX";
这样就完成了定义;
二、集合Map常量定义
方式一:
private static final Map<String, String> constMap1 = new HashMap<String, String>() {
{
put("name", "map1");
put("age", "100");
}
};
方式二:
private static final Map<String, String> constMap2 = new HashMap<String, String>();
static {
constMap2.put("name", "map2");
constMap2.put("age", "1000");
}
貌似上面两种方式都完成了常量Map定义,常量Map不能修改内存指向,可以满足一定的业务需求。
但是,有些时候,我们想要的是:不仅Map不能修改指向,同时也要实现,不能对其进行元素增删等操作,那么这时,我们就会用到:unmodifiableMap,Java提供的一种不能修改元素的Map实现,简要分析源码如下:
1、构建一个UnmodifiableMap对象
Map<String, String> unmodifiableMap = Collections.unmodifiableMap(map);
源码分析:
Collections工具类提供了一个静态方法,可以用于构造UnmodifiableMap对象
2、UnmodifiableMap对象是否可以增删元素,源码如下:
public V put(K key, V value) {
throw new UnsupportedOperationException();
}
public V remove(Object key) {
throw new UnsupportedOperationException();
}
public void putAll(Map<? extends K, ? extends V> m) {
throw new UnsupportedOperationException();
}
public void clear() {
throw new UnsupportedOperationException();
}
@Override
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
throw new UnsupportedOperationException();
}
@Override
public V putIfAbsent(K key, V value) {
throw new UnsupportedOperationException();
}
@Override
public boolean remove(Object key, Object value) {
throw new UnsupportedOperationException();
}
@Override
public boolean replace(K key, V oldValue, V newValue) {
throw new UnsupportedOperationException();
}
@Override
public V replace(K key, V value) {
throw new UnsupportedOperationException();
}
@Override
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
throw new UnsupportedOperationException();
}
@Override
public V computeIfPresent(K key,
BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
throw new UnsupportedOperationException();
}
@Override
public V compute(K key,
BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
throw new UnsupportedOperationException();
}
@Override
public V merge(K key, V value,
BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
throw new UnsupportedOperationException();
}
由以上源码可见,当对该Map进行增加元素、删除元素、替换元素等操作时,都会抛出:不支持的操作的异常。
三、UnmodifiableMap使用举例
1、在org.springframework.boot.autoconfigure.orm.jpa.DatabaseLookup中的应用:
static {
Map<DatabaseDriver, Database> map = new EnumMap<>(DatabaseDriver.class);
map.put(DatabaseDriver.DERBY, Database.DERBY);
map.put(DatabaseDriver.H2, Database.H2);
map.put(DatabaseDriver.HSQLDB, Database.HSQL);
map.put(DatabaseDriver.MYSQL, Database.MYSQL);
map.put(DatabaseDriver.ORACLE, Database.ORACLE);
map.put(DatabaseDriver.POSTGRESQL, Database.POSTGRESQL);
map.put(DatabaseDriver.SQLSERVER, Database.SQL_SERVER);
map.put(DatabaseDriver.DB2, Database.DB2);
map.put(DatabaseDriver.INFORMIX, Database.INFORMIX);
map.put(DatabaseDriver.HANA, Database.HANA);
LOOKUP = Collections.unmodifiableMap(map);
}
2、测试验证
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* 不可修改的Map
*/
public class UnmodifiableMapTest {
// 定义Map常量1
private static final Map<String, String> constMap1 = new HashMap<String, String>() {
{
put("name", "map1");
put("age", "100");
}
};
// 定义Map常量2
private static final Map<String, String> constMap2 = new HashMap<String, String>();
static {
constMap2.put("name", "map2");
constMap2.put("age", "1000");
}
private void constantMap() {
// 向constMap1添加元素
constMap1.put("hobby", "sing");
for (Map.Entry<String, String> row : constMap1.entrySet()) {
System.out.println(row.getKey() + ":" + row.getValue());
}
// 向constMap2添加元素
constMap2.put("hobby", "sing");
for (Map.Entry<String, String> row : constMap2.entrySet()) {
System.out.println(row.getKey() + ":" + row.getValue());
}
}
private void unmodifiableMap() {
// 可修改
Map<String, String> map = new HashMap<String, String>();
map.put("name", "jake");
map.put("gener", "male");
System.out.println(map.size());
// 转换为不可修改
Map<String, String> unmodifiableMap = Collections.unmodifiableMap(map);
try {
unmodifiableMap.put("hobby", "sing");
} catch (Exception e) {
//此处会抛出异常:java.lang.UnsupportedOperationException,表示不可以修改
System.out.println(e.getClass());
}
}
public static void main(String[] args) {
UnmodifiableMapTest umt = new UnmodifiableMapTest();
umt.constantMap();
umt.unmodifiableMap();
}
}