1

.txt通过以下方式有 5L 值(行)的文件,并且还有分区大小50000

 1
 3
-1546.9
-67.90
3456
.
.
.

通过以下示例,您可以理解分区在我看来是什么意思。

将文件数据导入列表后,我们可以通过以下方式看到。

 importingdata={1,2,3,4,5,.........500000};

分区后,可以通过以下方式看到。

PartitionList={{1,2,3,...50000},{50001,....,100000},{100001,......,150000},...{450000,.....500000}};

partitionList 数据类型是ArrayList<HashMap<Integer,Double>>.it 表示 partitionlist 的所有子列表都是HashMap<Integer,Double>

所有列表都按以下方式HashMap从 .like 开始其键值。1 to 50000

     PartitionList={{1->1,2->2,3->3,...,50000->50000},{1->50001,.....,50000->100000},{1->100001,....,50000->150000},...,{1->450000,.....,50000->500000}};

我想在文件导入时以上述方式排列文件数据。

为此,我尝试了示例代码,但它不起作用。

public static void main(String[] args) {
    ArrayList<HashMap<Integer, Double>> finalList=new ArrayList<>();
    HashMap<Integer, Double> hash1=new HashMap<>();
    hash1.put(1, 1.0);
    hash1.put(2, 2.0);
    hash1.put(3, 3.0);
    finalList.add(hash1);
    System.out.println(finalList.size());
    System.out.println(hash1.size());
    hash1.clear();
    System.out.println(hash1.size());
    hash1.put(1, 1.0);
    hash1.put(2, 2.0);
    hash1.put(3, 3.0);
    finalList.add(hash1);
    System.out.println(finalList.size());
    System.out.println(hash1.size());
    hash1.clear();
    System.out.println(hash1.size());
    HashMap<Integer, Double> hash2=finalList.get(1);
    HashMap<Integer, Double> hash3=finalList.get(2);
    System.out.println(hash2.size());
    System.out.println(hash3.size());
}

我希望,你们明白我在尝试什么。在这里我提到5L了线条,但在我的真实情况下,我正在处理80L所以建议我使用优化的代码。

谢谢

4

3 回答 3

7

HashMap 是可变的!hashMap 引用仍然相同。当你这样做时:

hash1.clear();

您清除原始地图实例。这意味着,您放入列表的地图实例将被清除。

你应该做

hash1 = new HashMap<Integer, Double>();

反而。这会更新变量对 HashMap()新实例的引用。

于 2013-10-02T06:39:52.347 回答
1
public static void main(String[] args) {
    List<Map<Integer, Double>> finalList=new ArrayList<Map<Integer, Double>>();
    Map<Integer, Double> hash1=new HashMap<Integer, Double>();
    hash1.put(1, 1.0);
    hash1.put(2, 2.0);
    hash1.put(3, 3.0);
    finalList.add(hash1);
    System.out.println(finalList.size());
    System.out.println(hash1.size());
    hash1 = new HashMap<Integer, Double>();
    System.out.println(hash1.size());
    hash1.put(1, 1.0);
    hash1.put(2, 2.0);
    hash1.put(3, 3.0);
    finalList.add(hash1);
    System.out.println(finalList.size());
    System.out.println(hash1.size());
    // and so on
}
于 2013-10-02T06:46:06.477 回答
0

您的示例代码存在一些问题。

  1. 将 50000 个条目添加到其中后,您需要创建哈希图的新实例。打电话明确不会帮助你的情况。
  2. 由于您在创建哈希映射时非常了解哈希映射的大小,因此请确保传递初始大小。提供初始大小可防止影响性能的频繁重新散列

HashMap<Integer, Double> hash1=new HashMap<Integer,Double>(50000);

  1. 我不知道它是否是错字。在您尝试调用 get(1) 的示例代码中,列表索引从 0 开始,而不是从 1 开始。

对于您的实际问题,您应该查看 Google Collections2 API。请在下面找到可以帮助您实现最终目标的代码

// Read all 80Lac lines
    List<String> allLines = Files.readAllLines(
            new File("d:/input.txt").toPath(), Charset.defaultCharset());       

    // Partition the 80L records into lists of 50000
    List<List<String>> partitionedLists = Lists.partition(allLines, 50000);

    ArrayList<ListMultimap<Integer, String>> finalList = new ArrayList<ListMultimap<Integer,String>>();
    for(final List<String> item: partitionedLists){         
        ListMultimap<Integer, String> finalMap = Multimaps.index(item.iterator(), new Function<String,Integer>(){               
            @Override
            public Integer apply(String arg0) {
                return item.indexOf(arg0);                                  
            }

        });

        finalList.add(finalMap);
    }

即使我是 Collections API 的新手,但我测试了上面的代码,它确实创建了一个以键为索引的地图列表。唯一的问题是 MultiMaps 通常用于实现 GROUP BY 类型的操作,因此重复值被组合在一起。我正在研究它,但同时你可以使用上面的代码开始你的实现。

于 2013-10-02T06:47:36.503 回答