0

I had a question regarding my code below. I'm reading a file containing lots of data of which some stuff is irrelevant. The data is written out on one line, so I cannot use nextLine or something.

For each vertex, I save the relevant information into dataperpoint. When I go to the next vertex, I want to clear the list to fill it with new relevant information.

The issue that I have is that each time I clear dataperpoint, all values in Map get cleared. When I then try to fill it, all previous positions in the Map get the same values.

How can I do this and make sure that each vertex will get his own list?

Looking forward to your suggestions!

    public static Map<Integer, List<Double>> readData(File f) // throws IO exception? 
{
    // Create Map to store the vertex and list with relevant information in
    List<Double> dataperpoint = new ArrayList<Double>();
    Map<Integer, List<Double>> data = new HashMap<>();
    // Open the scanner
    try (Scanner in = new Scanner(f))
    {
        // To make sure the correct localization is used
        in.useLocale(Locale.US);

        // The first six integers contain irrelevant information
        for (int step = 1; step <= 6; step++)
        {
            in.nextInt();
        }

        // Do the information for vertex 0 separately, since it has one data point less
        int vertex = in.nextInt();
        for (int doubleinfo = 1; doubleinfo <= 4; doubleinfo++) // six relevant variables 
        {
            dataperpoint.add(in.nextDouble());
        }

        // irrelevant information
        for (int irrelevantinfo = 1; irrelevantinfo <= 2; irrelevantinfo++)
        {
            in.nextInt();
        }

        // Opening and Closing of time window
        dataperpoint.add((double) in.nextInt());
        dataperpoint.add((double) in.nextInt());

        data.put(vertex, dataperpoint);

        while (in.hasNext()) // think of different statement later
        {
            dataperpoint = new ArrayList<Double>();
            vertex = in.nextInt();

            for (int doubleinfo = 1; doubleinfo <= 4; doubleinfo++) // six relevant variables 
            {
                dataperpoint.add(in.nextDouble());
            }

            // irrelevant information
            for (int irrelevantinfo = 1; irrelevantinfo <= 3; irrelevantinfo++)
            {
                in.nextInt();
            }

            // Opening and Closing of time window
            dataperpoint.add((double) in.nextInt());
            dataperpoint.add((double) in.nextInt());

            data.put(vertex, dataperpoint);
        }
        in.close();
    }
    catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }
Student NL
  • 409
  • 6
  • 16

1 Answers1

0

Use LinkedHashMap<> instead of HashMap<> it should solve your problem. Read this Difference between HashMap, LinkedHashMap and TreeMap

Community
  • 1
  • 1
Sanjit Kumar Mishra
  • 1,153
  • 13
  • 32
  • Okay, I have been reading some information and I still have a question. If you use a specific key, why is the information not inserted only for that key? – Student NL May 09 '16 at 12:08
  • Can you please add code where you are trying to do the same. I might help. And also read about hashing mechanism, that might help you – Sanjit Kumar Mishra May 09 '16 at 17:46