zoukankan      html  css  js  c++  java
  • Set

    import java.util.*;
    
    class SetType{
        int i;
        public SetType(int n){ this.i = n;}
        public boolean equals(Object o){
            return o instanceof SetType && (this.i == ((SetType)o).i);
        }
        @Override
        public String toString() {
            return Integer.toString(i);
        }
        
    }
    class HashType extends SetType{
        public HashType(int n) {super(n);}
        public int hashCode(){return i;}
    }
    class TreeType extends SetType implements Comparable<TreeType>{
        public TreeType(int n){super(n);}
        
        public int compareTo(TreeType o){
            return (o.i<i?-1: (o.i ==i ?0:1));
    }
    }
    
    public class TypesForSets {
        static <T> Set<T>  fill(Set<T> set,Class<T> type)
        {
            try{
                for(int i=0;i<10;i++){
                    //下面代码什么鬼?
                    set.add(type.getConstructor(int.class).newInstance(i));
                }
            }catch(Exception e){
                throw new RuntimeException(e);
            }
            
            return set;
            
        }
        static <T> void test(Set<T> set,Class<T> type ){
            fill(set,type);
            fill(set,type); //try to add duplicates
            fill(set,type);
            System.out.println(set);
        }
        
        public static void main(String[] args) {
            //test(new HashSet<HashType>(),HashType.class);
            //test(new LinkedHashSet<HashType>(),HashType.class);
            //test(new TreeSet<TreeType>(),TreeType.class);
            
            //Things that don't work:
            //test(new HashSet<SetType>(),HashType.class);
            
            Set<String> set = new LinkedHashSet<String>();
            set.add("Bob");
            set.add("Allen");
            set.add("Carb");
            
            System.out.println(set);
            
        }
    
    }
  • 相关阅读:
    nodejs--模块化
    node-package.json 文件package-lock.json
    NPM
    REPL介绍
    nvm npm nrm 区别
    docker docker-compose安装
    微信小程序对接阿里云视频点播,备忘
    python requests包爬网页数据demo
    php redis扩展地址
    php7.2.4安装rabbitmq扩展的过程中错误处理
  • 原文地址:https://www.cnblogs.com/vector11248/p/7765309.html
Copyright © 2011-2022 走看看