Java如何从ArrayList中删除重复项

3

我有一个CSV文件,其中包含规则和规则版本。CSV文件的格式如下:

CSV FILE:
          #RULENAME, RULEVERSION
          RULE,01-02-01
          RULE,01-02-02
          RULE,01-02-34
          OTHER_RULE,01-02-04
          THIRDRULE, 01-02-04
          THIRDRULE, 01-02-04

正如您所见,一个规则可以有一个或多个规则版本。我需要做的是读取这个CSV文件并将它们放入一个数组中。我目前正在使用以下脚本来实现:

 private static List<String[]> getRulesFromFile() {
         String csvFile = "rulesets.csv";
         BufferedReader br = null;
         String line = "";
         String delimiter = ",";

         List<String[]> input = new ArrayList<String[]>();

         try {
                br = new BufferedReader(new FileReader(csvFile));
                while ((line = br.readLine()) != null) {
                       if (!line.startsWith("#")) {
                              String[] rulesetEntry = line.split(delimiter);
                              input.add(rulesetEntry);
                       }
                }

         } catch (FileNotFoundException e) {
                e.printStackTrace();
         } catch (IOException e) {
                e.printStackTrace();
         } finally {
                if (br != null) {
                       try {
                              br.close();
                       } catch (IOException e) {
                              e.printStackTrace();
                       }
                }
         }
         return input;
   }

但是我需要修改脚本,以便将信息保存在以下格式中:
ARRAY (
          => RULE       => 01-02-01, 01-02-02, 01-02-04
          => OTHER_RULE => 01-02-34
          => THIRDRULE  => 01-02-01, 01-02-02
          )

什么是最好的方法?使用多维数组?如何确保它不会保存重复的规则名称?

使用 (Hash)Map<String, List<String>> - Konstantin Yovkov
我强烈建议您花时间研究Java集合API。请参考以下帖子,这可能会让您朝着正确的方向前进:https://dev59.com/3GEh5IYBdhLWcg3w0WVP - e.doroskevic
5个回答

3

您应该使用不同的数据结构,例如HashMap,如下所示。

    HashMap<String, List<String>> myMap = new HashMap<>();

    try {
        br = new BufferedReader(new FileReader(csvFile));
        while ((line = br.readLine()) != null) {
            if (!line.startsWith("#")) {
                String[] parts = string.split(delimiter);
                String key     = parts[0];
                String value   = parts[1];
                if (myMap.containsKey(key)) {
                    myMap.get(key).add(value);
                } else {
                    List<String> values = new ArrayList<String>();
                    values.add(value);
                    myMap.put(key, values);
                }
            }
        }

这应该可以工作!


2

使用 ArrayList 并不是在这里做出好的数据结构选择。

我个人建议您使用 HashMap> 来实现特定的目的。

规则将成为您的 ,规则版本将成为您的 ,它将是一个字符串列表。

在遍历原始文件时,只需检查是否存在规则(键),然后将该值添加到已经存在的规则版本(值)列表中,否则添加新的键并将其添加到其中。


1
例如像这样:

例如像这样:

public List<String> removeDuplicates(List<String> myList) {
    Hashtable<String, String> hashtable=new Hashtable<String, String>();
    for(String s:myList) {
        hashtable.put(s, s);
    }
    return new ArrayList<String>(hashtable.values());
}

0

代码:

// This collection will take String type as a Key 
// and Prevent duplicates in its associated values

Map<String, HashSet<String>> map = new HashMap<String,HashSet<String>>();

// Check if collection contains the Key you are about to enter
// !REPLACE! -> "rule"  with the Key you want to enter into your collection
// !REPLACE! -> "whatever" with the Value you want to associate with the key

if(!map.containsKey("rule")){
map.put("rule", new HashSet<String>());
}
else{
map.get("rule").add("whatever");
}

参考:

Set(集合)
Map(映射表)


0

这正是键值对可以使用的地方。只需看一下Map接口。在那里,您可以定义一个包含各种元素作为值的唯一键,完美地解决了您的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接