在java中创建一个字符串列表,其中每个字符串是一个键,它是一个元组列表(作为值)

时间:2013-04-23 12:09:54

标签: java list data-structures map hashmap

如何创建字符串列表,其中每个字符串指向元组列表;换句话说,每个字符串是元组列表的键(作为值)?

每个元组的格式应为:

List<String> pref, where each element of the list pref (say pref_i):
pref1 --> {(T1:10),(T2:13), T3:3),...}
pref2 --> {(T1:7), (T4:3), (T5:1),...}
pref3 --> ...

4 个答案:

答案 0 :(得分:3)

在我看来你想要一个Map,具体取决于你的元组类型(假设这里有一个字符串):

Map<String, List<String>> prefs = new HashMap<String, List<String>>;

你可以让你的元组属于任何类型,为方便起见,我使用了一个字符串。

如果通过SetMap更好地表示您的元组,请相应更改。

答案 1 :(得分:1)

MultiMap救援!

除非你模拟它们,否则java中没有touples这样的东西。 multimap就像有一个Map,其中key是String,Value是一个元素数组

MultiMap

答案 2 :(得分:0)

也许你可以使用HashMap的HashMap?第一个HashMap使用你的字符串键,第二个使用你的元组。

HashMap<String, HashMap<String, String>> myHashMap = new HashMap<String, HashMap<String, String>>();

希望这有帮助!再见!

答案 3 :(得分:0)

您也可以使用List。

import java.util.ArrayList;
    import java.util.List;
    public class TestArrayList {
        public static void main(String[] args) {

            List<String> tempList=new ArrayList<String>();
            List<String> temp1=new ArrayList<String>();
            temp1.add(0, "a");
            temp1.add(1, "b");
            List<String> temp2=new ArrayList<String>();
            temp2.add(0, "c");
            temp2.add(1, "d");
            List<String> temp3=new ArrayList<String>();
            temp3.add(0, "e");
            temp3.add(1, "f");
            tempList.addAll(0, temp1);
            tempList.addAll(1, temp2);
            tempList.addAll(1, temp3);

            for(int i=0;i<tempList.size();i++){
                System.out.println(">>"+tempList.get(i));
            }
        }
    }