// or similar depending on your implementation and needs; can be
// extended for choosing get(), set(), etc.
boolean flag;
HashMap<Key, Value> trueMap = new HashMap<>();
HashMap<Key, Value> falseMap = new HashMap<>();
HashMap<Key, Value> map = flag ? trueMap : falseMap;
1条答案
按热度按时间gcuhipw91#
有两种方法可以做到这一点(就我目前所能想到的):
方法1:(来自注解中的@wjs)
有一个带有布尔键的hashmap和相应的hashmap作为值。像这样:
方法2:由于只能有两个hashmap对应于您的布尔真值或假值,我认为您不需要有另一个hashmap,可以简单地使用一个布尔变量。
例如:
像这样选择hashmaps并没有坏处,而且实际上是相当常见的。