JsonHelper { static JSONArray extractTopTenHashtags(String tweetsArray) { JSONArray toptenArray = new JSONArray(); Map<String, Integer> hm = new TreeMap<>(); Pattern p = Pattern.compile("#\\w+"); Matcher m = p.matcher(tweetsArray); while (m.find()) { String tag = m.group(); tag = tag.toLowerCase(); if (!hm.containsKey(tag)) { hm.put(tag, 1); } else { hm.put(tag, (Integer) hm.get(tag) + 1); } } Map sortedMap = sortByValues(hm); Set set = sortedMap.entrySet(); Iterator iterator = set.iterator(); int counter = 1; while (iterator.hasNext() && counter <= 10) { counter++; Map.Entry me = (Map.Entry) iterator.next(); toptenArray.put(me.getKey()); } return toptenArray; }  static String generateCoordinates(String json); static String createTweetsWithHashtagRanking(String tweetsArray); static String createTweetsWithoutHashtagRanking(String tweetsArray); static final String KEY_COORDINATES; static final String KEY_PLACE; static final String KEY_TWEETS; static final String KEY_TOPTEN; }
@Test public void extractTopTenHashtagsTest() throws Exception { JSONArray hashtags = JsonHelper.extractTopTenHashtags(tweetsArray); assertTrue(hashtags.length() == 10); assertTrue(Objects.equals(hashtags.getString(0), "#yolo")); assertTrue(Objects.equals(hashtags.getString(1), "#dirili")); System.out.println(hashtags.getString(2) + " <<-->> " + "#cosmoblogawards"); assertTrue(Objects.equals(hashtags.getString(2), "#cosmoblogawards")); }