Bill
Bill

Reputation: 79

How to store objects and retrieve them later in java?

I am working on a code that reads a user input model and then performs numerical calculations with the given data. Currently, the data structures are set up as arrays. For example, if the user input file specifies a single compartment with a filter flow rate and time value, the values are stored in the following arrays:

filter_flow_rate[0]
filter_time[0]

If a second compartment is specified with another filter flow rate and time value, the value is stored as:

filter_time[1]
filter_flow_rate[1]

i.e. the first dimension of the array specifies the compartment number while the name of the array specifies the type of value. I am in the process of changing this structure. I created a filter class which is instantiated when a filter is read in. I stored the filter instance in a hash table, with the key being the compartment number and the value being the filter instance. Now, when I need the filter object and its fields, I can find them in this way:

filter.get(iCompartment);
double x = filter.flowRate; // example of retrieving flow rate value
double y = filter.time;     // example of retrieving time value

I like this structure better, but I would like to remove the hash table if possible. It would be nice to create a compartment class which is instantiated when a compartment is read in. Then, if a filter were read in, it could be associated with that compartment object and could be stored and retrieved as such:

compartment.filter.flowRate = ...

Here, the compartment instance is associated with the filter instance, which is more desirable than using the clumsy hash table method where the key is standing in for the compartment object. The problem is, I don't know how to do it. Is there a good way to implement something like the above?

Upvotes: 1

Views: 3108

Answers (3)

Java Devil
Java Devil

Reputation: 10959

For your compartment class, have an internal member which is of Filter class.

public class Compartment{
     Filter filter;
     // + others
     // ...
}

Then use getter and setter methods to get and set the Filter for the compartment.

public void setFilter(Filter filter){
     this.filter = filter;
}

public Filter getFilter(){
     return filter;
}

Then when you load a compartment, set its filter.

Then you can do compartment.getFilter().flowRate to get that compartments flowrate.

If you want to use your syntax compartment.filter.flowRate then you could define it as a public member

public class Compartment{
     public Filter filter;
     // + others
     // ...
}

Then you wouldn't need the getter and setter.

Also depending on how you create your component instances, you could also define it as part of the constructor.

public class Compartment{
     public Filter filter;

     public Compartment(Filter filter){
         this.filter = filter;
     }
}

Then when you create your compartment you can supply your filter as new Compartment(filter). This is a better option if your compartment has to have a filter as you then cannot create a compartment without one (unless you specifically pass in a null value for the filter) as the default constructor new Compartment() will no longer work.

Upvotes: 1

searchengine27
searchengine27

Reputation: 1471

You might want to consider an index based structure, from the sounds of it.

You can do:

double [][] compartment = new double[][/*number of compartments*/];
for(int i = 0 ; i < compartment.length; i++) {
    compartment[i] = new double[2]; //for the two types of values you care about
}
...
int indexICareAbout = ...;
double [] values = compartment[indexICareAbout];
double sortValue = values[0];
double time = values[1];

which is a dual dimension array of doubles for both values, rather than two individual arrays.

Alternatively:

class Foo {
    public final double flowRate;
    public final double time;
    Foo(double flowRate, double time) {
        this.flowRate = flowRate;
        this.time = time;
    }
}
...
Foo[] compartment = new Foo[/*number of compartments*/];
...
int indexICareAbout = ...;
Foo values = compartment[indexICareAbout];

Which is a single dimension array using your object you mentioned.

These two are good because you can grab both values out without searching through memory (without getting into specifics of how arrays work under the hood).

Or using java.util:

class Foo {
    public final double flowRate;
    public final double time;
    Foo(double flowRate, double time) {
        this.flowRate = flowRate;
        this.time = time;
    }
}
...
List<Foo> compartments = new ArrayList<Foo>();
...
int indexICareAbout = ...;
Foo values = compartment.get(indexICareAbout);
double sortValue = values.flowRate;
double time = values.time;

I used ArrayList as an example, but any class that extends java.util.List will do fine here. There are only a few syntax differences between the 2nd and final example, but this will be more useful in most cases - including yours I suspect. The logical key difference between the 2nd and 3rd example is that the 3rd example grows in size as it fills up, meaning you are not bound by size, such as is the case in the 1st and 2nd example.

Now I won't really go into too much detail than that because it seems you're still learning some things, and I suspect the use of generics was probably too far to push you, let alone java Collections.

Upvotes: 1

SylarBenes
SylarBenes

Reputation: 411

I would create a class as you already suggest in your answer, You could do it like this:

public class Filter{
    double time;
    double flowRate;

//constructor
public Filter(double time, double flowRate){
    this.time = time;
    this.flowRate = flowRate;
}

}

From what I read in your explanation, the Compartment isn't really more than a numbered index? In that case you could initialize a List and whenever a new one gets read in you can add it to your list like this:

filterList.add(new Filter(x, y)) // your double values would be passed instead of x and y

If the Compartment is actually a datatype as well that wouldn't work. In that case it'd be helpful if you could post the Compartment code as well.

Upvotes: 1

Related Questions