0

I want to get a map with order and duplicates.

Map<String, Boolean> map = ?;

map.put("abc", true);
map.put("cba", true);
map.put("cba", true);
map.put("bca", false);
map.put("bac", false);
map.put("bca", true);
map.put("cab", true);
map.put("cba", true);
map.put("cba", false);
map.put("cba", true);

System.out.println(map);

I used LinkedHashMap, but It doesn't allow duplicates.

Expected:  {abc=true, cba=true, cba=true, bca=false, bac=false, bca=true, cab=true, cba=true, cba=false, cba=true}
Retrieved: {abc=true, cba=true, bca=true, bac=true, cab=true}

Is there any implementation of map interface to achieve my goal?

UPD

Guava MultiMap doesn't fit into my issue.

Multimap<String, Boolean> map = ArrayListMultimap.<String, Boolean>create();

It prints

{cab=[true], abc=[true], cba=[true, true, true, false, true], bac=[false], bca=[false, true]}

UPD2

I found solution. Apache Commons provides Tuple type Pair.

So my code may look like this

List<Pair<String, Boolean>> list = new ArrayList<>();

list.add(new ImmutablePair("abc", true));
list.add(new ImmutablePair("cba", true));
list.add(new ImmutablePair("cba", true));
list.add(new ImmutablePair("bca", true));
list.add(new ImmutablePair("bac", true));
list.add(new ImmutablePair("bca", true));
list.add(new ImmutablePair("cab", true));
list.add(new ImmutablePair("cba", true));
list.add(new ImmutablePair("cba", true));

And now it prints

[(abc,true), (cba,true), (cba,true), (bca,true), (bac,true), (bca,true), (cab,true), (cba,true), (cba,true)]

But I'm still sure that it just should be some good implementation of proper map.

5
  • Just put an array of boolean as the value, and fill it for each key. " A map cannot contain duplicate keys" docs.oracle.com/javase/7/docs/api/java/util/Map.html Commented Feb 18, 2015 at 16:01
  • If there is one, it violates the interface. From the Map's interface Javadoc: "An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value." What you want, is no map. Commented Feb 18, 2015 at 16:01
  • 1
    Consider Guava's Multimap. Commented Feb 18, 2015 at 16:01
  • Or Apache's MultiValueMap. Commented Feb 18, 2015 at 16:05
  • I suspect Guava's LinkedListMultimap would do the job where ArrayListMultimap would not. Commented Feb 18, 2015 at 17:04

2 Answers 2

1

You can do the following to store an ordered list with duplicates.

List<String> map = new ArrayList<>();

map.add("abc" + '=' + true);
map.add("cba" + '=' + true);
map.add("cba" + '=' + true);
map.add("bca" + '=' + false);
map.add("bac" + '=' + false);
map.add("bca" + '=' + true);
map.add("cab" + '=' + true);
map.add("cba" + '=' + true);
map.add("cba" + '=' + false);
map.add("cba" + '=' + true);

System.out.println(map);

To create a Tuple class you can do

public class Tuple<L,R> {
    private L left;
    private R right;

    Tuple(L left, R right) {
        this.left = left;
        this.right = right;
    }

    public static <L, R> Tuple<L, R> of(L left, R right) { 
        return new Tuple<>(left, right); 
    }

    public L left() { return left; }
    public R right() { return right; }

    public String toString() { return left + "=" + right; }

}

However, in Java it is generally preferred to give this class and field names a meaningful name and type instead of having tuples of generic data with no meaning attached.

Sign up to request clarification or add additional context in comments.

2 Comments

I want something like Tuple in Scala. Is it possible to set List<Tuple>?
@barbara yes, though there is not a built in one, you can create one
0

Apparently you are concerned about the format of the output that you print in your map. If that is the case, then you could use the implementations already in place and simply override the toString() method.

If for some reason you would need information about the the absolute order in which entries were entered to your map, you would need to write on implementation yourself that does this for you, since using current implementations would only keep the ordering relative to each key, but not the absolute order.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.