0

I have something like this :

  • category1 : foo, bar, ...
  • category2: baz, qux, ...
  • ...

At some point in my code I'll have to retrieve which category an item belong to as fast as possible (example: find "category1" from "foo").

I have to decide on how to store these lists in a way that allow me to identify a category as fast as possible (I'm free to choose whatever data structure I want).

It won't happen often but I must also be able to update this list afterward (directly editing the file or using shell scripts or whatever, that'll be independent from the current executable).

What would be the most suited for my needs in order to store these lists in external files ?

Aleksandair
  • 167
  • 1
  • 2
  • 11
  • If none of the strings in one category can exist within another category, then a simple map of strings to category would be fine. If you need to have duplicates within each list, then you could use a map of List to Category. In the firs solution - map.get("foo") returns category1. In the second, you would iterate through the keyset of your map to find the list containing the string you want, and return the category that matches when you find it. – Michael Peacock Oct 30 '17 at 14:30
  • 1
    Does it matter how it's stored in the file exactly? It's not clear whether you're asking for a Java data structure suggestion or for a file layout. Usually, reading a file is so slow that it doesn't really matter how it is structured performancewise. – daniu Oct 30 '17 at 14:30

2 Answers2

1

In order to find which category an item belong to: Use HashMap<String, String> where the key is the items and the value is its category.

To store the HashMap into a file and read it back, considering that HashMap implements Serializable, see here.

std4453
  • 528
  • 7
  • 12
1

Storing a map in a file with the item as key and category as property can be easily done by using java.util.Properties.

java.util.Properties is an extension of java.util.Hashtable which is very similar to java.util.HashMap.

So you could use code similar to the example below in order to serialize your item - category map into a properties file and to read it back from a file:

Properties properties = new Properties();
properties.setProperty("foo", "cat1");
properties.setProperty("ba", "cat1");
properties.setProperty("fooz", "cat2");
properties.setProperty("baz", "cat2");
File storage = new File("index.properties");
// write to file
try(BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(storage), "UTF-8"))) {
    properties.store(writer, "index");
}

// Read from file
Properties readProps = new Properties();
try(BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(storage), "UTF-8"))) {
    readProps.load(reader);
}

if(!readProps.equals(properties)) {
    throw  new IllegalStateException("Written and read properties do not match");
}

System.out.println(readProps.getProperty("foo"));
System.out.println(readProps.getProperty("fooz"));

If you run the code it will print out:

cat1
cat2

If you edit the created index.properties file, this is what you see:

#index
#Mon Oct 30 15:41:35 GMT 2017
fooz=cat2
foo=cat1
baz=cat2
ba=cat1
gil.fernandes
  • 12,978
  • 5
  • 63
  • 76