4

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?

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.

Edit 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.

sdanzig
  • 4,510
  • 1
  • 23
  • 27
Jacob
  • 14,463
  • 65
  • 207
  • 320
  • No, there's not such collection in Java Collection API. – Luiggi Mendoza Nov 04 '13 at 06:04
  • @LuiggiMendoza If not what are the options available to achieve this functionality? – Jacob Nov 04 '13 at 06:05
  • 1
    It would be better if you explain your functional requirement instead. – Luiggi Mendoza Nov 04 '13 at 06:05
  • 1
    Anyway, if you know your `Map` isn't going to change, you can create a `List> indexedKV = new ArrayList(linkedHashMap.entrySet());` – Luiggi Mendoza Nov 04 '13 at 06:08
  • 1
    *construct a string with query* now I wonder even more why would you need to use index instead of just retrieving the needed value by the key... – Luiggi Mendoza Nov 04 '13 at 06:10
  • 1
    Do you ever access the map by the key? Or only by the index? – Jason C Nov 04 '13 at 06:10
  • @LuiggiMendoza The reason why I would like to call by index is because I would be certain that index(x) will have `a` and `b` has key value pair. – Jacob Nov 04 '13 at 06:20
  • 1
    If you have a known key K and a known index N, and you are looking up the item at index N, checking if its key is K, then grabbing its value, then you might as well just directly get the value from the key, since you already know what the key is. If you never access by key and only access by index, you might as well just not use a map at all. So it all depends. Please clarify why it matters that the key K is at a specific index N. Also please clarify your performance requirements (why not just manually iterate to position N in a `LinkedHashMap`?) – Jason C Nov 04 '13 at 06:25
  • @JasonC By iterating how can I get the position of key value pair? – Jacob Nov 04 '13 at 06:38
  • @Polppan `entrySet().iterator()`, start at the beginning, and advance N times... is there a reason you are avoiding answering requests to clarify your question? You won't get a good answer if you continue to ignore our requests. – Jason C Nov 04 '13 at 06:48

3 Answers3

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.

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).

jayantS
  • 817
  • 13
  • 29
  • Mandrake, good you have pointed this, why you have mentioned that it is too ugly to use? Any particular reason? – Jacob Nov 04 '13 at 07:20
  • 1
    @Polppan: In `Map>` you will be first creating a Map for _key, value_ then put this in the above `Map`. But `Table` lets you do this in just one step. This is my reason to say _ugly_. – jayantS Nov 04 '13 at 07:36
  • Thanks Mandrake, this is what I was precisely looking for. Not sure why people are voting to close this question. – Jacob Nov 04 '13 at 07:43
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.

here is the answer(answerd by another user: Adriaan Koster)

how to get the one entry from hashmap without iterating

Community
  • 1
  • 1
Raghu
  • 1,363
  • 1
  • 23
  • 37
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.

Howard
  • 4,474
  • 6
  • 29
  • 42