在 Java 中,初始化静态映射可以创建与类而不是类实例关联的映射对象。这允许该映射在该类的所有实例之间共享并在不创建对象的情况下进行访问。当存储需要在类的多个实例之间共享的数据或维护在整个应用程序中可访问的全局映射时,静态映射特别有用。
方法 1:静态初始化块
- 将静态映射定义为类变量。
- 使用静态初始化块实例化映射并用键值对填充它。
- 该块在 JVM 加载类时执行。
public class StaticMapExample { // 将静态map定义为类变量 ; private static Map<String,Integer> staticMap; // 静态初始化块 ; static { // 实例化map并填充键值对 ; staticMap=new HashMap<>(); staticMap.put("apple",10); staticMap.put("banana",20); staticMap.put("orange",30); } // Main method to demonstrate accessing the static map public static void main(String[] args) { // Access and use the static map System.out.println(staticMap.get("apple")); System.out.println(staticMap.get("banana")); System.out.println(staticMap.get("orange")); } }
|
方法二:静态工厂方法
- 将静态映射定义为类变量。
- 创建一个初始化并返回映射的静态方法。
- 调用该方法获取Map实例。
- 属于Gof设计模式中工厂模式
public class StaticMapExample { // Define the static map as a class variable private static Map<String,Integer> staticMap; // 静态工厂方法,用于初始化和返回map; public static Map<String,Integer> initializeStaticMap() { // Instantiate the map and populate it with key-value pairs staticMap = new HashMap<>(); staticMap.put("apple",10); staticMap.put("banana",20); staticMap.put("orange",30); return staticMap; } // Main method to demonstrate accessing the static map public static void main(String[] args) { // Call the static method to obtain the initialized static map Map<String,Integer> map=initializeStaticMap(); // Access and use the static map System.out.println(map.get("apple")); System.out.println(map.get("banana")); System.out.println(map.get("orange")); } }
|
方法三:直接初始化
- 将静态映射定义为类变量。
- 直接在声明点初始化并填充map。
public class StaticMapExample { // Define the static map as a class variable and initialize it directly private static Map<String,Integer>staticMap=new HashMap<>() { { // Populate the map with key-value pairs put("apple",10); put("banana",20); put("orange",30); } }; // Main method to demonstrate accessing the static map public static void main(String[] args) { // Access and use the static map System.out.println(staticMap.get("apple")); System.out.println(staticMap.get("banana")); System.out.println(staticMap.get("orange")); } } 注意:这种方法虽然简洁,但也有一些缺点,比如会创建一个匿名内部类。
|
方法四:使用Java8 Stream
Stream 在Java 8 中引入,Stream API 用于处理对象集合。流是支持各种方法的对象序列,这些方法可以通过管道传输以产生所需的结果。
// Declaring and instantiating the static map private static Map<String, String> map = Arrays.stream(new String[][] { { "1", "GFG" }, { "2", "Geek" }, { "3", "GeeksForJdon" } }) .collect(Collectors.toMap( keyMapper -> keyMapper[0], valueMapper -> valueMapper[1])); // Driver code public static void main(String[] args) { System.out.println(map); }
|
方法五:使用Java 9+ Map.of:
public class MyClass { private static Map<String, Integer> myMap = Map.of( "key1", 1, "key2", 2 // Add more entries as needed ); }
|
这是从 Java 9 开始提供的,它提供了一种创建不可变映射的简洁方法。方法六:使用 Google Guava:
如果使用 Google Guava 库,则可以使用其 ImmutableMap 生成器:
import com.google.common.collect.ImmutableMap;
public class MyClass { private static Map<String, Integer> myMap = ImmutableMap.<String, Integer>builder() .put("key1", 1) .put("key2", 2) // Add more entries as needed .build(); }
|
选择最符合您的要求和编码标准的方法。
静态初始化块和静态工厂方法一般比较常见,因为它们简单明了,不涉及任何额外的构造。