0

I am trying to create a generic database function to populate default values into a table on creation if need be. I have a class which will create pairs, thanks to this post but don't quite understand how it works! :(

I believe I would create something like this for the pairs:

public class DBColumnValuePair<T, V>
{
    private final T column;
    private final V value;

public DBColumnValuePair(T column, V value)
{
    this.column = column;
    this.value = value;
}

public T getColumn()
{
    return column;
}

public V getValue()
{
    return value;
}

@Override
public int hashCode()
{

    return this.column.hashCode() ^ this.value.hashCode();
}

@Override
public boolean equals(Object o)
{
    if (o == null)
    {
        return false;
    }
    if (!(o instanceof DBColumnValuePair))
    {
        return false;
    }
    DBColumnValuePair dbCVPObject = (DBColumnValuePair) o;
    return this.column.equals(dbCVPObject.getColumn())
            && this.value.equals(dbCVPObject.getValue());
}
 }

Then populate an array with these pairs? Then send this to the method I want to handle the default values.

So my question is how do i populate the pairs in the first place? and would an array of pairs be the way to send them to the handler method?

Hope you can help :)

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Well no sooner had I posted than I realised that it would be quite simple, it's amazing what taking a break and grabbing a beer does!! `code DBColumnValuePair test = new DBColumnValuePair("Ian", 1); test2 = new DBColumnValuePair[3]; test2[0] = test; Log.e("Ian", "T value: " + test2[0].getColumn()); Log.e("Ian", "V value: " + test2[0].getValue()); ` – user1031668 Nov 05 '11 at 23:56
  • If all you are interested is key value pairs, why don't you just use a `HashMap`? – ring bearer Nov 05 '11 at 23:57
  • LOL, Because I have spent a long time in front of my computer so i thought i'd re-invent the wheel!! Hashmaps would work a treat! thanks :) – user1031668 Nov 06 '11 at 00:04
  • If you've solved your problem, then you should post your solution as an answer, and then accept it. :-) – ruakh Nov 06 '11 at 00:47
  • It appears I cannot for another few hours, I tried to in the above comment but it came out as a garbled mess :)!! will try again later. Huh unless i edit the question :) – user1031668 Nov 06 '11 at 01:12
  • A hashmap is not the same as an array of pairs. A map is a `set` of `A` mapped to `B`, whereas an array of pairs is akin to two arrays of the same size `A` + `B`. – Synesso Nov 06 '11 at 01:24
  • Please don't edit the answer into the question. You might want to post your answer as a real answer (which you can accept yourself later). – oers Nov 10 '11 at 11:16

1 Answers1

0

Answer:

Well I figured it out, even though as ring bearer pointed out I could just use a HashMap!

What I did follows:

DBColumnValuePair<String, Integer> test = new DBColumnValuePair<String, Integer>("Ian", 1);
test2 = new DBColumnValuePair[3];
test2[0] = test;
Log.e("Ian", "T value: " + test2[0].getColumn());
Log.e("Ian", "V value: " + test2[0].getValue());
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • [Answered in a question edit. Moved to Community Wiki.](https://meta.stackoverflow.com/questions/267434/what-is-the-appropriate-action-when-the-answer-to-a-question-is-added-to-the-que) – Brian Tompsett - 汤莱恩 Mar 07 '19 at 20:06