user1988892
user1988892

Reputation: 5

LinkedLists as Values of HashMap don't contain the right data after modification

While iterating through a text file of lines of hex addresses, I am trying to save a list of the line numbers where each hex address appears for each address.

I'm using a HashMap of Long, LinkedList to hold the hex addresses, where the LinkedList is the List of line numbers which the Long appears in.

The txt file I'm using as a test is located here, a short excerpt from it is:

0041f7a0 R
13f5e2c0 R
05e78900 R
004758a0 R
31348900 W
004a30e0 R

I parse each line, and save the hex address as a Long, then populate the map by using my code (supplied below)

HashMap<Long,LinkedList<Long>> pMap = new HashMap<Long,LinkedList<Long>>();
//this declaration is a global variable in the program


//these variables are all local
    Long address = new Long(0);
    String newLine;
    String[] tempStr;
    int lineNum = 0;

    BufferedReader br = new BufferedReader(new FileReader(file));

    while((newLine = br.readLine()) != null) {
        tempStr = newLine.split(" ");
        address = Long.parseLong(tempStr[0],16);

        if(!pMap.containsKey(address)) {
            LinkedList<Long> tempList = new LinkedList<Long>();
            tempList.add(lineNum);
            pMap.put(address,tempList);
        }
        else {
            pMap.get(address).add(lineNum);
        }

        lineNum++;
    }

I look through the HashMap here:

        int evictIndex = -1; //(to tell which ones have been called)
        int farthestIndex = -1; //the value of the farthest away call's index
        long farthestAddress = -1; //the address that contains the farthest away call

        for(int i = 0; i < pTableSize; i++) {
            Long addr = pTable[i][0]; //get the address that I'll be searching for
            LinkedList<Integer> curr = pMap.get(addr);
            for(Integer l : curr) {
               if(l-accessCt > 0 && l > farthestIndex) {
                    farthestIndex = l.intValue();
                    farthestAddress = addr;
               }
            }
        }

        for(int i = 0; i < pTableSize; i ++){ 
            if(pTable[i][0] == farthestAddress)
                evictIndex = i;
        }

After this code, the LinkedLists for each address only contain 1 Long: the first occurrence of the address. Can anyone help me?

Upvotes: 0

Views: 74

Answers (1)

blueygh2
blueygh2

Reputation: 1538

You declare LinkedList<Integer> tempList = new LinkedList<Integer>();, but you want it to be a list of Longs. How about LinkedList<Long> tempList = new LinkedList<Long>();?

EDIT:

Are you sure the scope of pMap is correct? If you are re-initializing it everytime you call the method, it will simply be overridden. If you move its scope up one level (instance variable), it should work.

EDIT 2:

If I understand your code correctly (and have implemented it the way you intended it to be), it works fine. I get results like 844754720=[1242, 1275]

There are a lot of addresses that only have one linenum, so you might be fooled into thinking that you're only getting the first line?

The number of addresses that occur only in one line is : 14314

The number of addresses occuring in more than one line is: 23383

At least in the file you have specified. So you really have a lot of addresses only occuring in one line. Is that the problem?

Upvotes: 1

Related Questions