Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用google collections提供多个键和值_Java_Guava - Fatal编程技术网

Java 使用google collections提供多个键和值

Java 使用google collections提供多个键和值,java,guava,Java,Guava,我想使用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.

我想使用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();
    }
  }
}
导入java.io.BufferedReader;
导入java.io.FileNotFoundException;
导入java.io.FileReader;
导入java.io.IOException;
公共类HashMapKey{
公共静态void main(字符串[]args)抛出FileNotFoundException、IOException{
字符串inputFile=“inputData.txt”;
BufferedReader br=新的BufferedReader(新文件读取器(inputFile));
弦斯特林;
而((strLine=br.readLine())!=null){
String[]line=strLine.replaceAll(“,”).trim().split(“,”);
对于(int i=0;i
不幸的是,我不知道如何在谷歌收藏保存这些信息

先谢谢你


非常感谢,

您需要定义键和值类,这样您就可以定义

  Map<Key, Value> map = new HashMap<Key, Value>();
Map Map=newhashmap();
注意,Key类必须重写equals()和hashCode()


Google Collections提供了少量的帮助:可以定义哈希代码并创建映射。

您想要一个包含由多个对象组成的键的映射吗

是否要将多个键别名为指向同一值

然后你可以检查答案

否则,请澄清您希望地图的外观:)

我有此代码

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);
    }
  }
}
导入java.io.BufferedReader;
导入java.io.FileNotFoundException;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.HashMap;
公共类HashMapKey{
公共静态void main(字符串[]args)抛出FileNotFoundException、IOException{
字符串fFile=“inputData.txt”;
BufferedReader br=新的BufferedReader(新文件读取器(fFile));
HashMap mantleMap=
新的HashMap();
HashMap中间映射=
新的HashMap();
哈希映射inMap=
新的HashMap();
弦斯特林;
而((strLine=br.readLine())!=null){
String[]line=strLine.replaceAll(“,”).trim().split(“,”);
对于(int i=0;i
但不幸的是,我无法打印出HashMaps内容

如何打印HashMap内容