0

I have a class like this:

public class StringCheckLHMapSorted<K, V>
    extends
    ObjectLHMapSorted<K, V> {

I have a LinkedHashMap inside declared as follows:

private final Map<Integer, StringCheck> scoLHMap =
        (Map<Integer, StringCheck>) new LinkedHashMap<K, V>();

The class StringCheck has this attributes with its respective setters/getters and nothing else inside:

private boolean check;
private String string;

I created a method inside the class described at the top of this question like this:

public LinkedHashMap<Boolean, String> getSCLHAllStringAndCheckInside() {
LinkedHashMap<Boolean, String> allStrCh = new LinkedHashMap<Boolean, String>();

for (Map.Entry<Integer, StringCheck> e : scoLHMap.entrySet()) {
        allStrCh.put(e.getValue().isCheck(), e.getValue().getString());
}

    return allStrCh;
}

The object returned allStrCh gives me only one entry; the last one inserted to be more specific. I've replaced allStrCh with an ArrayList to get only keys/values and it gives me the complete list of each one of them; keys/values.

Why is this happening? Am I missing something?

John Jones
  • 33
  • 3
  • Why doesn't your backing map use the bounds `K` and `V` instead of `Integer` and `StringCheck`? – Makoto Dec 03 '13 at 07:07
  • 1
    Well you've got a map from `Boolean` to `String` - there are only two `Boolean` values (false and true) so your map *can* only contain two entries. If it's only got one entry, then presumably `isCheck()` was false for all entries, or it was true for all entries. – Jon Skeet Dec 03 '13 at 07:09
  • Makoto, ObjectLHMapSorted expects a more wider range of keys/values. – John Jones Dec 03 '13 at 07:35
  • Jon Skeet, scoLHMap holds , so, I want a list like this: --Name : 3loagt, Status: true-- for every key in scoLHMap – John Jones Dec 03 '13 at 07:37
  • You were right Jon Skeet. I used them in the reverse way. String to Boolean. Thanks. – John Jones Dec 03 '13 at 07:53

1 Answers1

0

LinkedHashMap just preserves the order of insertion - if you .put(true,x) and then .put(true,y) it will still overwrite whatever was at the true key previously.

You probably want to do something like this? HashMap with multiple values under the same key

Community
  • 1
  • 1
Tom McClure
  • 6,699
  • 1
  • 21
  • 21
  • I get it. I can not have duplicates. So you recommend me use them in the reverse way. I mean . The strings are 6 random characters. – John Jones Dec 03 '13 at 07:44