Using hashcode for a unique ID

前端 未结 4 802
栀梦
栀梦 2020-12-17 20:31

I am working in a java-based system where I need to set an id for certain elements in the visual display. One category of elements is Strings, so I decided to use the String

4条回答
  •  猫巷女王i
    2020-12-17 21:24

    What you can do when you only have 30-50 values as you said is register each String you get into an HashMap together with a running counter as value:

    HashMap StringMap = new HashMap();
    
    StringMap.add("Test",1);
    StringMap.add("AnotherTest",2);
    

    You can then get your unique ID by calling this:

    StringMap.get("Test"); //returns 1
    

提交回复
热议问题