Saurabh Saxena
Saurabh Saxena

Reputation: 3205

Need to parse data in a Hashtable

I have data in a hashtable in following way :

key : year    value : 2011  2011
key : title   value : Almayer's Folly     Faust

I have two keys and values separated by tab spaces.

I need to have my output in following manner.

Year     Title
2011     Almayer's Folly
2011     Faust

How could i parse the data in hashtable in the required manner ??

Upvotes: 0

Views: 2706

Answers (4)

nidomiro
nidomiro

Reputation: 830

This works for me but you have to have a title for each year and a year for each title

Hashtable<String, String> map = new Hashtable<String, String>();
    map.put("Year", "2011   2011");
    map.put("Title", "Almayer's Folly   Faust");

    String[] rows = null;

    for (String key : map.keySet()) {

        String value = map.get(key);

        String[] elements = value.split("\t");

        if (rows == null) {
            rows = new String[elements.length + 1]; // Element rows + Title row
            for (int i = 0; i < rows.length; i++) {
                rows[i] = "";
            }
        }
        String prefix = "";
        if (!rows[0].equals("")) { // On first append no tab
            prefix = "\t";
        }

        rows[0] += prefix + key; // append Title

        for (int i = 1; i < rows.length; i++) {
            rows[i] += prefix + elements[i - 1]; // Append Data 
        }

    }

    for (String row : rows) {
        System.out.println(row);

    }

Output:

Year    Title
2011    Almayer's Folly
2011    Faust

Upvotes: 1

twmb
twmb

Reputation: 1810

Your approach at storing the data is terrible. In your table, you basically show that you are going to be using 2011 as your key... so do it!

Another problem is that you should not strings with tabs in a map to delimit different data. An easier way that is more logical is to store tab-delimited data is in a list, without the tabs.

Here's what you are trying to do, showing that you can add stuff to a list before or after you add it to the map.

    HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
    ArrayList<String> titles2011 = new ArrayList<String>();
    titles2011.add("Almayer's Folly");
    map.put("2011", titles2011);
    map.get("2011").add("Faust");

    System.out.println("Year\tTitle");
    for (String year : map.keySet()) {
        for (String title : map.get(year)) { 
            System.out.println(year+"\t"+title);
        }
    }

I also recommend reading the beginnings of java.util.map and java.util.HashMap. As well as looking up how and what a hash table is for, and more specifically, how to deal with collisions (which you will have, once you learn how to properly use a hash table).

Upvotes: 0

Captain Giraffe
Captain Giraffe

Reputation: 14705

First, your approach is a very fragile way of managing this kind structure. At the very least you should make the values some sort of List. I suspect this is some kind of exercise/ homework like problem that you want to solve.

That being said I will expand pb2q's answer with Tony's remark in a pseudocode manner.

The tabs in the structure are useless with your desired output so they will just be used as delimiters.

System.out.println("Year\tTitle");
keys = table.keySet();
Map<Scanner> scanners = new Map<Scanner>();
for (String key : keys){
    // Here comes the trick
    scanners[i] = new Scanner( table.get(key) ).useDelimiter("\t");
}
-Print headings.
//Now you can pull elements from each scanner until they are empty.
boolean hasMoreElements = true;
while( hasMoreElements ){
  for ( key : keys){
     print  scanners[key].next()
  }
  printLine
  hasMoreElements = scanners[key].hasNext()
}

A head on approach, using the original data with the surprisingly useful Scanner.

Upvotes: 0

pb2q
pb2q

Reputation: 59627

This is very simple, you can iterate over the HashTable using the set of keys:

System.out.println("Year\tTitle");
keys = table.keySet();
for (String key : keys)
    System.out.println(key + "\t" + table.get(key);

Assuming that the year is a String.

Upvotes: 0

Related Questions