我有一个文本文件,它看起来像这样:
gui awtui swingui
swingui runner extensions
textui runner framework
awtui runner
runner framework
extensions framework
它们由标签分隔
我使用哈希映射存储它们,键是第一行,值是该行中的其余字符,例如: key = gui value = atwui swingui
因此,我使用了一个密钥,多值哈希映射来实现这一点,这就是我到目前为止所做的。
import java.util.*;
import java.io.*;
public class PackageAnalyser {
public static void main(String[] args) throws IOException{
HashMap<String, List<String>> dependencies = new HashMap<String, List<String>>();
List<String> pack = new ArrayList<String>();
try (Scanner in = new Scanner(new File(args[0]))){
//String line = null;
while ((in.hasNext() )){
String [] packs = in.nextLine().split("\t");
for (int i = 0; i < packs.length; i++){
pack.add(packs[i]);
Arrays.sort(packs);
}
dependencies.put(packs[0], pack);
}
}catch (IOException ex){
System.err.println(ex);
} finally {
}System.out.println(dependencies);
}
}
在多次尝试修改代码之后,这就是我运行时输出的样子
`{awtui = [awtui, swingui, runner, swingui, runner, textui, runner, framework, framework], framework=[awtui, swingui, runner, swingui, textui, runner, framework, framework], extensions=[awtui, swingui, runner, swingui, runner, textui, runner, framework, framework]}
我想要的输出是:
{gui = [awtui, swingui], swingui = [runner, extensions], textui =[runner, framework], awtui = [runner], runner = [framework], extensions = [framework]}
如图所示,我正在阅读文本文件,我希望显示上面的输出显示。请帮忙。
答案 0 :(得分:2)
public class PackageAnalyser {
public static void main(String[] args) throws IOException {
HashMap<String, SortedSet<String>> dependencies = new HashMap<String, SortedSet<String>>();
try (Scanner in = new Scanner(new File(args[0]))) {
while ((in.hasNext())) {
String[] packs = in.nextLine().split("\\s");
String key = packs[0];
SortedSet<String> deps = dependencies.get(key);
if (deps == null) {
deps = new TreeSet<String>();
dependencies.put(key, deps);
}
for (int i = 1; i < packs.length; i++) {
if (!packs[i].equals("")) {
deps.add(packs[i]);
}
}
}
} catch (IOException ex) {
System.err.println(ex);
} finally {
}
System.out.println(dependencies);
}
}
给出{extensions=[framework], textui=[framework, runner], gui=[awtui, swingui], swingui=[extensions, runner], awtui=[runner], runner=[framework]}
如果您想保留插入顺序,可以使用LinkedHashMap
代替普通HashMap
答案 1 :(得分:0)
首先,您要将整个列表添加到您的值中,而不是丢弃第一个值。
其次,抛弃它,或移动它。你正在对它进行排序,然后存储第一个值。这可能会弄乱您的订单并使用错误的密钥。
最后,它似乎从未被清除。在每个循环之后,pack必须是新的arraylist,或者清除它的值
答案 2 :(得分:0)
也许,您试图排序以保留读取顺序。但这是一段相对较短的代码:
package com.demo;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Scanner;
public class PackageAnalyser {
public static void main(String[] args){
Scanner scanner = null;
try {
scanner = new Scanner(new File(args[0]));
String line = null;
String[] lineParts = null;
HashMap<String, List<String>> dependencies = new LinkedHashMap<String, List<String>>();
while(scanner.hasNextLine()){
line = scanner.nextLine();
lineParts = line.split("\\s+");
dependencies.put(lineParts[0], Arrays.asList(Arrays.copyOfRange(lineParts, 1, lineParts.length)));
}
System.out.println(dependencies);
} catch (FileNotFoundException e) {
System.err.println(e.getMessage());
} finally {
scanner.close();
}
}
}
给出
{gui = [awtui,swingui],swingui = [runner,extensions],textui = [runner, 框架],awtui = [亚军],亚军= [框架], 扩展= [框架]}