简体   繁体   中英

Comparing Keys in a Hashmap

I have a test.csv file that is formatted as:

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.

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

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

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

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 . The second file is supposed to contain the values and hence you named the corresponding hash map 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 .

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). Wrap it inside an if conditional and check of the headerRecord(i) even exists in the containers hash map.

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

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.

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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