-1

I'm having trouble trying to load lots of data into different hashMaps. I use a different class that handles all of the decoding of the file and transferring to a hashMap. My problem is that i'll load in one file into a specific hashMap (say hashMap x). But when I got to load different data into ex. hasMap y, hashMap x gets re-written with all the stuff hashMap y is supposed to have. I later found out that some keys and values were getting through from different files, but some were getting erased because hashMap doesn't allow duplicates. So my end result for x and y is a mashup of data from both files into one hashMap. I dont know how to fix this.

Here is what I have so far:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class CodeFileProcessor {

    private static Map<String, String> codeMap = new HashMap<String, String>();

    public static Map<String, String> readCodeFile(String fileName) throws  IOException {
        codeMap.clear();
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        String nextLine = br.readLine();
        while(nextLine != null) {
            String[] parts = nextLine.split(",");
            codeMap.put(parts[0], parts[1]);
            nextLine = br.readLine();
        }
    br.close();
    return codeMap;
    }
}
wildplasser
  • 43,142
  • 8
  • 66
  • 109
Collecto
  • 89
  • 9
  • Possible duplicate of [What does the 'static' keyword do in a class?](http://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-a-class) – Mark B Mar 19 '17 at 15:55
  • Everything under the class should be static. I'm following a UML from a professor for an assignment. – Collecto Mar 19 '17 at 16:08
  • I'm sure the professor wants you to actually learn how to write Java code. Please look into what it means for a variable to be `static`. You are reusing the same HashMap over and over. If there is an issue or question about the professor's requirements then why aren't you asking us about it instead of asking your professor? – Mark B Mar 19 '17 at 16:11
  • class memory is different and heap memory is different.what is the use to maintain the static keyword. – Lova Chittumuri Mar 19 '17 at 16:14

1 Answers1

0

Your problem looks like codeMap is static. You're basically re-using the same map over and over which is why things look like they're getting overwritten.

Try creating a new map as part of the method call:

//DELETE THIS LINE - private static Map<String, String> codeMap = new HashMap<String, String>();

public static Map<String, String> readCodeFile(String fileName) throws  IOException {
    //Create a new map instead of reusing the static (shared) one
    Map<String, String> codeMap = new HashMap<String, String>();
        ...
Paolo
  • 22,188
  • 6
  • 42
  • 49
  • Im following a UML for an assignment. Everything under this class should be static. Unless the UML is wrong, this is how it should be – Collecto Mar 19 '17 at 16:05
  • 1
    I think your UML is wrong in this case. You cannot have a static map if you want multiple distinct instances of it (map "x" and "y" from your description) – Paolo Mar 19 '17 at 16:20