2016-01-12 63 views
3

我有一个包含规则和规则的CSV文件。 CSV文件如下所示:Java如何从ArrayList中删除重复项

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 

如您所见,1个规则可以有1个或多个规则版本。我需要做的是读取这个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 
     ) 

什么是做到这一点的最好办法?多维数组?我如何确保它不会多次保存规则名称?

+0

使用'(哈希)地图<字符串,列表>' –

+0

我会强烈建议您花点时间和调查Java集合API。请参阅以下文章以供参考。这可能会让你在正确的方向:http://stackoverflow.com/questions/21974361/what-java-collection-should-i-use –

回答

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>这个特殊的目的。

规则将是你的和规则版本将是你的这将是一个字符串列表。

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

0

这正是key - value对可用于的对象。只要看看Map Interface。在那里你可以定义一个包含各种元素的唯一键值,完全适合你的问题。

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