Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static class Multiset<E> {
- private NavigableMap<E, Integer> map;
- int totalElements;
- public Multiset() {
- map = new TreeMap<>();
- totalElements = 0;
- }
- public void add(E element) {
- map.put(element, map.getOrDefault(element, 0) + 1);
- totalElements += 1;
- }
- public void remove(E element) {
- int count = map.getOrDefault(element, 0);
- if(count > 0) {
- map.put(element, count - 1);
- count -= 1;
- if(count == 0) {
- map.remove(element);
- }
- totalElements -= 1;
- }
- }
- public int count(E element) {
- return map.getOrDefault(element, 0);
- }
- public int size() {
- return totalElements;
- }
- public boolean isEmpty() {
- return map.isEmpty();
- }
- public E firstEntry() {
- Map.Entry<E, Integer> entry = map.firstEntry();
- return entry.getKey();
- }
- public E lastEntry() {
- Map.Entry<E, Integer> entry = map.lastEntry();
- return entry.getKey();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement