0

I have a csv file of 2 column. I`m trying to create a hash table for each dimension - only add a value if I haven't seen it before. I want to create 2 separate hash table for every column. columns contain string and numeric value. From the class definition i found containsKey(Object key) methoid Tests if the specified object is a key in this hashtable. i can explain a bit detail like my csv file may look like as below

New York, 50 Sydney, jessi california, 10 New York, 10 

so for column 1 New york came 2 in hash table i`d like to put key New York and value 2

can anyone help me how can i create a hash table like this way using java hashtable class, or maintain a separate array

4
  • 1
    Why does "New York" become "2"? Commented May 23, 2012 at 22:18
  • 7
    I suggest you actually read the documentation on HashTable (and classes it descends from). This is not a code-generation site, you know. Commented May 23, 2012 at 22:18
  • How much data do you actually have? Do you really need a Hashtable? have you looked at using a simple Map? Commented May 23, 2012 at 22:19
  • 2
    I would recommend HashMap for a standard "go to" implementation, because being newer is cooler ;-) (Although HashTable has been retrofitted to Map.) Commented May 23, 2012 at 22:20

1 Answer 1

1

Try this open source project on SourceForge called OpenCSV.

Then you could code something like this to read the CSV into your Map.

try { CSVReader reader = new CSVReader(new InputStreamReader(new FileInputStream(new File("/path/to/your/file.csv")))); Map<String, String> result = new HashMap<String, String>(); for(String[] row : reader.readAll()) { result.put(row[0], row[1]); } } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } 

You can read more on the OpenCSV documentation here.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot for your response , i'm reading hash map , i'm new of using hashtable and hashmap,...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.