Shortest way to reverse Properties

后端 未结 3 1592
一生所求
一生所求 2021-01-22 09:05

In Java I have a java.util.Properties object and I want to obtain another one with the same pairs but keys converted to values and viceversa.

If there are c

3条回答
  •  难免孤独
    2021-01-22 10:08

    A Properties object is a Hashtable object, so you should be able to do something like:

    Hashtable reversedProps = new Hashtable();
    
    for (String key : props.keySet()) {
        reversedProps.put(props.get(key), key);
    }
    

    Result: 3 lines of code.

    This code is untested, but it should give you the idea.

提交回复
热议问题