Source code

    001/*
    002# Licensed Materials - Property of IBM
    003# Copyright IBM Corp. 2015
    004 */
    005package twitter;
    006
    007import java.util.ArrayList;
    008import java.util.Collections;
    009import java.util.Comparator;
    010import java.util.HashMap;
    011import java.util.List;
    012import java.util.Map.Entry;
    013
    014/**
    015 * A container for a hashmap that keeps the top ten of whatever is in the map.
    016 */
    017public class Trender {
    018    // Overall hashmap for hashtags -- maintains the count for each hashtag
    019    private HashMap<String, Integer> hmap = new HashMap<String, Integer>();
    020
    021    public HashMap<String, Integer> getMap() {
    022        return hmap;
    023    }
    024
    025    /*
    026     * Adds a hashtag to the table of hashtags, increasing its count.
    027     */
    028    public void add(String s) {
    029        Integer val;
    030
    031        // Insert the hashtag into the map.
    032        if (!hmap.containsKey(s)) {
    033            val = 1;
    034            hmap.put(s, val);
    035        } else {
    036            val = hmap.get(s) + 1;
    037            hmap.put(s, val);
    038        }
    039    }
    040
    041    /**
    042     * Get a hashMap element
    043     *
    044     * @param s
    045     * @return an element of the hashmap
    046     */
    047    public Integer get(String s) {
    048        return hmap.get(s);
    049    }
    050
    051    /**
    052     * Gets the top ten most frequent hashtags as a list.
    053     *
    054     * @return A ListContainer containing the top ten most frequent hashtags.
    055     */
    056    public List<HashTagCount> getTopTen() {
    057        ArrayList<HashTagCount> topTen = new ArrayList<HashTagCount>();
    058
    059        // String[] topTen= new String[10];
    060        Integer minValue = 0;
    061        for (Entry<String, Integer> eset : hmap.entrySet()) {
    062            if (eset.getValue() < minValue) {
    063                continue;
    064            }
    065            if (topTen.size() > 10) {
    066                topTen.remove(0);
    067            }
    068            HashTagCount htc = new HashTagCount(eset.getKey(), eset.getValue());
    069            int idx = Collections.binarySearch(topTen, htc,
    070                    new Comparator<HashTagCount>() {
    071
    072                        @Override
    073                        public int compare(HashTagCount o1, HashTagCount o2) {
    074                            if (o1.getCount() > o2.getCount())
    075                                return 1;
    076                            else if (o1.getCount().equals(o2.getCount()))
    077                                return 0;
    078                            else
    079                                return -1;
    080                        }
    081                    });
    082            if (idx < 0)
    083                idx = (-1 * idx) - 1;
    084            topTen.add(idx, htc);
    085            minValue = topTen.get(0).getCount();
    086        }
    087        // Possible for the list to have 11 items
    088        if (topTen.size() > 10)
    089            topTen.remove(0);
    090        return topTen;
    091    }
    092
    093}