-1

I have been searching for hours through all the posts regarding LinkedHashMap but I seem to be missing the basics.

I will be generating a K,V sets from a db call, but no idea what each set will be. Some K,V will be processed and others not

I need to iterate the map, get the position (indexOf[i]??) + K(String). This will then be passed out to anther method to ask how that K is to be processed (put into 2nd array)

Based on the 2nd array, I will then process the required K,V sets calling the position[i].

ie 1) poll db for info, records return as follows - abc txt - def blah - ghi blah - jkl txt 2) loop records and ask 2nd script what to do - txt process like this ..... - blah do nothing - save indexOf for quick referral as K may change value. - change K,V if needed (spelling error corrected, remove spaces, strtolower etc) 3) based on further processing call indexOf as needed (K may have changed as above)

So is a LinkedHashMap the correct type to use?

From a PHP point of view seems easy, so I must be a bit thick on this.

Any code examples would be great.

art vanderlay
  • 2,341
  • 4
  • 35
  • 64

4 Answers4

1

Maps in Java don't support the concept of an index being assigned to each key-value mapping. This is true even for LinkedHashMap, which has the only advantage that the iteration order is predictable.

I didn't fully understand your question, but if it is "How can I get the index of a key-value mapping in a map", this is not directly possible. You can iterate through the map and count the keys you see before the one you are looking for, but of course this is of O(n) complexity and not faster.

In PHP this is different because the default data structure there is some combination of array/list and map, whereas in Java these are distinct data structures.

If you make your question clearer, perhaps we can provide a better-suited data structure for you to use.

Philipp Wendler
  • 11,184
  • 7
  • 52
  • 87
1

It sounds like you need to be able to efficiently find elements in your structure both by key and by position.

One way to achieve this with standard Java containers is by using a Map in conjunction with an ArrayList (both storing references to the same objects). The former enables fast lookups by key; the latter, by position.

Insertions into such a structure would be O(n). However, it sounds like you're building the structure once, and not modifying it afterwards.

Finally, if you don't need to be able quickly locate an element given its key, you can lose the Map and just use an ArrayList.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

Your question is not clear to me, but I think you are asking about how to iterate a map.

You can do this the following way:

    Map<String, String> map = new HashMap<String, String>();
    map.put("key1", "value1");
    for (Entry<String, String> entry : map.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
    }
Kai
  • 38,985
  • 14
  • 88
  • 103
0

Im not exactly sure what you want to do...

public void processDbResult(Map<K, V> map)
{
    // you got sth like this: 
    // Map<K, V> map = new LinkedHashMap<K, V>();
    for (Entry<K, V> entry : map.entrySet())
    {
        K key = entry.getKey();
        V value = entry.getValue();

        Object how = determine(key);

        process(how, entry);
    }
}

private void process(Object how, Entry<K, V> entry)
{
    // do something
}

private Object determine(K key)
{
    // determine processing steps
    return new Object();
}
helt
  • 4,925
  • 3
  • 35
  • 54