反转属性的最短方法 [英] Shortest way to reverse Properties

查看:41
本文介绍了反转属性的最短方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,我有一个 java.util.Properties 对象,我想获得另一个对象,它们具有相同的对,但键转换为值,反之亦然.

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 collision (i.e. there are two equal values) then just pick as value an arbitrary key.

最短的方法是什么?

可以随意使用库,commons-collection或其他任何东西.

Feel free to use libraries, commons-collections, or whatever.

推荐答案

Properties 对象是 Hashtable 对象,因此您应该能够执行以下操作:

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

Hashtable<String, String> reversedProps = new Hashtable<String, String>();

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

结果:3行代码.

此代码未经测试,但可以给您一个提示.

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

这篇关于反转属性的最短方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆