我想一個錶轉換兩個字符串的Java轉換兩個字符串的表到地圖<字符串,ArrayList的<String>>
到地圖<「字符串,ArrayList的<」字符串>>
的例如: 我得到這樣
hello world
hi mom
hi doug
hello dad
我想在地圖將這一這樣
表個非常感謝我disapointed ^^
我想一個錶轉換兩個字符串的Java轉換兩個字符串的表到地圖<字符串,ArrayList的<String>>
到地圖<「字符串,ArrayList的<」字符串>>
的例如: 我得到這樣
hello world
hi mom
hi doug
hello dad
我想在地圖將這一這樣
表個非常感謝我disapointed ^^
Map<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
// Loop through the table and assign the
// key/value to be checked and added to your map
...
if(map.containsKey(key))
{
map.get(key).add(value);
}
else
{
ArrayList<String> newValueList = new ArrayList<String>();
newValueList.add(value);
map.put(key, newValueList);
}
非常感謝這正是我所需要的,也感謝其他人:) –
幫助:) 初始化Map<String, List<String>>
,稱之爲map
循環在你的表的k
,v
條目和...
如果k
已經不是地圖中的關鍵字,則添加一個e n請映射k
含v
如果k
一個新的列表是一個關鍵,加v
到列表map.get(k)
+1這是對這個問題的合理答案。 –
你需要的是這樣的;
class Test{
public static List<String> al1 = new ArrayList<String>();
public static HashMap<String, List<String>> ohm = new HashMap<String, List<String>>();
public static void main(String[] args){
String insertStr = "Hello";
ohm.put(insertStr, al1);
if(ohm.containsKey(insertStr)) // DO SOME OPERATIONS HERE
{
ohm.get(insertStr).add("MOM");
ohm.get(insertStr).add("World");
ohm.get(insertStr).add("DAD");
System.out.println(ohm.get(insertStr));
}
}
}
輸出: [MOM,世界,DAD]
讓我知道,如果它幫助,或者您有任何疑問。
謝謝;)這也有幫助! ^^ –
package stackoverflow;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MapExample
{
private static final String[] data = new String[] { "hello world", "hello dad", "hello mom", "hi mom", "hi doug" };
public static void main(String[] args)
{
Map<String, List<String>> dataMap = new HashMap<String, List<String>>();
// Adjust the following as appropriate for actual data input and format
for(String s : data)
{
String[] splitData = s.split(" ");
String key = splitData[0];
String newValue = splitData[1];
List<String> values = dataMap.get(key);
if(values == null)
{
values = new ArrayList<>();
}
values.add(newValue);
dataMap.put(key, values);
}
for(String key : dataMap.keySet())
{
System.out.print(key + ", { ");
for(String value : dataMap.get(key))
{
System.out.print(value + " ");
}
System.out.println("}");
}
}
}
輸出:
hello, { world dad mom }
hi, { mom doug }
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class TableToMap {
private static String[] table = new String[] {"hello world", "hello mom", "hello dad"};
private static Map <String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
public static void main(String[] args) {
for (String row : table) {
String[] splitString = row.split(" ");
if (map.containsKey(splitString[0])) {
map.get(splitString[0]).add(splitString[1]);
}
else {
ArrayList<String> newList = new ArrayList<String>();
newList.add(splitString[1]);
map.put(splitString[0], newList);
}
}
}
}
Java並沒有內置多重映射功能,這是你想要的這裏。您可以創建一個自定義地圖,當您添加一個關鍵值集(如{"hello", "world"}
)時,它會檢查是否先有一個「hello」鍵。如果沒有「hello」鍵,則會添加新的{hello, List<String>}
,然後將「world」添加到映射列表中。否則,它只會將「世界」添加到映射到「hello」的列表中。
您可以瞭解如何使用地圖,甚至如何從獲得mutlimap功能的詳細信息:
http://docs.oracle.com/javase/tutorial/collections/interfaces/map.html
一個'地圖<字符串,String'> CANT包含'{你好,世界},{你好,爸爸},{你好,媽媽}'..鍵互相重疊。向我們展示你的嘗試? – sanbhat
它是'地圖'而不是'地圖'。 –
A地圖是對的集合。你的用例不清楚。 –
rocketboy