tags:

views:

225

answers:

3

Hello, I would like use google-collection in order to save the following file in a Hash with multiple keys and values

Key1_1, Key2_1, Key3_1, data1_1, 0, 0
Key1_2, Key2_2, Key3_2, data1_2, 0, 0
Key1_3, Key2_3, Key3_3, data1_3, 0, 0
Key1_4, Key2_4, Key3_4, data1_4, 0, 0

The first three columns are the different keys and the last two integer are the two different values. I have already prepare a code which spilt the lines in chunks.

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class HashMapKey {

  public static void main(String[] args) throws FileNotFoundException, IOException {
    String inputFile = "inputData.txt";

    BufferedReader br = new BufferedReader(new FileReader(inputFile));

    String strLine;
    while ((strLine = br.readLine()) != null) {    
      String[] line = strLine.replaceAll(" ", "").trim().split(",");

      for (int i = 0; i < line.length; i++) {
        System.out.print("[" + line[i] + "]");
      }
      System.out.println();
    }
  }
}

Unfortunately, I do not know how to save these information in google-collection?

Thank you in advance.

Best regards,

+2  A: 

You need to define Key and Value classes, so you can define

  Map<Key, Value> map = new HashMap<Key, Value>();

Note the the Key class must override equals() and hashCode().

Google Collections provides a small amount of assistance: Object.hashCode() can define the hash code and Maps.newHashMap() can create the Map.

Jared Levy
+1  A: 

Do you want to have a Map with Keys that are composed of multiple objects?

http://commons.apache.org/collections/apidocs/org/apache/commons/collections/map/MultiKeyMap.html

Do you want to just alias multiple keys to point to same value?

Then you could check the answer http://stackoverflow.com/questions/822322/how-to-implement-a-map-with-multiple-keys

Else please clarify how you want the map to look like :)

Calm Storm
A: 

I have this code

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;

public class HashMapKey {

  public static void main(String[] args) throws FileNotFoundException, IOException {
    String fFile = "inputData.txt";

    BufferedReader br = new BufferedReader(new FileReader(fFile));

    HashMap<String, HashMap<String, HashMap<String, String[]>>> mantleMap =
            new HashMap<String, HashMap<String, HashMap<String, String[]>>>();
    HashMap<String, HashMap<String, String[]>> middleMap =
            new HashMap<String, HashMap<String, String[]>>();
    HashMap<String, String[]> inMap =
            new HashMap<String, String[]>();

    String strLine;
    while ((strLine = br.readLine()) != null) {

      String[] line = strLine.replaceAll(" ", "").trim().split(",");

      for (int i = 0; i < line.length; i++) {
        System.out.print("[" + line[i] + "]");
      }

      inMap.put(line[2], new Integer[]{line[3], line[4]});
      middleMap.put(line[1], inMap);
      mantleMap.put(line[0], middleMap);

      System.out.println();
    }

    String[] values = mantleMap.get("Key1_1").get("Key2_1").get("Key3_1");
    for (String h : values) {
      System.out.println(h);
    }
  }
}

but unfortunately I am not able to print out the HashMaps content.

How is possible to print the HashMap content?

flash3000