简体   繁体   中英

Repeatable Collection in Java

I was wondering if we had a repeatable key, value function in java? I'm working on some key value with Hashtable.The problem is I can't add any key that has been existed in the table. would you happen to know any method for that?

this is a part of my code:

public class SymbolTable {
   public static Hashtable<Object,Object> rows = new Hashtable<>();
//    Hashtable
    static int counter=1;
    public void addKeyword(Object keyword){
        rows.put(keyword,counter);
        counter++;
    }
    public void InstallID(){
    rows.put("id ",counter);
    counter++;
    }
    public void otherTokens(String lexeme,Object attribute){
        rows.put(lexeme,attribute);
        counter++;
    }
    public void addnumber(String lexeme , Object attribute){
        rows.put(lexeme,attribute);
        counter++;
    }

    public Hashtable gettokens(){
        return rows;
    }
}

There is no such thing in the Java standard library.

One option is to create a Hashmap whose values are ArrayList s. Wrap this into your own class for ease of use.

You could also use a ready-made implementation, for example, Multimap from the Guava library.

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