gpt4 book ai didi

java - 在嵌套 HashMap 中搜索字符串数组项

转载 作者:行者123 更新时间:2023-12-01 12:56:30 25 4
gpt4 key购买 nike

我有一个嵌套的 HashMap (outer_map),其中有另一个 HashMap 作为值 (inner_map),实现如下

Map<String, HashMap<String, String>> outer_map = new HashMap<String, HashMap<String, String>>();
Map<String, String> inner_map = new HashMap<String, String>();

下图展示了 map 的整体结构: enter image description here长话短说,我需要通过字符串数组比较和搜索 outer_map 的 vlaue (inner_map) 内的值 项目,然后生成另一个字符串数组来添加匹配的项目。

If the String Array has there elements which are the same as one of the inner_map's random (for example; value2, value1, and value7) values, how can I search and compare these items to add to another String Array?

我尝试过但无法成功的最新代码片段:

if( !( theStringArray.equals("") ) )
{
while( outer_map.keySet().iterator().hasNext() )
{
for( int i=0; i <= theStringArray.length; i++)
{
// outer_map keys are order as 1,2,3,..,8
theStringArray[i] = outer_map.get(String.valueOf(i+1)).get("key1");
...
}
}
}

EDIT: map 生成函数

private void parse(String in) throws IOException
{
reader = new JsonReader(new StringReader(in));
...
int nodeCounter = 1;
while(reader.hasNext())
{
...
String nameAsKey1 = "blabla"; // value1
inner_map.put("name", nameAsKey1);

String surnameAsKey2 = "blabla"; // value2
inner_map.put("surname", surnameAsKey2);
...

outer_map.put(String.valueOf(nodeCounter), (HashMap<String, String>) inner_map);
inner_map = new HashMap<String, String>();
nodeCounter++;
}
}

编辑:我不知道如何更清楚地解释这个问题,但这可能有助于理解它: map 结构

enter image description here

enter image description here

最佳答案

我假设你有一个字符串数组和一个 map 映射。现在您想要根据字符串数组搜索内部映射的值字段,并创建一个具有匹配值的新字符串数组。

如果是这种情况,下面的程序将为您提供帮助..

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class InnerMapSearch {

public static void main(String[] args) {
Map<String, HashMap<String, String>> outer_map = new HashMap<String, HashMap<String, String>>();
Map<String, String> inner_map = new HashMap<String, String>();

String[] searchParams = {"blabla1", "blabla3", "blabla20"};

//Populating the map
int reader = 1;
while (reader < 10) {
String nameAsKey1 = "blabla" + reader; // value1
inner_map.put("name", nameAsKey1);

String surnameAsKey2 = "blabla" + reader; // value2
inner_map.put("surname", surnameAsKey2);

outer_map.put(String.valueOf(reader), (HashMap<String, String>) inner_map);
inner_map = new HashMap<String, String>();
reader++;
}

//Searching
Set<String> searchResults = new HashSet<String>(); // Using set to avoid duplicate

// Iterate over the outer map
for(String key : outer_map.keySet()){
// Iterate through each inner_map value of outer map
for(Entry<String, String> innerEntry : outer_map.get(key).entrySet()){
// Iterate through the list of search params and see if its present in inner_hashmap
for(String searchParam : searchParams){
if(searchParam.equals(innerEntry.getValue())){
// The search parameter is in inner map so adding to result.
searchResults.add(searchParam);
}
}
}
}

// Converting the list to an array.
String[] searchResultsArray = searchResults.toArray(new String[searchResults.size()]);
}

}

关于java - 在嵌套 HashMap 中搜索字符串数组项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23842117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com