java集合框架(习题)集合框架KeyPoint*Collection接口、Set接口、List接口基本操作*List接口及其实现类*Set接口及其实现类*迭代遍历*Hash算法与hashCode方法*Comparable接口*Map接口及其实现类*遍历Map*泛型练习1.填空Collection接口的特点是元素是对象;List接口的特点是元素有(有|无)顺序,可以(可以|不可以)重复;Set接口的特点是元素无(有|无)顺序,不可以(可以|不可以)重复;Map接口的特点是元素是键值对,其中值可以重复,键不可以重复。2.(List)有如下代码importjava.util.*;publicclassTestList{publicstaticvoidmain(Stringargs[]){Listlist=newArrayList();list.add(“Hello”);list.add(“World”);list.add(1,“Learn”);list.add(1,“Java”);printList(list);}publicstaticvoidprintList(Listlist){for(Objectobj:list){Stringstr=(String)obj;System.out.println(obj);}}}要求:1)把//1处的代码补充完整,要求输出list中所有元素的内容2)写出程序执行的结果HellojavaLearnWorld3)如果要把实现类由ArrayList换为LinkedList,应该改哪里?ArrayList和LinkedList使用上有什么区别?实现上有什么区别?把实例化的语句改为newLinkedList();ArrayList数组实现查询快增删慢LinkedList链表实现查询慢增删快4)如果要把实现类由ArrayList换为Vector,应该改哪里?ArrayList和Vector使用上有什么区别?实现上有什么区别?ArrayList是线程不同步的,轻量级的,线程不安全,速度快Vector是线程同步的,多线程访问比较安全,速度慢3.(List)写出下面程序的运行结果importjava.util.*;publicclassTestList{publicstaticvoidmain(Stringargs[]){Listlist=newArrayList();list.add(“Hello”);list.add(“World”);list.add(“Hello”);list.add(“Learn”);list.remove(“Hello”);list.remove(0);for(inti=0;ilist.size();i++){System.out.println(list.get(i));}}}HelloLearn4.(Set,List)importjava.util.*;publicclassTestListSet{publicstaticvoidmain(Stringargs[]){Listlist=newArrayList();list.add(“Hello”);list.add(“Learn”);list.add(“Hello”);list.add(“Welcome”);Setset=newHashSet();set.addAll(list);System.out.println(set.size());}}选择正确答案A.编译不通过B.编译通过,运行时异常C.编译运行都正常,//输出HashSet中不能放重复值D.编译运行都正常,输出45.(List)已知有一个Worker类如下:publicclassWorker{privateintage;privateStringname;privatedoublesalary;publicWorker(){}publicWorker(Stringname,intage,doublesalary){this.name=name;this.age=age;this.salary=salary;}publicintgetAge(){returnage;}publicvoidsetAge(intage){this.age=age;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicdoublegetSalary(){returnsalary;}publicvoidsetSalary(doublesalary){this.salary=salary;}publicvoidwork(){System.out.println(name+“work”);}}完成下面的要求1)创建一个List,在List中增加三个工人,基本信息如下:姓名年龄工资zhang3183000li4253500wang52232002)在li4之前插入一个工人,信息为:姓名:zhao6,年龄:24,工资33003)删除wang5的信息4)利用for循环遍历,打印List中所有工人的信息5)利用迭代遍历,对List中所有的工人调用work方法。6)为Worker类添加equals方法6.(Set,Hash算法)为上一题的Worker类,在添加完equals方法的基础上,添加一个hashCode方法。publicinthashCode(){//1}有几种写法:1)return0;2)intresult=0;if(name!=null)result=name.hashCode();returnresult+age;3)returnsuper.hashCode();现在要把Worker类放入HashSet中,并希望在HashSet中没有重复元素,则下面说法正确的是:A.三种写法都正确B.1),2)写法正确,2)效率更高C.2)写法正确,1),3)写法都不正确7.(Set,Hash算法,方法覆盖)代码改错importjava.util.*;classWorker{Stringname;intage;doublesalary;publicWorker(){}publicWorker(Stringname,intage,doublesalary){this.name=name;this.age=age;this.salary=salary;}publicinthashCode(){//hashCode必须声明为公共的。return(int)(name.hashCode()+age+salary);//返回值类型为整形(}//equals方法实现有错publicbooleanequals(Workerw){if(w.name==name&&w.salary==salary&&w.age==age){returntrue;}elsereturnfalse;}}publicclassTestWorker{publicstaticvoidmain(Stringargs[]){Setset=newHashSet();set.add(newWorker(“tom”,18,2000));set.add(newWorker(“tom”,18,2000));set.add(0,newWorker(“jerry”,18,2000));//HashSet中没有定义带下标的add方法。System.out.println(set.size());}}8.(Set,Hash算法)在前面的Worker类基础上,为Worker类增加相应的方法,使得Worker放入HashSet中时,Set中没有重复元素。并编写相应的测试代码。classWorker{Stringname;intage;doublesalary;publicWorker(){}publicWorker(Stringname,intage,doublesalary){this.name=name;this.age=age;this.salary=salary;}publicinthashCode(){return(int)(name.hashCode()+age+salary);}publicbooleanequals(Workerw){if(w.name==name&&w.salary==salary&&w.age==age){returntrue;}elsereturnfalse;}}9.(Set,Comparable接口)在前面的Worker类基础上,为Worker类添加相应的代码,使得Worker对象能正确放入TreeSet中。并编写相应的测试代码。注:比较时,先比较工人年龄大小,年龄小的排在前面。如果两个工人年龄相同,则再比较其收入,收入少的排前面。如果年龄和收入都相同,则根据字典顺序比较工人姓名。例如:有三个工人,基本信息如下:姓名年龄工资zhang3181500li4181500wang5181600zhao6172000放入TreeSet排序后结果为:zhao6li4zhang3wang5importjava.util.HashSet;publicclassTest1{publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubHashSetWorkerhs=newHashSetWorker();Workerw1=newWorker(zhang3,18,1500);Workerw2=newWorker(lis4,18,1500);Workerw3=newWorker(wang5,18,1600);Workerw4=newWorker(zhao6,17,2000);hs.add(w1);hs.add(w2);hs.add(w3);hs.add(w4);System.out.println(hs.size());System.out.println(hs);}}classWorkerimplementsComparableWorker{Stringname;intage;doublesalary;publicWorker(){}publicWorker(Stringname,intage,doublesalary){this.name=name;this.age=age;this.salary=salary;}@OverridepublicintcompareTo(Workero){//TODOAuto-generatedmethodstubif(this.age!=o.age){returnthis.age-o.age;}elseif(this.salary!=o.salary){//Integerinteger1=newInteger(this.salary)returnnewDouble(this.salary).compareTo(newDouble(o.salary));}elseif(this.name.equals(o.name)){returnthis.name.compareTo(o.name);}return0;}@OverridepublicinthashCode(){finalintprime=31;intresult=1;result=prime*result+age;result=prime*result+((name==null)?0:name.hashCode());longtemp;temp=Double.doubleToLongBits(salary);result=prime*result+(int)(temp^(temp32));returnresult;}@Overridepublicbooleanequals(Objectobj){if(this==obj)returntrue;if(