barbara
barbara

Reputation: 3201

JAVA: Indexed map with duplicates

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.

Upvotes: 0

Views: 107

Answers (2)

Jadiel de Armas
Jadiel de Armas

Reputation: 8802

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.

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533580

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.

Upvotes: 1

Related Questions