简体   繁体   English

比较哈希图中的键

[英]Comparing Keys in a Hashmap

I have a test.csv file that is formatted as: 我有一个test.csv文件,其格式为:

Home,Owner,Lat,Long
5th Street,John,5.6765,-6.56464564
7th Street,Bob,7.75,-4.4534564
9th Street,Kyle,4.64,-9.566467364
10th Street,Jim,14.234,-2.5667564

I have a hashmap that reads a file that contains the same header contents such as the CSV, just a different format, with no accompanying data. 我有一个哈希图,该哈希图读取的文件包含相同的标头内容(例如CSV),只是格式不同,没有附带数据。

In example: 例如:

Map<Integer, String> container = new HashMap<>();

where, 哪里,

Key, Value 核心价值

[0][NULL]
[1][Owner]
[2][Lat]
[3][NULL]

I have also created a second hash map that: 我还创建了第二个哈希图,该哈希图:

BufferedReader reader = new BufferedReader (new FileReader("test.csv"));
CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT);
Boolean headerParsed = false;
CSVRecord headerRecord = null;
int i;
Map<String,String> value = new HashMap<>();
for (final CSVRecord record : parser) {
    if (!headerParsed = false) {
     headerRecord = record;
     headerParsed = true;
    }
    for (i =0; i< record.size(); i++) {
        value.put (headerRecord.get(0), record.get(0));
    }
}

I want to read and compare the hashmap, if the container map has a value that is in the value map, then I put that value in to a corresponding object. 我想阅读并比较哈希图,如果容器映射具有值映射中的值,则将该值放入相应的对象中。

example object 示例对象

public DataSet (//args) {
    this.home
    this.owner
    this.lat
    this.longitude
}

I want to create a function where the data is set inside the object when the hashmaps are compared and when a value map key is equal to a contain map key, and the value is placed is set into the object. 我想创建一个函数,在比较哈希图时,当值映射键等于包含映射键,并且将值放置到对象中时,将数据设置在对象内部。 Something really simply that is efficient at handling the setting as well. 确实很简单的方法也可以有效地处理设置。

Please note: I made the CSV header and the rows finite, in real life, the CSV could have x number of fields(Home,Owner,Lat,Long,houseType,houseColor, ect..), and an number of values associated to those fields 请注意:在现实生活中,我使CSV标头和行是有限的,CSV可能具有x个字段(Home,Owner,Lat,Long,houseType,houseColor等),以及与那些领域

First off, your approach to this problem is too unnecessarily long. 首先,您解决此问题的方法太长了。 From what I see, all you are trying to do is this: 从我看来,您要做的就是:

Select a two columns from a CSV file, and add them to a data structure. 从CSV文件中选择列,然后将它们添加到数据结构中。 I highlighted the word two because in a map, you have a key and a value. 我强调了单词“ 二”,因为在地图中,您有一个键和一个值。 One column becomes the key, and the other becomes the value. 一栏成为键,另一栏成为值。

What you should do instead: 您应该怎么做:

  1. Import the names of columns you wish to add to the data structure into two strings. 将要添加到数据结构中的列名称导入两个字符串。 (You may read them from a file). (您可以从文件中读取它们)。

  2. Iterate over the CSV file using the CSVParser class that you did. 使用您执行的CSVParserCSVParser CSV文件。

  3. Store the value corresponding to the first desired column in a string, repeat with the value corresponding to the second desired column, and push them both into a DataSet object, and push the DataSet object into a List<DataSet> . 将对应于第一所需列的值存储在字符串中,重复对应于第二所需列的值,然后将它们都推入DataSet对象,然后将DataSet对象推入List<DataSet>

If you prefer to stick to your way of solving the problem: 如果您喜欢坚持解决问题的方式:

Basically, the empty file is supposed to hold just the headers (column names), and that's why you named the corresponding hash map containers . 基本上,空文件应该只包含标题(列名),这就是为什么命名相应的哈希映射containers The second file is supposed to contain the values and hence you named the corresponding hash map values . 第二个文件应该包含这些值,因此您已命名了相应的哈希图values

First off, where you say 首先,你在哪里说

if (!headerParsed = false) {
    headerRecord = record;
    headerParsed = true;
}

you probably mean to say 你可能想说

if (!headerParsed) {
    headerRecord = record;
    headerParsed = true;
}

and where you say 你在哪里说

for (i =0; i< record.size(); i++) {
    value.put(headerRecord.get(0), record.get(0));
}

you probably mean 你可能是说

for (i =0; i< record.size(); i++) {
    value.put(headerRecord.get(i), record.get(i));
}

ie You iterate over one record and store the value corresponding to each column . 即,您遍历一条记录并存储与每一column相对应的value

Now I haven't tried this code on my desktop, but since the for loop also iterates over Home and Longitude, I think it should create an error and you should add an extra check before calling value.put (ie value.put("Home", "5th Street") should create an error I suppose). 现在,我还没有在桌面上尝试此代码,但是由于for循环还会在Home和Longitude上进行迭代,因此我认为它应该会产生错误,并且在调用value.put (即value.put("Home", "5th Street")应该会产生一个错误(我想)。 Wrap it inside an if conditional and check of the headerRecord(i) even exists in the containers hash map. if有条件则将其包装,并检查headerRecord(i)甚至存在于容器哈希图中。

for (i =0; i< record.size(); i++) {
    if (container[headerRecord.get(i)] != NULL) {
        value.put(headerRecord.get(i), record.get(i));
    }
}

Now thing is, that the data structure itself depends on which values from the containers hash map you want to store. 现在的事情是,数据结构本身取决于您要存储的容器哈希映射中的哪些值。 It could be Home and Lat, or Owner and Long. 它可以是Home and Lat,也可以是Owner and Long。 So we are stuck. 所以我们被困住了。 How about you create a data structure like below: 如何创建如下所示的数据结构:

struct DataSet {
    string val1;
    string val2;
}

Also, note that this DataSet is only for storing ONE row. 另外,请注意,此数据集仅用于存储一行。 For storing information from multiple rows, you need to create a Linked List of DataSet . 为了存储多行中的信息,您需要创建一个DataSetLinked List

Lastly, the container file contains ALL the column names. 最后,容器文件包含所有列名称。 Not all these columns will be stored in the Data Set (ie You chose to NULL Home and Long. You could have chosen to NULL Owner and Lat), hence the header file is not what you need to make this decision. 并非所有这些列都将存储在数据集中(即,您选择了NULL Home和Long。您可能选择了NULL Owner和Lat),因此,头文件不是您做出此决定所需要的。

If you think about it, just iterate over the values hash map and store the first value in string val1 and the second value in val2 . 如果您考虑一下,只需遍历值哈希映射并将第一个值存储在字符串val1 ,将第二个值存储在val2

List<DataSet> myList;
DataSet row;

Iterator it = values.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry pair = (Map.Entry)it.next();

    row.val1 = pair.getKey();
    row.val2 = pair.getValue();
    myList.add(row);
    it.remove();
}

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM