Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Data Not Storing into HashMap Properly

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Data Not Storing into HashMap Properly

    I've tried to create mock data to pass to Multiple Classes for testing, However, the creation of the Mock data doesn't work.

    Essentially, HashMap<String, LinkedList<Point>>
    The Point stores the data correctly,
    The LinkedList stores the Points correctly,
    But the HashMap no longer stores the 'x' values from the data

    Here is the code: (The Rectangle is just used for other purposes)

            Rectangle rect = new Rectangle(20, 20, 300, 300);
            HashMap<String, LinkedList<Point>> randomResultsSet = new HashMap<>(10);
            for (int i = 0; i < 5; i++) {
                LinkedList<Point> dataPoints = new LinkedList<>();
                String key = "Criteria: " + i;
                for (int j = 0; j < 10; j++) {
                    Point px = new Point(j, j);
                    System.out.println("Point[" + px.x + "," + px.y + "] = " + px.toString());
                    dataPoints.add(px);
                }
                randomResultsSet.put(key, dataPoints);
            }
            HGraph graph1 = new HGraph(rect,  randomResultsSet);
            System.out.println("Map: " + randomResultsSet.toString());
            this.TESTINGAREA.add(graph1);

    Part of the output looks like this,
    Point[7,7] = java.awt.Point[x=7,y=7]
    Point[8,8] = java.awt.Point[x=8,y=8]
    Point[9,9] = java.awt.Point[x=9,y=9]
     
    DataPoints: [java.awt.Point[x=0,y=0], java.awt.Point[x=1,y=1], java.awt.Point[x=2,y=2], java.awt.Point[x=3,y=3], java.awt.Point[x=4,y=4], java.awt.Point[x=5,y=5], java.awt.Point[x=6,y=6], java.awt.Point[x=7,y=7], java.awt.Point[x=8,y=8], java.awt.Point[x=9,y=9]]
     
    Map: {Criteria: 0=[java.awt.Point[x=0,y=0], java.awt.Point[x=0,y=1], java.awt.Point[x=0,y=2], java.awt.Point[x=0,y=3], java.awt.Point[x=0,y=4], java.awt.Point[x=0,y=5], java.awt.Point[x=0,y=6], java.awt.Point[x=0,y=7], java.awt.Point[x=0,y=8], java.awt.Point[x=0,y=9]], Criteria: 2=[java.awt.Point[x=0,y=0]...
    BUILD SUCCESSFUL (total time: 16 seconds)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,154
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Data Not Storing into HashMap Properly

    But the HashMap no longer stores the 'x' values from the data
    Are you saying the contents of the Point objects stored in the HashMap has problems with the x values in the Point objects?

    Can you make a small, complete program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Data Not Storing into HashMap Properly

    Correct, The Point data, Stored in the LinkedList, Which is stored in the HashMap, Doesn't seem to save the 'x' Values.

    Sure, If you drop this code into a main method, Imports etc, then it should compile and run, and show the problem. (i'm not in front of my programming computer)


            HashMap<String, LinkedList<Point>> randomResultsSet = new HashMap<>(10);
            for (int i = 0; i < 5; i++) {
                LinkedList<Point> dataPoints = new LinkedList<>();
                String key = "Criteria: " + i;
                for (int j = 0; j < 10; j++) {
                    Point px = new Point(j, j);
                    System.out.println("Point[" + px.x + "," + px.y + "] = " + px.toString());
                    dataPoints.add(px);
                }
                randomResultsSet.put(key, dataPoints);
            }
            System.out.println("Map: " + randomResultsSet.toString());

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,154
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Data Not Storing into HashMap Properly

    Can you make a small, complete program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Data Not Storing into HashMap Properly

    So i haven't fixed the problem, But when i recreated the 'random generating data' program it seemed to work, so my problem lies elsewhere! Thanks for your help!

Similar Threads

  1. Need help storing user data in hashtable
    By leonne in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 5th, 2012, 06:04 PM
  2. Storing data
    By imsuperman05 in forum Collections and Generics
    Replies: 0
    Last Post: December 23rd, 2011, 04:36 PM
  3. Storing User Data
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: January 21st, 2011, 08:07 AM
  4. storing data using form
    By anupam.j2ee in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 27th, 2010, 09:43 AM
  5. Storing data
    By Joyce in forum Collections and Generics
    Replies: 1
    Last Post: September 20th, 2010, 09:16 AM

Tags for this Thread