简体   繁体   English

HASHMAP,存在的密钥仍给出“假”结果

[英]HASHMAP, key present still giving “false” result

Following is my code that returning false even if the key exists: 以下是我的代码,即使该键存在,它也会返回false:

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
1233 1233
1234 1234

The result should be 结果应该是
1 1个
2 2
1 1个

But everytime its going in the else loop and the result is 但是每次它进入else循环时,结果是
1 1个
1 1个
1 1个

I am trying to use HASHMAP as I am creating my own index. 我在创建自己的索引时尝试使用HASHMAP。

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. 每次调用getSequenceNumber函数时,都会在CODE中创建新的HashMap。 I believe this is not something you want. 我相信这不是您想要的。

To avoid that - you can simply move Map<String, Integer> map = new HashMap<String, Integer>(); 为了避免这种情况-您只需移动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. 由于函数getSequenceNumber是静态函数-您需要将变量设为静态。 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. (也许更好)是避免静态函数和变量,并创建SequenceNumber对象的实例。 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);
        }
    // ...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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