OnJava8-Examples/onjava/CountMap.java

92 lines
2.4 KiB
Java
Raw Permalink Normal View History

2016-07-05 14:46:09 -06:00
// onjava/CountMap.java
// (c)2021 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2016-01-25 18:05:55 -08:00
// Unlimited-length Map containing sample data
2016-07-28 12:48:23 -06:00
// {java onjava.CountMap}
package onjava;
2015-06-15 17:47:35 -07:00
import java.util.*;
2017-01-08 22:55:49 -08:00
import java.util.stream.*;
2015-06-15 17:47:35 -07:00
2016-07-05 14:46:09 -06:00
public class CountMap
2015-06-15 17:47:35 -07:00
extends AbstractMap<Integer,String> {
private int size;
2016-07-05 14:46:09 -06:00
private static char[] chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
private static String value(int key) {
return
chars[key % chars.length] +
Integer.toString(key / chars.length);
}
public CountMap(int size) {
2017-01-09 14:26:12 -08:00
this.size = size < 0 ? 0 : size;
2015-06-15 17:47:35 -07:00
}
@Override public String get(Object key) {
2016-07-05 14:46:09 -06:00
return value((Integer)key);
}
2015-06-15 17:47:35 -07:00
private static class Entry
implements Map.Entry<Integer,String> {
int index;
Entry(int index) { this.index = index; }
@Override public boolean equals(Object o) {
2017-01-08 22:55:49 -08:00
return o instanceof Entry &&
Objects.equals(index, ((Entry)o).index);
2015-06-15 17:47:35 -07:00
}
@Override public Integer getKey() { return index; }
@Override public String getValue() {
2017-01-22 16:48:11 -08:00
return value(index);
}
@Override public String setValue(String value) {
2015-06-15 17:47:35 -07:00
throw new UnsupportedOperationException();
}
@Override public int hashCode() {
2017-01-10 14:11:16 -08:00
return Objects.hashCode(index);
2015-06-15 17:47:35 -07:00
}
}
@Override
public Set<Map.Entry<Integer,String>> entrySet() {
// LinkedHashSet retains initialization order:
2017-01-08 22:55:49 -08:00
return IntStream.range(0, size)
.mapToObj(Entry::new)
2017-01-22 16:48:11 -08:00
.collect(Collectors
.toCollection(LinkedHashSet::new));
2015-06-15 17:47:35 -07:00
}
public static void main(String[] args) {
2017-05-01 14:33:10 -06:00
final int size = 6;
2016-07-05 14:46:09 -06:00
CountMap cm = new CountMap(60);
System.out.println(cm);
System.out.println(cm.get(500));
2017-01-09 14:26:12 -08:00
cm.values().stream()
2017-05-01 14:33:10 -06:00
.limit(size)
2017-01-09 14:26:12 -08:00
.forEach(System.out::println);
System.out.println();
2017-05-01 14:33:10 -06:00
new Random(47).ints(size, 0, 1000)
2017-01-09 14:26:12 -08:00
.mapToObj(cm::get)
.forEach(System.out::println);
2015-06-15 17:47:35 -07:00
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
{0=A0, 1=B0, 2=C0, 3=D0, 4=E0, 5=F0, 6=G0, 7=H0, 8=I0,
9=J0, 10=K0, 11=L0, 12=M0, 13=N0, 14=O0, 15=P0, 16=Q0,
17=R0, 18=S0, 19=T0, 20=U0, 21=V0, 22=W0, 23=X0, 24=Y0,
25=Z0, 26=A1, 27=B1, 28=C1, 29=D1, 30=E1, 31=F1, 32=G1,
33=H1, 34=I1, 35=J1, 36=K1, 37=L1, 38=M1, 39=N1, 40=O1,
41=P1, 42=Q1, 43=R1, 44=S1, 45=T1, 46=U1, 47=V1, 48=W1,
49=X1, 50=Y1, 51=Z1, 52=A2, 53=B2, 54=C2, 55=D2, 56=E2,
57=F2, 58=G2, 59=H2}
2016-07-05 14:46:09 -06:00
G19
2017-01-09 14:26:12 -08:00
A0
B0
C0
D0
E0
F0
2017-01-10 14:11:16 -08:00
Y9
J21
R26
D33
Z36
N16
2015-09-07 11:44:36 -06:00
*/