My question might sound silly, but would like to know whether there are any Collection object in Java that does store index,key and values in a single Collection object?
我的问题可能听起来很愚蠢,但是想知道Java中是否有任何Collection对象在单个Collection对象中存储索引,键和值?
I have the following:
我有以下内容:
Enumeration hs = request.getParameterNames();
LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<String, String>();
while (hs.hasMoreElements()) {
linkedHashMap.put(value, request.getParameter(value));
}
The above stores key and value in linkedHashMap, but it doesn't have an index. If it has then I could call by index(pos) and get corresponding key and value.
以上内容在linkedHashMap中存储键和值,但它没有索引。如果它有,那么我可以通过索引(pos)调用并获得相应的键和值。
Edit 1
编辑1
I would want to conditionally check if index(position) is x then get the corresponding key and value pair and construct a string with query.
我想条件检查索引(位置)是否为x然后获取相应的键和值对并构造一个带查询的字符串。
3 个解决方案
#1
3
As mentioned by others, Java collections does not support this. A workaround is Map<R, Map<C, V>>
. But it is too ugly to use. You can go with Guava. It provides a Table collection type. It has the following format Table<R, C, V>
. I haven't tried this but I think this will work for you.
正如其他人所提到的,Java集合不支持这一点。解决方法是Map
Enumeration hs = request.getParameterNames();
Table<Integer, String, String> table = HashBasedTable.create();
while (hs.hasMoreElements()) {
table.put(index, value, request.getParameter(value));
}
Now, if you want key, value pair at, let's say, index 1. Just do table.row(1)
. Similarly, to get index, value pairs just do table.column(value)
.
现在,如果你想要键,值对,比方说,索引1.只需要做table.row(1)。同样,要获取索引,值对只需执行table.column(value)。
#2
1
No Collection in java, will support this. You need to create a new class IndexedMap inheriting HashMap and store the key object into the arraylist by overriding put method.
在java中没有Collection,会支持这个。您需要创建一个继承HashMap的新类IndexedMap,并通过重写put方法将键对象存储到arraylist中。
here is the answer(answerd by another user: Adriaan Koster)
这是答案(另一位用户回答:Adriaan Koster)
how to get the one entry from hashmap without iterating
如何从hashmap中获取一个条目而不进行迭代
#3
1
Maybe you need implementing yourself for achieving this functionality.
也许你需要实现自己来实现这个功能。
public class Param{
private String key;
private String value;
public Param(String key, String value){
this.key = key;
this.value = value;
}
public void setKey(String key){
this.key = key;
}
public String getKey(){
return this.key;
}
public void setValue(String value){
this.value = value;
}
public String getValue(){
return this.value;
}
}
Enumeration hs = request.getParameterNames();
List<Param> list = new ArrayList<Param>();
while (hs.hasMoreElements()) {
String key = hs.nextElement();
list.add(new Param(key, request.getParameter(key)));
}
By doing this, you could get param with an index provided by List API.
通过这样做,您可以获得由List API提供的索引的参数。
#1
3
As mentioned by others, Java collections does not support this. A workaround is Map<R, Map<C, V>>
. But it is too ugly to use. You can go with Guava. It provides a Table collection type. It has the following format Table<R, C, V>
. I haven't tried this but I think this will work for you.
正如其他人所提到的,Java集合不支持这一点。解决方法是Map
Enumeration hs = request.getParameterNames();
Table<Integer, String, String> table = HashBasedTable.create();
while (hs.hasMoreElements()) {
table.put(index, value, request.getParameter(value));
}
Now, if you want key, value pair at, let's say, index 1. Just do table.row(1)
. Similarly, to get index, value pairs just do table.column(value)
.
现在,如果你想要键,值对,比方说,索引1.只需要做table.row(1)。同样,要获取索引,值对只需执行table.column(value)。
#2
1
No Collection in java, will support this. You need to create a new class IndexedMap inheriting HashMap and store the key object into the arraylist by overriding put method.
在java中没有Collection,会支持这个。您需要创建一个继承HashMap的新类IndexedMap,并通过重写put方法将键对象存储到arraylist中。
here is the answer(answerd by another user: Adriaan Koster)
这是答案(另一位用户回答:Adriaan Koster)
how to get the one entry from hashmap without iterating
如何从hashmap中获取一个条目而不进行迭代
#3
1
Maybe you need implementing yourself for achieving this functionality.
也许你需要实现自己来实现这个功能。
public class Param{
private String key;
private String value;
public Param(String key, String value){
this.key = key;
this.value = value;
}
public void setKey(String key){
this.key = key;
}
public String getKey(){
return this.key;
}
public void setValue(String value){
this.value = value;
}
public String getValue(){
return this.value;
}
}
Enumeration hs = request.getParameterNames();
List<Param> list = new ArrayList<Param>();
while (hs.hasMoreElements()) {
String key = hs.nextElement();
list.add(new Param(key, request.getParameter(key)));
}
By doing this, you could get param with an index provided by List API.
通过这样做,您可以获得由List API提供的索引的参数。