简体   繁体   中英

HASHMAP, key present still giving “false” result

Following is my code that returning false even if the key exists:

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


public class SequenceNumber {

 public static int getSequenceNumber (String TcOrderId){

    // Create a hash map to set key values pair.
    Map<String, Integer> map = new HashMap<String, Integer>();  
    int i= 1;

    // check if hashmap contains the key.
    System.out.println("key present " +map.containsKey(TcOrderId));
    if (map.containsKey(TcOrderId))
    {
       //Key Present
       System.out.println("Inside IF ");
       int value = map.get(TcOrderId);
       System.out.println("value from the key " + value);
       map.remove(value);
       map.put(TcOrderId, value+1);
       return map.get(TcOrderId);
    }
    else
    {
        //Key Not present
        System.out.println("INSIDE ELSE ");
        map.put(TcOrderId, i);
        System.out.println("map "+ map);
        return map.get(TcOrderId);
    }
}

public static void main(String[] args) throws IOException {

    String sCurrentLine;
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader("C:\\Users\\BongAn\\Desktop\\Package\\testing.txt"));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    {
        while ((sCurrentLine = br.readLine()) != null) {
            //String orderid = sCurrentLine.substring(0, 6);
            System.out.println("reading line " +sCurrentLine);
            int seqvalue = getSequenceNumber(sCurrentLine);
            System.out.println("seqvalue "+seqvalue);
        }

    }

}

}

Input data in the file:
1233
1233
1234

The result should be
1
2
1

But everytime its going in the else loop and the result is
1
1
1

I am trying to use HASHMAP as I am creating my own index.

Something like this should work. Haven't tried running it though.

public class SequenceNumber {

 public static int getSequenceNumber (String TcOrderId, Map<String, Integer> map){  
    if(!map.contains(TcOrderId)){
        map.put(TcOrderId, 0);
    }

    map.put(TcOrderId, map.get(TcOrderId)+1);
    return map.get(TcOrderId);
}

public static void main(String[] args) throws IOException {

    String sCurrentLine;
    BufferedReader br = null;
    Map<String, Integer> map = new HashMap<String, Integer>();
    try {
        br = new BufferedReader(new FileReader("C:\\Users\\BongAn\\Desktop\\Package\\testing.txt"));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    {
        while ((sCurrentLine = br.readLine()) != null) {
            //String orderid = sCurrentLine.substring(0, 6);
            System.out.println("reading line " +sCurrentLine);
            int seqvalue = getSequenceNumber(sCurrentLine, map);
            System.out.println("seqvalue "+seqvalue);
        }

    }

In your CODE everytime you call getSequenceNumber function - you create new HashMap. I believe this is not something you want.

To avoid that - you can simply move Map<String, Integer> map = new HashMap<String, Integer>(); into the body of class. Since the function getSequenceNumber is a static function - you will need to make the variable static. Hope this helps.

Snippet:

public class SequenceNumber {

 // PUT STATIC VARIABLE HERE:
 static Map<String, Integer> map = new HashMap<String, Integer>();  

 public static int getSequenceNumber (String TcOrderId){

    // Create a hash map to set key values pair.
    // (REMOVE) Map<String, Integer> map = new HashMap<String, Integer>();**  
    int i= 1;

    // check if hashmap contains the key.
  ...
  }
...
}

Another alternative

(perhaps better) would be to avoid static functions and variables and create an instance of SequenceNumber object. That way you could keep a couple of different instance numbers separately.

Simple snippet:

public class SequenceNumber {

 // Your hashmap here:
 Map<String, Integer> map = new HashMap<String, Integer>();

 public int getSequenceNumber (String TcOrderId) {
  // ...
 }

public static void main(String[] args) throws IOException {

    // Instance of SequenceNumber object:
    SequenceNumber sequenceNumber = new SequenceNumber();

    String sCurrentLine;
    BufferedReader br = null;

    // ...
        while ((sCurrentLine = br.readLine()) != null) {
            //String orderid = sCurrentLine.substring(0, 6);
            System.out.println("reading line " +sCurrentLine);
            int seqvalue = sequenceNumber.getSequenceNumber(sCurrentLine);
            System.out.println("seqvalue "+seqvalue);
        }
    // ...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM