【问题标题】:Creating a list of strings where each string, is a key, to a list of tuples (as value), in java在java中创建一个字符串列表,其中每个字符串都是一个键,到一个元组列表(作为值)
【发布时间】:2013-04-23 12:09:54
【问题描述】:

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

每个元组应采用以下形式:

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 --> ...

【问题讨论】:

    标签: java list data-structures map hashmap


    【解决方案1】:

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

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

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

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

    【讨论】:

      【解决方案2】:

      MultiMap 助你一臂之力!

      在java中没有touples这样的东西,除非你模拟它们。 多重映射就像有一个映射,其中键是字符串,值是元素数组

      MultiMap

      【讨论】:

        【解决方案3】:

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

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

        希望这会有所帮助!再见!

        【讨论】:

          【解决方案4】:

          你也可以使用 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));
                      }
                  }
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-07-24
            • 1970-01-01
            • 2020-07-29
            • 1970-01-01
            • 2019-09-04
            • 1970-01-01
            • 1970-01-01
            • 2017-02-13
            相关资源
            最近更新 更多