如何获取 HashMap<String, Object> 中特定键值的子集 initialMap = new HashMap<String, Object>()
How to get subset of values for a particular key in HashMap<String, Object> initialMap = new HashMap<String, Object>()
我有一个
HashMap<String, Object> initialMap = new HashMap<String, Object>();
其中我以这种方式设置了键的字符串值,
error=[e1, e2, e3, e4, e5, e6],[e7, e8, e9, e10, e11, e12],[e13, e14 ,e15, e16, e17, e18],[e19, e20, e21, e22, e23, e24],[e25, e26, e27, e28, e29, e30], status=[s1, s2, s3, s4, s5, s6],[s7, s8 s9, s10, s11, s12],[s13, s14 ,s15, s16, s17, s18],[s19, s20, s21, s22, s23, s24],[s25, s26, s27, s28, s29, s30], data=[[d1, d2, d3, d4, d5, d6],[d7, d8, d9, d10, d11, d12],[d13, d14 ,d15, d16, d17, d18],[d19, d20, d21, d22, d23, d24],[d25, d26, d27, d28, d29, d30]], warning=[w1, w2, w3, w4, w5, w6],[w7, w8, w9, w10, w11, w12],[w13, w14 ,w15, w16, w17, w18],[w19, w20, w21, w22, w23, w24],[w25, w26, w27, w28, w29, w30]}
所以,这里有一个名为 error 的键,
下面有这 5 组字符串值。
[e1, e2, e3, e4, e5, e6],[e7, e8, e9, e10, e11, e12],[e13, e14 ,e15, e16, e17, e18],[e19, e20, e21, e22, e23, e24],[e25, e26, e27, e28, e29, e30].
我不需要一个键的所有这 5 个设置值,但我需要使用开始和结束限制,即每个键的值的子集。
假设我的 startLimit=2 和 endLimit=4 那么我应该只得到 2,3,4 组键错误值,
[e7, e8, e9, e10, e11, e12],[e13, e14 ,e15, e16, e17, e18],[e19, e20, e21, e22, e23, e24]
同样需要对所有其他 3 个键重复。
我试过这种方法,但它总是给我一个特定键的所有值。
有什么方法可以获取哈希图中特定键值的子集吗?
这是我试过的代码,
public class MapTest {
HashMap<String, Object> finalMap = new HashMap<String, Object>();
int startLimit = 2;
int endLimit = 4;
public static void main(String[] args) {
MapTest mm = new MapTest();
mm.getData();
}
public void getData()
{
HashMap<String, Object> initialMap = new HashMap<String, Object>();
initialMap.put("status","[s1, s2, s3, s4, s5, s6],[s7, s8 s9, s10, s11, s12],[s13, s14 ,s15, s16, s17, s18],[s19, s20, s21, s22, s23, s24],[s25, s26, s27, s28, s29, s30]");
initialMap.put("data","[[d1, d2, d3, d4, d5, d6],[d7, d8, d9, d10, d11, d12],[d13, d14 ,d15, d16, d17, d18],[d19, d20, d21, d22, d23, d24],[d25, d26, d27, d28, d29, d30]]");
initialMap.put("warning","[w1, w2, w3, w4, w5, w6],[w7, w8, w9, w10, w11, w12],[w13, w14 ,w15, w16, w17, w18],[w19, w20, w21, w22, w23, w24],[w25, w26, w27, w28, w29, w30]");
initialMap.put("error","[e1, e2, e3, e4, e5, e6],[e7, e8, e9, e10, e11, e12],[e13, e14 ,e15, e16, e17, e18],[e19, e20, e21, e22, e23, e24],[e25, e26, e27, e28, e29, e30]");
Set<Entry<String, Object>> entries = initialMap .entrySet();
for (Iterator<Entry<String, Object>> i = entries.iterator(); i.hasNext(); ) {
Entry e = (Entry) i.next();
if(e.getKey().equals("error"))
{
Object errValue = initialMap.get(e.getKey());
//System.out.println("errValue" + errValue);
finalMap.put(e.getKey().toString(), errValue);
}
if(e.getKey().equals("data"))
{
Object errValue = initialMap.get(e.getKey());
//System.out.println("errValue" + errValue);
finalMap.put(e.getKey().toString(), errValue);
}
else if(e.getKey().equals("warning"))
{
Object errValue = initialMap.get(e.getKey());
//System.out.println("errValue" + errValue);
finalMap.put(e.getKey().toString(), errValue);
}
if(e.getKey().equals("status"))
{
Object errValue = initialMap.get(e.getKey());
//System.out.println("errValue" + errValue);
finalMap.put(e.getKey().toString(), errValue);
}
}
if(startLimit == 2 && endLimit == 4)
System.out.println("final map after limiting the data " + finalMap);
}
}
我正在从上面的程序中以这种方式获取一个键的所有 5 对值,
限制数据后的最终地图
{error=[e1, e2, e3, e4, e5, e6],[e7, e8, e9, e10, e11, e12],[e13, e14 ,e15, e16, e17, e18],[e19, e20, e21, e22, e23, e24],[e25, e26, e27, e28, e29, e30], status=[s1, s2, s3, s4, s5, s6],[s7, s8 s9, s10, s11, s12],[s13, s14 ,s15, s16, s17, s18],[s19, s20, s21, s22, s23, s24],[s25, s26, s27, s28, s29, s30], data=[[d1, d2, d3, d4, d5, d6],[d7, d8, d9, d10, d11, d12],[d13, d14 ,d15, d16, d17, d18],[d19, d20, d21, d22, d23, d24],[d25, d26, d27, d28, d29, d30]], warning=[w1, w2, w3, w4, w5, w6],[w7, w8, w9, w10, w11, w12],[w13, w14 ,w15, w16, w17, w18],[w19, w20, w21, w22, w23, w24],[w25, w26, w27, w28, w29, w30]}
但是我需要这样,因为我的限制是 2 到 4 对,
限制数据后的最终地图{error=[e7, e8, e9, e10, e11, e12],[e13, e14 ,e15, e16, e17, e18],[e19, e20, e21, e22, e23, e24], status=[s7, s8 s9, s10, s11, s12],[s13, s14 ,s15, s16, s17, s18],[s19, s20, s21, s22, s23, s24], data=[[d7, d8, d9, d10, d11, d12],[d13, d14 ,d15, d16, d17, d18],[d19, d20, d21, d22, d23, d24]], warning=[w7, w8, w9, w10, w11, w12],[w13, w14 ,w15, w16, w17, w18],[w19, w20, w21, w22, w23, w24]}
谁能帮我解决这个问题?
谢谢
我在您的代码中发现了 2 个大问题:
startLimit
和 endLimit
不用于过滤从 initialMap
中提取的数据,但它们只是在 i 值上进行测试;您的代码只需将 key/value 从 initialMap
复制到 finalMap
然后打印 finalMap
如果两个限制设置为 2 和 4(它们只是标志来决定是否打印或不是
initialMap
中的每个键都与 String
(不是 Object
或 List
)关联,所以我看到的唯一方法是将 [=39= 分开] 正在玩逗号..但我不确定这是否是您代码的目标
我建议您将地图值更改为 List<String>
并重新考虑 getData
的逻辑。
public static void main(String[] args) {
int startLimit = 2;
int endLimit = 4;
HashMap<String, List<String>> finalMap = new HashMap<>();
HashMap<String, List<String>> initialMap = new HashMap<>();
List<String> sList = Arrays.asList("[s1, s2, s3, s4, s5, s6]", "[s7, s8, s9, s10, s11, s12]", "[s13, s14 ,s15, s16, s17, s18]", "[s19, s20, s21, s22, s23, s24]", "[s25, s26, s27, s28, s29, s30]");
List<String> dList = Arrays.asList("[d1, d2, d3, d4, d5, d6]", "[d7, d8, d9, d10, d11, d12]", "[d13, d14 ,d15, d16, d17, d18]", "[d19, d20, d21, d22, d23, d24]", "[d25, d26, d27, d28, d29, d30]");
List<String> wList = Arrays.asList("[w1, w2, w3, w4, w5, w6]", "[w7, w8, w9, w10, w11, w12]", "[w13, w14 ,w15, w16, w17, w18]", "[w19, w20, w21, w22, w23, w24]", "[w25, w26, w27, w28, w29, w30]");
List<String> eList = Arrays.asList("[e1, e2, e3, e4, e5, e6]", "[e7, e8, e9, e10, e11, e12]", "[e13, e14 ,e15, e16, e17, e18]", "[e19, e20, e21, e22, e23, e24]", "[e25, e26, e27, e28, e29, e30]");
initialMap.put("status", sList);
initialMap.put("data", dList);
initialMap.put("warning", wList);
initialMap.put("error", eList);
Iterator<Entry<String, List<String>>> i = initialMap.entrySet().iterator();
Entry<String, List<String>> e = null;
List<String> eValue = null;
while (i.hasNext()) {
e = i.next();
eValue = e.getValue();
finalMap.put(e.getKey(), eValue.subList(startLimit, endLimit));
}
System.out.println("final map after limiting the data " + finalMap);
}
编辑:如果地图值不能是List<String>
你可以按如下方式做
public static void main(String[] args) {
int startLimit = 2;
int endLimit = 4;
String objectSeparator = "],";
HashMap<String, String> finalMap = new HashMap<>();
HashMap<String, String> initialMap = new HashMap<>();
String sList = "[s1, s2, s3, s4, s5, s6],[s7, s8 s9, s10, s11, s12],[s13, s14 ,s15, s16, s17, s18],[s19, s20, s21, s22, s23, s24],[s25, s26, s27, s28, s29, s30]";
String dList = "[[d1, d2, d3, d4, d5, d6],[d7, d8, d9, d10, d11, d12],[d13, d14 ,d15, d16, d17, d18],[d19, d20, d21, d22, d23, d24],[d25, d26, d27, d28, d29, d30]]";
String wList = "[w1, w2, w3, w4, w5, w6],[w7, w8, w9, w10, w11, w12],[w13, w14 ,w15, w16, w17, w18],[w19, w20, w21, w22, w23, w24],[w25, w26, w27, w28, w29, w30]";
String eList = "[e1, e2, e3, e4, e5, e6],[e7, e8, e9, e10, e11, e12],[e13, e14 ,e15, e16, e17, e18],[e19, e20, e21, e22, e23, e24],[e25, e26, e27, e28, e29, e30]";
initialMap.put("status", sList);
initialMap.put("data", dList);
initialMap.put("warning", wList);
initialMap.put("error", eList);
Iterator<Entry<String, String>> it = initialMap.entrySet().iterator();
Entry<String, String> e = null;
String eValue = null;
String[] splitted = null;
while (it.hasNext()) {
e = it.next();
eValue = e.getValue();
// Split each "square bracket object" in a separate string
splitted = eValue.split(objectSeparator);
// Create a new string containing only objects with index between limits
String limited = "";
for(int idx = startLimit-1; idx <= endLimit-1; idx++)
limited += (splitted[idx] + objectSeparator);
// Remove trailing comma added inside for loop
limited = limited.substring(0, limited.lastIndexOf(","));
finalMap.put(e.getKey(), limited);
}
System.out.println("final map after limiting the data " + finalMap);
}
注意: 我假设与 "data" 键关联的值与其他值具有相同的结构(没有额外的括号),如果不是这样的话然后算法变得有点复杂。
我在 JDK 6.
之上使用了 guava and commons-lang3
HashMap<String, Object> finalMap = Maps.newHashMap(Maps.transformValues(initialMap, new Function<Object, Object>() {
public Object apply(Object input) {
String inputAsString = (String) input;
String trimmedInput = inputAsString.replaceAll("^\[+", "").replaceAll("\]+$", "");
int removedBraces = (inputAsString.length() - trimmedInput.length()) / 2;
List<String> splitInput = Lists.newArrayList(Splitter.on("],[").split(trimmedInput));
List<String> subList = splitInput.subList(startLimit - 1, endLimit);
return StringUtils.repeat('[', removedBraces) + Joiner.on("],[").join(subList)
+ StringUtils.repeat(']', removedBraces);
}
}));
简而言之,这段代码创建了一个新地图,其中新地图中的每个值都是通过使用以下算法转换初始值来构建的:
- 去掉开头的“[”和结尾的“]”
- 用“],[”字符串分割
- 获取结果列表的子列表(从 startLimit - 1 到 endLimit)
- 使用“],[”将列表重新组合成一个字符串,向后添加初始前导“[”和尾随“]”
我有一个
HashMap<String, Object> initialMap = new HashMap<String, Object>();
其中我以这种方式设置了键的字符串值,
error=[e1, e2, e3, e4, e5, e6],[e7, e8, e9, e10, e11, e12],[e13, e14 ,e15, e16, e17, e18],[e19, e20, e21, e22, e23, e24],[e25, e26, e27, e28, e29, e30], status=[s1, s2, s3, s4, s5, s6],[s7, s8 s9, s10, s11, s12],[s13, s14 ,s15, s16, s17, s18],[s19, s20, s21, s22, s23, s24],[s25, s26, s27, s28, s29, s30], data=[[d1, d2, d3, d4, d5, d6],[d7, d8, d9, d10, d11, d12],[d13, d14 ,d15, d16, d17, d18],[d19, d20, d21, d22, d23, d24],[d25, d26, d27, d28, d29, d30]], warning=[w1, w2, w3, w4, w5, w6],[w7, w8, w9, w10, w11, w12],[w13, w14 ,w15, w16, w17, w18],[w19, w20, w21, w22, w23, w24],[w25, w26, w27, w28, w29, w30]}
所以,这里有一个名为 error 的键,
下面有这 5 组字符串值。
[e1, e2, e3, e4, e5, e6],[e7, e8, e9, e10, e11, e12],[e13, e14 ,e15, e16, e17, e18],[e19, e20, e21, e22, e23, e24],[e25, e26, e27, e28, e29, e30].
我不需要一个键的所有这 5 个设置值,但我需要使用开始和结束限制,即每个键的值的子集。
假设我的 startLimit=2 和 endLimit=4 那么我应该只得到 2,3,4 组键错误值,
[e7, e8, e9, e10, e11, e12],[e13, e14 ,e15, e16, e17, e18],[e19, e20, e21, e22, e23, e24]
同样需要对所有其他 3 个键重复。
我试过这种方法,但它总是给我一个特定键的所有值。
有什么方法可以获取哈希图中特定键值的子集吗?
这是我试过的代码,
public class MapTest {
HashMap<String, Object> finalMap = new HashMap<String, Object>();
int startLimit = 2;
int endLimit = 4;
public static void main(String[] args) {
MapTest mm = new MapTest();
mm.getData();
}
public void getData()
{
HashMap<String, Object> initialMap = new HashMap<String, Object>();
initialMap.put("status","[s1, s2, s3, s4, s5, s6],[s7, s8 s9, s10, s11, s12],[s13, s14 ,s15, s16, s17, s18],[s19, s20, s21, s22, s23, s24],[s25, s26, s27, s28, s29, s30]");
initialMap.put("data","[[d1, d2, d3, d4, d5, d6],[d7, d8, d9, d10, d11, d12],[d13, d14 ,d15, d16, d17, d18],[d19, d20, d21, d22, d23, d24],[d25, d26, d27, d28, d29, d30]]");
initialMap.put("warning","[w1, w2, w3, w4, w5, w6],[w7, w8, w9, w10, w11, w12],[w13, w14 ,w15, w16, w17, w18],[w19, w20, w21, w22, w23, w24],[w25, w26, w27, w28, w29, w30]");
initialMap.put("error","[e1, e2, e3, e4, e5, e6],[e7, e8, e9, e10, e11, e12],[e13, e14 ,e15, e16, e17, e18],[e19, e20, e21, e22, e23, e24],[e25, e26, e27, e28, e29, e30]");
Set<Entry<String, Object>> entries = initialMap .entrySet();
for (Iterator<Entry<String, Object>> i = entries.iterator(); i.hasNext(); ) {
Entry e = (Entry) i.next();
if(e.getKey().equals("error"))
{
Object errValue = initialMap.get(e.getKey());
//System.out.println("errValue" + errValue);
finalMap.put(e.getKey().toString(), errValue);
}
if(e.getKey().equals("data"))
{
Object errValue = initialMap.get(e.getKey());
//System.out.println("errValue" + errValue);
finalMap.put(e.getKey().toString(), errValue);
}
else if(e.getKey().equals("warning"))
{
Object errValue = initialMap.get(e.getKey());
//System.out.println("errValue" + errValue);
finalMap.put(e.getKey().toString(), errValue);
}
if(e.getKey().equals("status"))
{
Object errValue = initialMap.get(e.getKey());
//System.out.println("errValue" + errValue);
finalMap.put(e.getKey().toString(), errValue);
}
}
if(startLimit == 2 && endLimit == 4)
System.out.println("final map after limiting the data " + finalMap);
}
}
我正在从上面的程序中以这种方式获取一个键的所有 5 对值,
限制数据后的最终地图
{error=[e1, e2, e3, e4, e5, e6],[e7, e8, e9, e10, e11, e12],[e13, e14 ,e15, e16, e17, e18],[e19, e20, e21, e22, e23, e24],[e25, e26, e27, e28, e29, e30], status=[s1, s2, s3, s4, s5, s6],[s7, s8 s9, s10, s11, s12],[s13, s14 ,s15, s16, s17, s18],[s19, s20, s21, s22, s23, s24],[s25, s26, s27, s28, s29, s30], data=[[d1, d2, d3, d4, d5, d6],[d7, d8, d9, d10, d11, d12],[d13, d14 ,d15, d16, d17, d18],[d19, d20, d21, d22, d23, d24],[d25, d26, d27, d28, d29, d30]], warning=[w1, w2, w3, w4, w5, w6],[w7, w8, w9, w10, w11, w12],[w13, w14 ,w15, w16, w17, w18],[w19, w20, w21, w22, w23, w24],[w25, w26, w27, w28, w29, w30]}
但是我需要这样,因为我的限制是 2 到 4 对,
限制数据后的最终地图{error=[e7, e8, e9, e10, e11, e12],[e13, e14 ,e15, e16, e17, e18],[e19, e20, e21, e22, e23, e24], status=[s7, s8 s9, s10, s11, s12],[s13, s14 ,s15, s16, s17, s18],[s19, s20, s21, s22, s23, s24], data=[[d7, d8, d9, d10, d11, d12],[d13, d14 ,d15, d16, d17, d18],[d19, d20, d21, d22, d23, d24]], warning=[w7, w8, w9, w10, w11, w12],[w13, w14 ,w15, w16, w17, w18],[w19, w20, w21, w22, w23, w24]}
谁能帮我解决这个问题?
谢谢
我在您的代码中发现了 2 个大问题:
startLimit
和endLimit
不用于过滤从initialMap
中提取的数据,但它们只是在 i 值上进行测试;您的代码只需将 key/value 从initialMap
复制到finalMap
然后打印finalMap
如果两个限制设置为 2 和 4(它们只是标志来决定是否打印或不是initialMap
中的每个键都与String
(不是Object
或List
)关联,所以我看到的唯一方法是将 [=39= 分开] 正在玩逗号..但我不确定这是否是您代码的目标
我建议您将地图值更改为 List<String>
并重新考虑 getData
的逻辑。
public static void main(String[] args) {
int startLimit = 2;
int endLimit = 4;
HashMap<String, List<String>> finalMap = new HashMap<>();
HashMap<String, List<String>> initialMap = new HashMap<>();
List<String> sList = Arrays.asList("[s1, s2, s3, s4, s5, s6]", "[s7, s8, s9, s10, s11, s12]", "[s13, s14 ,s15, s16, s17, s18]", "[s19, s20, s21, s22, s23, s24]", "[s25, s26, s27, s28, s29, s30]");
List<String> dList = Arrays.asList("[d1, d2, d3, d4, d5, d6]", "[d7, d8, d9, d10, d11, d12]", "[d13, d14 ,d15, d16, d17, d18]", "[d19, d20, d21, d22, d23, d24]", "[d25, d26, d27, d28, d29, d30]");
List<String> wList = Arrays.asList("[w1, w2, w3, w4, w5, w6]", "[w7, w8, w9, w10, w11, w12]", "[w13, w14 ,w15, w16, w17, w18]", "[w19, w20, w21, w22, w23, w24]", "[w25, w26, w27, w28, w29, w30]");
List<String> eList = Arrays.asList("[e1, e2, e3, e4, e5, e6]", "[e7, e8, e9, e10, e11, e12]", "[e13, e14 ,e15, e16, e17, e18]", "[e19, e20, e21, e22, e23, e24]", "[e25, e26, e27, e28, e29, e30]");
initialMap.put("status", sList);
initialMap.put("data", dList);
initialMap.put("warning", wList);
initialMap.put("error", eList);
Iterator<Entry<String, List<String>>> i = initialMap.entrySet().iterator();
Entry<String, List<String>> e = null;
List<String> eValue = null;
while (i.hasNext()) {
e = i.next();
eValue = e.getValue();
finalMap.put(e.getKey(), eValue.subList(startLimit, endLimit));
}
System.out.println("final map after limiting the data " + finalMap);
}
编辑:如果地图值不能是List<String>
你可以按如下方式做
public static void main(String[] args) {
int startLimit = 2;
int endLimit = 4;
String objectSeparator = "],";
HashMap<String, String> finalMap = new HashMap<>();
HashMap<String, String> initialMap = new HashMap<>();
String sList = "[s1, s2, s3, s4, s5, s6],[s7, s8 s9, s10, s11, s12],[s13, s14 ,s15, s16, s17, s18],[s19, s20, s21, s22, s23, s24],[s25, s26, s27, s28, s29, s30]";
String dList = "[[d1, d2, d3, d4, d5, d6],[d7, d8, d9, d10, d11, d12],[d13, d14 ,d15, d16, d17, d18],[d19, d20, d21, d22, d23, d24],[d25, d26, d27, d28, d29, d30]]";
String wList = "[w1, w2, w3, w4, w5, w6],[w7, w8, w9, w10, w11, w12],[w13, w14 ,w15, w16, w17, w18],[w19, w20, w21, w22, w23, w24],[w25, w26, w27, w28, w29, w30]";
String eList = "[e1, e2, e3, e4, e5, e6],[e7, e8, e9, e10, e11, e12],[e13, e14 ,e15, e16, e17, e18],[e19, e20, e21, e22, e23, e24],[e25, e26, e27, e28, e29, e30]";
initialMap.put("status", sList);
initialMap.put("data", dList);
initialMap.put("warning", wList);
initialMap.put("error", eList);
Iterator<Entry<String, String>> it = initialMap.entrySet().iterator();
Entry<String, String> e = null;
String eValue = null;
String[] splitted = null;
while (it.hasNext()) {
e = it.next();
eValue = e.getValue();
// Split each "square bracket object" in a separate string
splitted = eValue.split(objectSeparator);
// Create a new string containing only objects with index between limits
String limited = "";
for(int idx = startLimit-1; idx <= endLimit-1; idx++)
limited += (splitted[idx] + objectSeparator);
// Remove trailing comma added inside for loop
limited = limited.substring(0, limited.lastIndexOf(","));
finalMap.put(e.getKey(), limited);
}
System.out.println("final map after limiting the data " + finalMap);
}
注意: 我假设与 "data" 键关联的值与其他值具有相同的结构(没有额外的括号),如果不是这样的话然后算法变得有点复杂。
我在 JDK 6.
之上使用了 guava and commons-lang3HashMap<String, Object> finalMap = Maps.newHashMap(Maps.transformValues(initialMap, new Function<Object, Object>() {
public Object apply(Object input) {
String inputAsString = (String) input;
String trimmedInput = inputAsString.replaceAll("^\[+", "").replaceAll("\]+$", "");
int removedBraces = (inputAsString.length() - trimmedInput.length()) / 2;
List<String> splitInput = Lists.newArrayList(Splitter.on("],[").split(trimmedInput));
List<String> subList = splitInput.subList(startLimit - 1, endLimit);
return StringUtils.repeat('[', removedBraces) + Joiner.on("],[").join(subList)
+ StringUtils.repeat(']', removedBraces);
}
}));
简而言之,这段代码创建了一个新地图,其中新地图中的每个值都是通过使用以下算法转换初始值来构建的:
- 去掉开头的“[”和结尾的“]”
- 用“],[”字符串分割
- 获取结果列表的子列表(从 startLimit - 1 到 endLimit)
- 使用“],[”将列表重新组合成一个字符串,向后添加初始前导“[”和尾随“]”