public class Customer {
private static long counter=1;
private final long id=counter++;
public Customer() {
}
@Override
public String toString() {
return "Customer{" +
"id=" + id +
'}';
}
public static Generator generator(){
return new Generator() {
@Override
public Customer next() {
return new Customer();
}
};
}
}
public class Teller {
private static long customer = 1;
private final long id = customer++;
public Teller() {
}
@Override
public String toString() {
return "Teller{" +
"id=" + id +
'}';
}
public static Generator tellerGenerator(){
return new Generator() {
@Override
public Teller next() {
return new Teller();
}
};
}
}
public class Generators {
static Collection fill(Collection collection,
Generator generator,
int n) {
for (int i = 0; i < n; i++)
collection.add(generator.next());
return collection;
}
}
public class BankTeller {
static void serve(Teller teller,Customer customer){
System.out.println("Teller: "+teller+" Customer: "+customer);
}
public static void main(String[] args) {
Random random=new Random(47);
Queue line=new LinkedList<>();
//向集合中填充数据
Generators.fill(line,Customer.generator(),15);
List tellerList=new ArrayList<>();
//同上
Generators.fill(tellerList,Teller.tellerGenerator(),4);
//循环遍历集合
line.stream().forEach(each->serve(tellerList.get(random.nextInt(tellerList.size())),each));
}
}
//运行结果为
Teller: Teller{id=3} Customer: Customer{id=1}
Teller: Teller{id=2} Customer: Customer{id=2}
Teller: Teller{id=3} Customer: Customer{id=3}
Teller: Teller{id=1} Customer: Customer{id=4}
Teller: Teller{id=1} Customer: Customer{id=5}
Teller: Teller{id=3} Customer: Customer{id=6}
Teller: Teller{id=1} Customer: Customer{id=7}
Teller: Teller{id=2} Customer: Customer{id=8}
Teller: Teller{id=3} Customer: Customer{id=9}
Teller: Teller{id=3} Customer: Customer{id=10}
Teller: Teller{id=2} Customer: Customer{id=11}
Teller: Teller{id=4} Customer: Customer{id=12}
Teller: Teller{id=2} Customer: Customer{id=13}
Teller: Teller{id=1} Customer: Customer{id=14}
Teller: Teller{id=1} Customer: Customer{id=15}
//例如,我们可以很容易地创建List 元组 如下
public class TwoTuple {
public final A a;
public final B b;
public TwoTuple(A a, B b) {
this.a = a;
this.b = b;
}
@Override
public String toString() {
return "TwoTuple{" +
"a=" + a +
", b=" + b +
'}';
}
}
public class ThreeTuple extends TwoTuple {
public final C c;
public ThreeTuple(A a, B b, C c) {
super(a, b);
this.c = c;
}
@Override
public String toString() {
return "ThreeTuple{" +
"c=" + c +
", a=" + a +
", b=" + b +
'}';
}
}
class FourTuple extends ThreeTuple {
public final D d;
public FourTuple(A a, B b, C c, D d) {
super(a, b, c);
this.d = d;
}
@Override
public String toString() {
return "FourTuple{" +
"c=" + c +
", d=" + d +
", a=" + a +
", b=" + b +
'}';
}
}
class Amphibian {}
class Vehicle {}
class TupleTest {
static TwoTuple f() {
return new TwoTuple<>("hi", 47);
}
}
public class TupleList extends ArrayList> {
public static void main(String[] args) {
TupleList tupleList=new TupleList<>();
//向集合中添加数据
tupleList.add(TupleTest.h());
tupleList.add(TupleTest.h());
//循环集合
tupleList.forEach(each->{
System.out.println(each);
});
}
}
//运行结果为
FiveTuple{c=hi, d=47, e=11.1, a=generic.Vehicle@817b38, b=generic.Amphibian@4437c4}
FiveTuple{c=hi, d=47, e=11.1, a=generic.Vehicle@13c675d, b=generic.Amphibian@191beef}
//构建的模型是一个零售店,它包括走廊,货架和商品。
public class Product {
private final int id;
private String description;
private double price;
public Product(int id, String description, double price) {
this.id = id;
this.description = description;
this.price = price;
toString();
}
@Override
public String toString() {
return "Product{" +
"id=" + id +
", description='" + description + '\'' +
", price=" + price +
'}'+this.getClass().getName();
}
void priceChange(double change) {
price += price;
}
static Generator generator = new Generator() {
Random random = new Random(47);
@Override
public Product next() {
return new Product(random.nextInt(1000), "Test", Math.round(random.nextDouble() * 1000) + 0.99);
}
};
}
class Shelf extends ArrayList {
public Shelf(int initialCapacity) {
Generators.fill(this, Product.generator, initialCapacity);
}
}
class Aisle extends ArrayList {
public Aisle(int nShelves, int initialCapacity) {
for (int i = 0; i < nShelves; i++) {
add(new Shelf(initialCapacity));
}
}
}
class CheckoutStand {}
class Office {}
class Store extends ArrayList {
List checkoutStands = new ArrayList<>();
private Office office = new Office();
public Store(int nAisle, int nShelvs, int initialCapacity) {
for (int i = 0; i < nAisle; i++) {
add(new Aisle(nShelvs, initialCapacity));
}
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
for (Aisle aisle:this) {
for (Shelf shelf:aisle) {
for (Product product:shelf) {
builder.append(product);
builder.append("\n");
}
}
}
return builder.toString();
}
public static void main(String[] args) {
System.out.println(new Store(2,2,2));
}
}
//运行结果为
Product{id=258, description='Test', price=400.99}generic.Product
Product{id=861, description='Test', price=160.99}generic.Product
Product{id=868, description='Test', price=417.99}generic.Product
Product{id=207, description='Test', price=268.99}generic.Product
Product{id=551, description='Test', price=114.99}generic.Product
Product{id=278, description='Test', price=804.99}generic.Product
Product{id=520, description='Test', price=554.99}generic.Product
Product{id=140, description='Test', price=530.99}generic.Product
//尽管你可以声明 ArrayList.class ,但是你不能声明 ArrayList.class。
public class ErasedTypeEquivalence {
public static void main(String[] args) {
Class c1 = new ArrayList().getClass();
Class c2 = new ArrayList().getClass();
System.out.println(c1 == c2);
}
}
//运行结果为
true
//这是对 谜题的一个补充
class Frob{}
class Fnorkle{}
class Quark{}
class Particle{}
public class LostInformation {
public static void main(String[] args) {
List list=new ArrayList<>();
Map map=new HashMap<>();
Quark quark=new Quark<>();
Particle partFactory=new Particle();
System.out.println(Arrays.toString(list.getClass().getTypeParameters()));
System.out.println(Arrays.toString(map.getClass().getTypeParameters()));
System.out.println(Arrays.toString(quark.getClass().getTypeParameters()));
System.out.println(Arrays.toString(partFactory.getClass().getTypeParameters()));
}
}
//运行结果为
[E]
[K, V]
[Q]
[T, K]