2015-11-11 20:20:04 -08:00
|
|
|
|
// onjava/CountingMapData.java
|
2015-11-14 16:18:05 -08:00
|
|
|
|
// <20>2016 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.
|
|
|
|
|
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
2015-06-15 17:47:35 -07:00
|
|
|
|
// Unlimited-length Map containing sample data.
|
2015-11-11 20:20:04 -08:00
|
|
|
|
package onjava;
|
2015-06-15 17:47:35 -07:00
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
public class CountingMapData
|
|
|
|
|
extends AbstractMap<Integer,String> {
|
|
|
|
|
private int size;
|
|
|
|
|
private static String[] chars =
|
|
|
|
|
"A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"
|
|
|
|
|
.split(" ");
|
|
|
|
|
public CountingMapData(int size) {
|
|
|
|
|
if(size < 0) this.size = 0;
|
|
|
|
|
else this.size = size;
|
|
|
|
|
}
|
|
|
|
|
private static class Entry
|
|
|
|
|
implements Map.Entry<Integer,String> {
|
|
|
|
|
int index;
|
|
|
|
|
Entry(int index) { this.index = index; }
|
|
|
|
|
@Override
|
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
|
return Integer.valueOf(index).equals(o);
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public Integer getKey() { return index; }
|
|
|
|
|
@Override
|
|
|
|
|
public String getValue() {
|
|
|
|
|
return
|
|
|
|
|
chars[index % chars.length] +
|
|
|
|
|
Integer.toString(index / chars.length);
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public String setValue(String value) {
|
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public int hashCode() {
|
|
|
|
|
return Integer.valueOf(index).hashCode();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public Set<Map.Entry<Integer,String>> entrySet() {
|
|
|
|
|
// LinkedHashSet retains initialization order:
|
|
|
|
|
Set<Map.Entry<Integer,String>> entries =
|
|
|
|
|
new LinkedHashSet<>();
|
|
|
|
|
for(int i = 0; i < size; i++)
|
|
|
|
|
entries.add(new Entry(i));
|
|
|
|
|
return entries;
|
|
|
|
|
}
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
System.out.println(new CountingMapData(60));
|
|
|
|
|
}
|
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}
|
2015-09-07 11:44:36 -06:00
|
|
|
|
*/
|