A java list that only shows the items once, but counts their occurence?

I'd like to create a list, that stores the same kind of objects, for example:

class Card {
    String title;
    int point;

    public Card(String t, int p) {
        title = t;
        point = p;
    }
}

And I add some objects to the list:

list.add(new Card("Fred",3));
list.add(new Card("Fred",1));
list.add(new Card("Luke",5));
list.add(new Card("John",3));

How can I do the following tasks? Create a new list, which contains the previous elements, but every title once, and counted their occurences. For example:

Fred 2
Luke 1
John 1

I can do the first part, I just make a HashSet, and overwrite the Song's 'equals' and 'hashCode' method. But I don't want to delete two with the same title completely.

Answers


Map<String, Card> cardsByTitle = new TreeMap<>();

void add(String title, int point) {
    Card card = cardsByTitle.get(title);
    if (card == null) {
        card = new Card(title, point);
        cardsByTitle.put(title, point);
    } else {
        card.point += point;
    }
}

for (Card card : cardsByTitle.values) { }

Or something similar. TreeMap (i.o. HashMap) keeps the titles ordered.


You can implement your own List, that internally stores its elements in an ArrayList and keeps a Map of the number of Cards with a given title:

public static class CountingList extends AbstractList<Card>{

    private final List<Card> list;
    private final Map<String, Integer> counterMap;

    public CountingList() {
        this(10);
    }

    public CountingList(Collection<? extends Card> c) {
        this(c.size());
        addAll(c);
    }

    public CountingList(int initialCapacity) {
        super();
        this.list = new ArrayList<>(initialCapacity);
        this.counterMap = new HashMap<>(initialCapacity);
    }


    @Override
    public boolean add(Card e) {
        Integer count = counterMap.get(e.title);
        String key = e.title;
        if (count == null) {
            list.add(e);
            count = 0;
        }
        counterMap.put(key, ++count);
        return count == 1;
    }

    @Override
    public void add(int index, Card element) {
        throw new UnsupportedOperationException();
    }

    @Override
    public Card remove(int index) {
         throw new UnsupportedOperationException();
    }

    @Override
    public Card get(int index) {
        return list.get(index);
    }

    @Override
    public int size() {
        return list.size();
    }

    public int getNumberOfCards(int index){
        return getNumberOfCards(list.get(index).title);
    }

    public int getNumberOfCards(String title){
        Integer count = counterMap.get(title);
        return count == null ? 0 : count;
    }

    @Override
    public String toString() {
        StringBuilder b = new StringBuilder();
        b.append('[');
        for (Card c : this) {
            b.append(c.title).append('(').append(getNumberOfCards(c.title)).append(')');
        }
        b.append(']');
        return b.toString();
    }
}

Then you can use this List like so:

public static void main(String[] args) {
    List<Card> list = new ArrayList<>();
    list.add(new Card("Fred",3));
    list.add(new Card("Fred",1));
    list.add(new Card("Luke",5));
    list.add(new Card("John",3));

    CountingList countingList = new CountingList(list);
    System.out.println(countingList);

    countingList.add(new Card("Tom", 1));
    countingList.add(new Card("Tom", 10));
    countingList.add(new Card("Tom", 15));
    System.out.println(countingList);

    countingList.add(new Card("Fred", 4));
    System.out.println(countingList);
}

Note, that inserting elements at a given index and removing elements is not yet supported for this List.

To get the number of the cards with a given title, you can call CountingList#getNumberOfCards.


Need Your Help

Read Wildcard As Asterisk

excel vba

I had a piece of code commissioned earlier this week (cheaper to have an expert write it than for me to spend a week trying to!). However, when putting it use I've hit a bit of a snag.

MySQL condition

mysql conditional-statements

I'm still pretty new with MySQL and I don't know the best way to do this.