package sunday;
import java.util.*;
//泛型
public class Demo {
public static void main(String[] args) {
//限制形式
Tools<String> t1 = new Tools<String>();
t1.show("罗宾");
//无限泛型
Tools t2 = new Tools("robin");
Tools t3 = new Tools("hancook");
//泛型方法
t2.show(3);
t2.show("汉库克");
System.out.println(t1.sum(1, 3, 4, 4));
//类型通配符
List<? extends Object> l1 = new ArrayList<Integer>();//子类
List<? super Integer> l2 = new ArrayList<Object>();//父类
//固定大小列表
List<String> l3 = Arrays.asList("罗宾", "汉库克", "佩佩");
l3.set(2, "达斯琪");
List<String> l4 = List.of("罗宾", "汉库克");
Set<String> s1 = Set.of("罗宾", "佩佩");
for (String s : s1) {
System.out.println(s);
}
//map
HashMap<Tools, Integer> hm1 = new HashMap<>();
hm1.put(t2, 1);
hm1.put(t3, 2);
Set<Tools> ks1 = hm1.keySet();
for (Tools t : ks1) {
Integer v = hm1.get(t);
System.out.println(t.getS1() + v);
}
//键值对遍历
Set<Map.Entry<Tools,Integer>> entrySet=hm1.entrySet();
for(Map.Entry<Tools,Integer>m:entrySet){
Tools t=m.getKey();
Integer i=m.getValue();
System.out.println(t.getS1()+i);
}
//嵌套集合
ArrayList<HashMap<Integer,String>> arrayList0=new ArrayList<HashMap<Integer,String>>();
HashMap<Integer,String> hashMap0=new HashMap<Integer,String>();
hashMap0.put(1,"robin");
arrayList0.add(hashMap0);
HashMap<Integer,String> hashMap2=new HashMap<Integer,String>();
hashMap0.put(2,"hancook");
arrayList0.add(hashMap2);
HashMap<Integer,String> hashMap3=new HashMap<Integer,String>();
hashMap0.put(1,"perona");
arrayList0.add(hashMap3);
for(HashMap<Integer,String> h:arrayList0){
Set<Integer> keySet = h.keySet();
for(Integer key:keySet){
String value=h.get(key);
System.out.println(key+","+value);
}
}
HashMap<Integer,String>hashMap=new HashMap<Integer,String>();
ArrayList<Integer>arrayList=new ArrayList<Integer>();
String[] colors={"♦","♣","♥","♠"};
String[]numbers={"3","4","5","6","7","8","9","0","J","Q","K","A","2"};
int index=0;
for(String number:numbers){
for(String color:colors){
hashMap.put(index,color+number);
arrayList.add(index);
index++;
}
}
hashMap.put(index,"小王");
arrayList.add(index);
index++;
hashMap.put(index,"大王");
arrayList.add(index);
Collections.shuffle(arrayList);
//保证编号排序用Treeset集合
TreeSet<Integer> robin=new TreeSet<Integer>();
TreeSet<Integer> hancook=new TreeSet<Integer>();
TreeSet<Integer> perona=new TreeSet<Integer>();
TreeSet<Integer> dp=new TreeSet<Integer>();
for(int i=0;i<arrayList.size();i++){
int x=arrayList.get(i);
if(i>=arrayList.size()-3){
dp.add(x);
}else if(i%3==0){
robin.add(x);
}else if(i%3==1){
hancook.add(x);
}else if(i%3==2){
perona.add(x);
}
}
//调用看牌方法
lookpoker("robin",robin,hashMap);
lookpoker("hancok",hancook,hashMap);
lookpoker("perona",perona,hashMap);
lookpoker("dp",dp,hashMap);
}//main
public static void lookpoker(String name,TreeSet<Integer> ts,HashMap<Integer,String> hm){
System.out.println(name+"的牌是");
for(Integer key:ts){
String poker=hm.get(key);
System.out.print(poker+"");
}
System.out.println();
}
}
package sunday;
//泛型类
public class Tools<T> implements Inter<T>{
String s1;
public String getS1() {
return s1;
}
public void setS1(String s1) {
this.s1 = s1;
}
public Tools(String s1) {
this.s1 = s1;
}
public Tools() {
}
@Override
public void show(T t) {
System.out.println(t);
}
//可变参数
public int sum(int b,int...a){
int sum=0;
for(int i:a){
sum+=i;
}
return sum;
}
}
package sunday;
//泛型接口
public interface Inter <T>{
void show(T t);
}