3

I have a CSV file which contains rules and ruleversions. The CSV file looks like this:

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

As you can see, 1 rule can have 1 or more rule versions. What I need to do is read this CSV file and put them in an array. I am currently doing that with the following script:

 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;
   }

But I need to adapt the script so that it saves the information in the following format:

ARRAY (
          => RULE       => 01-02-01, 01-02-02, 01-02-04
          => OTHER_RULE => 01-02-34
          => THIRDRULE  => 01-02-01, 01-02-02
          )

What is the best way to do this? Multidimensional array? And how do I make sure it doesn't save the rulename more than once?

Student
  • 531
  • 4
  • 12
  • 28
  • Use a `(Hash)Map>` – Konstantin Yovkov Jan 12 '16 at 10:00
  • I would highly recommend you to take your time and investigate java collections API. Please see following post for reference. This may put you in the right direction: http://stackoverflow.com/questions/21974361/what-java-collection-should-i-use – e.doroskevic Jan 12 '16 at 10:25

5 Answers5

3

You should use a different data structure, for example an HashMap, like this.

    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);
                }
            }
        }

This should work!

giograno
  • 1,749
  • 3
  • 18
  • 30
2

See using an ArrayList is not a good data structure of choice here.

I would personally suggest you to use a HashMap> for this particular purpose.

The rules will be your keys and rule versions will be your values which will be a list of strings.

While traversing your original file, just check if the rule (key) is present, then add the value to the list of rule versions (values) already present, otherwise add a new key and add the value to it.

Aritra Roy
  • 15,355
  • 10
  • 73
  • 107
1

For instance like this:

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());
}
Barmaley
  • 16,638
  • 18
  • 73
  • 146
0

This is exactly what key - value pairs can be used for. Just take a look at the Map Interface. There you can define a unique key containing various elements as value, perfectly for your issue.

Patrick Weiß
  • 436
  • 9
  • 23
0

Code:

// 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");
}

Reference:

Set
Map

e.doroskevic
  • 2,129
  • 18
  • 25