Alastair Rodgers wrote:

>It sounds like you should check out java.util.HashMap. 
>
This assumes a connection between the data pairs, which may well be 
there, but is not indicated in the request.

What may be called for is a simple wrapper class:

public class Pairs {
  public Pairs(){}
  public Pairs(String one, String two) {
    valueOne = one;
    valueTwo = two;
  }

  private String valueOne, valueTwo;

  public setOne(String value) {valueOne = value;}
  public setTwo(String value) {valueTwo = value;}
  public String getOne() {return valueOne;}
  public String getTwo() {return valueTwo;}
}

If you need the values to be immutable, so once created the values never 
change, simply remove the empty constructor and the setters.  If there 
is a possibility that you might place these objects into a Hashmap, 
supply a hash method, such as:

public int hash() }
  int hashValue = 0;
  if (valueOne != null)
    hashValue = valueOne.hash();
  if (valueTwo != null)
    hashValue &= valueTwo.hash(); // How you merge the values is not 
important.
  return hashValue;
}

A better way is to also supply a toString method and base the hash on 
the returned string.  But the hash method is not really necessary if you 
just want to pass the values around from one place to another.

It looks like you don't expect the first of the data pairs to ever be 
null.  If that is the case, you can place the appropriate check in the 
constructor and setter for the first value to throw an exception if the 
first value is null (and discard the first 'if' in the hash method above).

There you have it.  A small class that encapsulates your data pairs and 
provides all the functionality required to handle them.

Tomm

>>>I have a very simplistic requirement but a problem in trying to 
>>>determine the best object to use to satisfy it. I need to pass a 
>>>collection of pairs of data between objects and want to 
>>>      
>>>
>>maintain the 
>>    
>>
>>>association between the pairs. There is no order to the pairs. When 
>>>extracting the data I merely want to iterate through whatever the 
>>>object may be and pull out each pair of data.
>>>
>>>The data would be represented something like the following 
>>>      
>>>
>>and I have 
>>    
>>
>>>a need to store null:
>>>
>>>String String
>>>String String
>>>String null
>>>String null
>>>String null
>>>      
>>>




To change your JDJList options, please visit: http://www.sys-con.com/java/list.cfm

Reply via email to