如何将一行中的元素解析为不同的哈希表?

时间:2015-04-24 05:54:13

标签: java hashmap

假设我有一个字母a-z格式如下的文件:

a,9,1
b,2,4
*,0,2

我有两个名为hash的哈希映射:hashA,hashB 现在我需要编写一个代码来使字母表成为一个键,并将其旁边的数字作为值。所以它需要像这样:hashA有(a,9),hashB有(a,1) 我的部分代码是:

public static void constructLetterMaps() throws FileNotFoundException{
  File pointvals = new File(letterFileLoc);
  Scanner pts = new Scanner(pointvals);
  while (pts.hasNext()){
  char alpha = (pts.next()).charAt(0);

所以我如何解析不同的哈希映射并为每一行跟踪它。

1 个答案:

答案 0 :(得分:0)

例如,您可以逐行读取文件,将一行分成一个字符串数组,这样可以非常直接地将值存储到不同的Map<String, String> hashA = new HashMap<>(); Map<String, String> hashB = new HashMap<>(); String line = "a,9,1"; // the line from the file String[] parts = line.split(","); // split the lines into parts hashA.put(parts[0], parts[1]); // put "a", "9" hashB.put(parts[0], parts[2]); // put "a", "1" System.out.println("hashA = " + hashA); System.out.println("hashB = " + hashB); 中。

查看一些片段以证明这个想法。

RGBtoHSV(r: r, g: g, b: b, h: &h, s: &s, v: &v)
相关问题