【问题标题】:Java copy into a new list all lists (values) of Map<Integer, List<String>>Java 将 Map<Integer, List<String>> 的所有列表(值)复制到一个新列表中
【发布时间】:2014-12-09 17:07:33
【问题描述】:

我对@9​​87654322@ 有疑问。 假设我们有以下代码:

public class HashMaps {
  public static void main(String[] ar){
    List<String> list = new ArrayList<>();
    Map<Integer, List<String>> myMap = new HashMap<>();
    list.add("one");
    list.add("two");
    list.add("three");
    list.add("four");
    List cloneList = ((List) ((ArrayList) list).clone());
    myMap.put(0, cloneList);
    list.clear();
    list.add("un");
    list.add("deux");
    list.add("trois");
    list.add("quatre");
    cloneList = ((List) ((ArrayList) list).clone());
    myMap.put(1, cloneList);
    System.out.println("map : "+myMap);
    System.out.println("keys : "+myMap.keySet());
    System.out.println("values : "+myMap.values());
  }
}

我怎样才能进入myMap 的新List&lt;String&gt; 所有值(Lists)?

基本上,我有如下列表:

["one"]
["two"]
["three"]
["four"]

又想得到:

["one","two","three","four"]

【问题讨论】:

  • 你的意思是,打印它们每行一个值,而不是标准toString()列表方法的格式?下面没有实际的格式,toString() 方法决定像这样打印它们。您可以随意打印它们
  • 这段代码中有某种“克隆人的攻击”:)
  • 不要使用原始类型。
  • @VassilisDe 看看我的回答,你是这个意思吗?

标签: java list dictionary data-structures


【解决方案1】:

根据您的编辑,您似乎希望获取地图中的每个单独列表并将它们组合成一个列表:

List<String> combined = new ArrayList<String>();
for(List<String> list : myMap.values()) {
    combined.addAll(list);
}
// combined holds all lists into a single list

编辑(根据您的评论问题)

如果您只想添加某些列表,而不是所有列表,并且您决定哪些列表是 Map 中列表的索引的方式,那么您可以这样做。

myMap.values()返回的Collection转换为List,然后使用list.get(index)方法在允许的索引处添加所有值。

List<String> selectedLists = new ArrayList<String>();
int[] allowedIndexes = {0, 1, 2, 3};                                          // only lists at indexes 0, 1, 2 and 3 are inserted into combined list
List<List<String>> listOfLists = new ArrayList<List<String>>(myMap.values()); // convert Collection to List
for(int i = 0; i < allowedIndexes.length; i++) {                              // if allowed index is within the bounds of the amount of lists, add the list at that index to selected lists
    int index = allowedIndexes[i];
    if(index < listOfLists.size()) {
        selectedLists.addAll(listOfLists.get(index));
    }
}
// selectedLists now contains only lists from the allowed indexes

另一种方法是创建一个允许索引的数组并遍历映射中的所有值,只添加与允许索引对应的值。

要确定当前索引是否被允许,您可以创建一个辅助方法,该方法将遍历所有允许的索引并检查当前索引是否作为允许的索引存在。

辅助方法:

// check if element exists in the array
boolean isInArray(int[] array, int element) {
    for(int i = 0; i < array.length; i++) {
        if(array[i] == element) {
            return true;
        }
    }
    return false;
}

解决方案:

List<String> selectedLists = new ArrayList<String>();
int[] allowedIndexes = {0, 1, 2, 3};       // only lists at indexes 0, 1, 2 and 3 are inserted into combined list
int index = 0;                             // current index counter
for(List<String> list: myMap.values()) {   // iterate over all existing lists
    if(isInArray(allowedIndexes, index)) { // if the list at current index is allowed, add it to the selected lists
        selectedLists.addAll(list);
    }
    index++;                               // update the index
}
// selectedLists now contains only lists from the allowed indexes

假设您保持允许的索引数组排序,您可以改进辅助方法的一种方法是使用Binary Search 来检查索引是否存在,这将通过对数因子减少搜索时间:

boolean isInArrayImproved(int[] array, int element) {
    if(Arrays.binarySearch(array, element) >= 0) {
        return true;
    }
    return false;
}

【讨论】:

  • 是的!这正是我想要的!非常感谢!
  • @mem 是否有可能仅将那些引用 map.values() 的“第一个位置”的对象添加到组合中?像 map.values.get(0) 之类的东西,然后将这些元素放入组合列表中?所以 combineList 有一个、两个、三个、四个而不是全部
  • 没问题,很高兴为您提供帮助
【解决方案2】:

虽然我个人不推荐,但你可以简单的Override你的toString方法。

hack 是这样的:

List<String> list = new ArrayList<String>(){

@Override
 public String toString(){
  String s="";
  for(String string : this){
    s= s + "\""+ string "\"\n";
  }
  return s;
 }
};

正如人们所说,更好的方法是通过每个元素 iterate 并将它们打印出来。

【讨论】:

猜你喜欢
  • 2015-11-03
  • 1970-01-01
  • 2023-03-26
  • 2017-07-18
  • 1970-01-01
  • 2017-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多