[英]multiple keys and values with google-collections
我想使用google-collection以便將以下文件保存在具有多個鍵和值的哈希中
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
前三列是不同的鍵,后兩個整數是兩個不同的值。 我已經准備好了將代碼分成幾行的代碼。
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();
}
}
}
不幸的是,我不知道如何將這些信息保存在google-collection中?
先感謝您。
最好的祝福,
您需要定義鍵和值類,以便可以定義
Map<Key, Value> map = new HashMap<Key, Value>();
請注意,Key類必須重寫equals()和hashCode()。
Google館藏提供了少量幫助: Object.hashCode()可以定義哈希碼,而Maps.newHashMap()可以創建Map。
您是否想要具有包含多個對象的鍵的Map?
http://commons.apache.org/collections/apidocs/org/apache/commons/collections/map/MultiKeyMap.html
您是否只想別名多個鍵以指向相同的值?
然后,您可以查看答案如何實現具有多個鍵的Map?
否則,請說明您希望地圖的外觀如何:)
我有這個代碼
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);
}
}
}
但不幸的是我無法打印出HashMaps內容。
如何打印HashMap內容?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.