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

asked Jun 26, 2018 at 5:10
4
  • 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. Commented Jun 26, 2018 at 5:15
  • I think you maybe right, then only solution would be to convert the Vector to string and store it as a key. Commented Jun 26, 2018 at 6: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. Commented Jun 26, 2018 at 9:00
  • Side note: if you don't need the thread-safety of Vector, ArrayList is preferred (says the javadoc of Vector). Commented Jun 26, 2018 at 18:56

1 Answer 1

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.

answered Jun 26, 2018 at 5:20
1
  • deleted the redeclaring of sample Commented Jun 26, 2018 at 6:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.