0

If I access the keys of a LinkedHashMap<Vector, Vector<Integer>> inside its class it is extracted correctly, but if I create an instance of its class and call a class method that displays the LinkedHashMap, I find some of the fields in the keys that are instances of Vector are missing.

import java.util.Vector;
import java.util.LinkedHashMap;
public class TestLinkedHashMap 
{
    public static void main(String[] args) 
    {
        Test t = new Test();
        t.setSample();      
        System.out.println("The sample printed in main function is : \n" + t.getSample());
        return;
    }
}

class Test
{
    LinkedHashMap<Vector, Vector<Integer>> sample;

    public Test()
    {
        sample = 
            new LinkedHashMap<Vector, Vector<Integer>>();
    }

    public LinkedHashMap<Vector, Vector<Integer>> getSample()
    {
        return sample;
    }

    public void setSample()
    {
        Vector record1 = new Vector();
        record1.add(new String("Apple"));
        record1.add(new String("Orange"));
        record1.add(new String("Grapes"));
        record1.add(new String("Lime"));
        Vector<Integer> v1 = new Vector<Integer>();
        v1.add(new Integer(0));
        v1.add(new Integer(8));
        v1.add(new Integer(28));
        System.out.println("sample = " + sample);
        sample.put(new Vector(record1), v1);
        Vector record2 = new Vector();
        record2.add(new String("Pineapple"));
        record2.add(new String("Pear"));
        record2.add(new String("Mango"));
        Vector<Integer> v2 = new Vector<Integer>();
        v2.add(new Integer(0));
        v2.add(new Integer(18));
        sample.put(new Vector(record2), v2);
        System.out.println("The sample printed inside the class is : \n" + sample);
    }

Above code works fine, but in the application I am developing, I am getting different outputs for the sample. I have included the output of my application below. My application is using LinkedHashMap in similar manner as above.

Output is different in real application

  • 4
    In general, it's a bad idea to use something mutable as key. You are messing up the hashes in the hashmap. The documentation explicitly warned you not to use something mutable as the key. – Jai Jun 26 '18 at 05:15
  • I think you maybe right, then only solution would be to convert the Vector to string and store it as a key. – user3322178 Jun 26 '18 at 06:27
  • Random thought: Maybe OP could just use the toString output of the Vector objects as keys, because I think those don't ever change. – Dreamspace President Jun 26 '18 at 09:00
  • Side note: if you don't need the thread-safety of `Vector`, `ArrayList` is preferred (says the javadoc of `Vector`). – Harald Jun 26 '18 at 18:56

1 Answers1

1

Of course this.sample is null. You never set it to a value. In the constructor, you’re creating a new LinkedHashMap<Vector, Vector<Integer>> , but not actually assigning it to the class member variable sample, instead you’re just creating a local variable (local to the constructor) called sample.

Remove the LinkedHashMap<Vector, Vector<Integer>> before sample in the constructor and that will initialize the class member variable sample as expected.

Matthew Meacham
  • 403
  • 3
  • 9