Peter
Peter

Reputation: 131

Which Java Collection is best for this scenario

In my application I have small tables(files) with unique keys (most of those have two or more feilds - composed keys). I would like to create a class for each of those small tables and load the data in to a java collection. This object will read the file and load the data in to a a java collection. I would then like to fetch the data from this collection by the key values so that I can have access to all fields. My question is which collection I should use or say which collection supports multiple keys? A link to an examole will be great. Thanks in advance!

Upvotes: 2

Views: 1228

Answers (3)

wattostudios
wattostudios

Reputation: 8774

A Hashtable would probably work best to store the contents of each table. A Hashtable basically allows you to add any number of objects you want, and each one has a unique key.

My suggestion would be to do the following for each table file. In my example, I am assuming that you read each line of your table file into an object called Entry...

// Create a new Hashtable, where the contents will be a String (the unique key) and an "Entry" object for the table row of data
Hashtable<String,Entry> entriesTable = new Hashtable<String,Entry>();

for (each line in your table file){
  // Generate the unique value for this row. If there are multiple columns that make up the key,
  // just join them together into a single String
  String key = uniqueColumn1 + uniqueColumn2 + ...;

  // Create an object for your row, if you dont have one already.
  Entry row = new Entry(line);

  // Add the entry to the Hashtable
  entriesTable.put(key,row);
  }

When you want to get a row from the table, you ask for it by its unique value...

Entry entry = entriesTable.get(uniqueColumn1 + uniqueColumn2 + ...);

You can add as many objects as you like, provided each have a unique key. The Hashtable supports adding and removing of values, etc, so its pretty easy to work with.

You can also convert a Hashtable into an array if you want, and you can obtain an Enumerator for walking over each entry if you need to get the full contents of the Hashtable.

Upvotes: 1

user181533
user181533

Reputation: 49

Not completely sure what you mean by multiple keys. A class per 'table' that you can store in a collection and fetch with a key/value pair says HashMap to me.

Upvotes: 2

Jim Garrison
Jim Garrison

Reputation: 86774

Assuming I've read your question correctly (there's some ambiguity about "multiple keys") what you want is to define a class for the key itself that contains the multiple fields and implements equals() and hashCode() in such a way that the objects can be used as HashMap keys.

Here's a very simple skeleton implementation (not tested, some error handling omitted):

public class MyKey
{
    private String  part1 = null;
    private Integer part2 = null;
    public MyKey(String part1, int part2)
    {
        this.part1 = part1;
        this.part2 = part2;
    }

    @Override
    public boolean equals(Object o)
    {
        if (o == null || !(o instanceof MyKey)) return false;
        return this.part1.equals(((MyKey)o).part1) && this.part2.equals(((MyKey)o).part2);
    }

    @Override 
    public int hashCode()
    {
        return this.part1.hashCode() + this.part2.hashCode();
    }
}

Upvotes: 4

Related Questions