文本文件a.txt,内容如下
1 a b c
2 b e
3 a c e现在要将上述文件中的记录一行一行的读入到HashMap<Integer,Set<String>>里面,举例如下,读第一行记录,数字1应该存放到HashMap中的Integer位置,a b c存放在Set<String>位置,该怎么操作?

解决方案 »

  1.   

    Map<Integer, Set<String>> map = new HashMap<Integer, Set<String>>();
        BufferedReader in = null;
        try {
          in = new BufferedReader(new FileReader("D:/abc.txt"));
          String line;
          while ((line = in.readLine()) != null) {
            String[] array = line.split(" ");
            Integer key = Integer.parseInt(array[0]);
            String[] valueArray = Arrays.copyOfRange(array, 1, array.length);
            Set<String> value = new HashSet<String>(Arrays.asList(valueArray));
            map.put(key, value);
          }
        } finally {
          if (in != null) {
            try {
              in.close();
              in = null;
            } catch (Exception ex) {
            }
          }
        }
        System.out.println(map);