private List<PieSort> topX(Map<String, Integer> map, int top) { List<PieSort> list = new ArrayList<>(map.keySet().size() + 1); // Add the key/value pairs to the list containing the PieSort(key,value) object. for (String key : map.keySet()) { PieSort p = new PieSort(key + " - " + map.get(key) + "×", key, map.get(key)); list.add(p); } Collections.sort(list); // Collect the bottom of the list for the "Other" category. if (top < list.size()) { // Add the "Other" category up. int other = 0; for (PieSort p : list.subList(top, list.size())) other += p.value; list = list.subList(0, top); if (other > 0) list.add(new PieSort("Other - " + other + "×", "Other", other)); } return list; }
private List<PieSort> topX(Map<String, Integer> map, int top) { List<PieSort> list = new ArrayList<PieSort>(map.keySet().size() + 1); List<PieSort> bottomList; // Add the key/value pairs to the list containing the PieSort(key,value) object. for (String key : map.keySet()) { PieSort p = new PieSort(key + " - " + map.get(key), map.get(key)); list.add(p); } Collections.sort(list); // Collect the bottom of the list for the "Other" category. int other = 0; if (top < list.size()) { bottomList = list.subList(top, list.size()); // Add the "Other" category up. for (PieSort p : bottomList) { other += p.value; } list = list.subList(0, top); } if (other > 0) { list.add(new PieSort("Other" + " - " + other, other)); } return list; }
private List<PieSort> topX(Map<String, Integer> map, int top) { List<PieSort> list = new ArrayList<>(map.keySet().size() + 1); // Add the key/value pairs to the list containing the PieSort(key,value) object. for (String key : map.keySet()) { PieSort p = new PieSort(key + " - " + map.get(key) + "×", key, map.get(key)); list.add(p); } Collections.sort(list); // Collect the bottom of the list for the "Other" category. if (top < list.size()) { // Add the "Other" category up. int other = 0; for (PieSort p : list.subList(top, list.size())) other += p.value; list = list.subList(0, top); if (other > 0) list.add(new PieSort("Other - " + other + "×", "Other", other)); } return list; }