2 つの文字列のテーブルを変換したい
MAP<'String, ArrayList<'String>> に
例:このようなテーブルを取得します
hello world
hi mom
hi doug
hello dad
これをこのような地図に変換したい
hello, {world,dad,mom}
hi, {mom,doug}
助けてくれてありがとう:)私は失望しています^^
2 つの文字列のテーブルを変換したい
MAP<'String, ArrayList<'String>> に
例:このようなテーブルを取得します
hello world
hi mom
hi doug
hello dad
これをこのような地図に変換したい
hello, {world,dad,mom}
hi, {mom,doug}
助けてくれてありがとう:)私は失望しています^^
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);
}
必要なのはこれです。
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));
}
}
}
出力: [お母さん、世界、お父さん]
それが役に立ったかどうか、または質問があるかどうか教えてください。
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);
}
}
}
}