The output code is isolated in a single class that takes a Counter<T>
and a PrintStream
, for instance System.out
, but it could just as well be a file or a socket stream. Instead of manually adding spaces to smooth out and align the numbers, you can specify a minimum width specify a minimum width in Java format strings. For instance, %5s
will pad the text to maintain a width of 5 spaces.
The output code is isolated in a single class that takes a Counter<T>
and a PrintStream
, for instance System.out
, but it could just as well be a file or a socket stream. Instead of manually adding spaces to smooth out and align the numbers, you can specify a minimum width in Java format strings. For instance, %5s
will pad the text to maintain a width of 5 spaces.
The output code is isolated in a single class that takes a Counter<T>
and a PrintStream
, for instance System.out
, but it could just as well be a file or a socket stream. Instead of manually adding spaces to smooth out and align the numbers, you can specify a minimum width in Java format strings. For instance, %5s
will pad the text to maintain a width of 5 spaces.
public abstract class RandomDataSource<T> {
protected static final Random random = new Random();
public abstract T nextElement();
public Iterable<T> nextElements(int numberOfElements) {
Collection<T> elements = new ArrayList<T>ArrayList<>(numberOfElements);
for (int i = 0; i < numberOfElements; i++) {
elements.add(nextElement());
}
return elements;
}
}
public class Counter<T> implements Iterable<T> {
private Map<T, Integer> map = new HashMap<T, Integer>HashMap<>();
public void increment(T element) {
int count = map.containsKey(element) ? map.get(element) : 0;
map.put(element, count + 1);
}
public int getCount(T element) {
return map.get(element);
}
public Iterable<T> getLargestElements() {
int largest = Collections.max(map.values());
Collection<T> keys = new ArrayList<>();
for (T key : map.keySet()) {
if (map.get(key) == largest) {
keys.add(key);
}
}
return keys;
}
@Override
public Iterator<T> iterator() {
return map.keySet().iterator();
}
}
public abstract class RandomDataSource<T> {
protected static final Random random = new Random();
public abstract T nextElement();
public Iterable<T> nextElements(int numberOfElements) {
Collection<T> elements = new ArrayList<T>(numberOfElements);
for (int i = 0; i < numberOfElements; i++) {
elements.add(nextElement());
}
return elements;
}
}
public class Counter<T> implements Iterable<T> {
private Map<T, Integer> map = new HashMap<T, Integer>();
public void increment(T element) {
int count = map.containsKey(element) ? map.get(element) : 0;
map.put(element, count + 1);
}
public int getCount(T element) {
return map.get(element);
}
public Iterable<T> getLargestElements() {
int largest = Collections.max(map.values());
Collection<T> keys = new ArrayList<>();
for (T key : map.keySet()) {
if (map.get(key) == largest) {
keys.add(key);
}
}
return keys;
}
@Override
public Iterator<T> iterator() {
return map.keySet().iterator();
}
}
public abstract class RandomDataSource<T> {
protected static final Random random = new Random();
public abstract T nextElement();
public Iterable<T> nextElements(int numberOfElements) {
Collection<T> elements = new ArrayList<>(numberOfElements);
for (int i = 0; i < numberOfElements; i++) {
elements.add(nextElement());
}
return elements;
}
}
public class Counter<T> implements Iterable<T> {
private Map<T, Integer> map = new HashMap<>();
public void increment(T element) {
int count = map.containsKey(element) ? map.get(element) : 0;
map.put(element, count + 1);
}
public int getCount(T element) {
return map.get(element);
}
public Iterable<T> getLargestElements() {
int largest = Collections.max(map.values());
Collection<T> keys = new ArrayList<>();
for (T key : map.keySet()) {
if (map.get(key) == largest) {
keys.add(key);
}
}
return keys;
}
@Override
public Iterator<T> iterator() {
return map.keySet().iterator();
}
}