




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
程序员JAVAJAVA2013-06-0212:52157(0)收藏------------androidjavajava概java语言的java.util包中提供了一些集合类,这些集合又被Collection接可以重复,Set >List 询速度很快。但是增删稍慢。线同步 增删速度很快,查询稍慢。线同步 |---->HashSet:底层数据结构是哈希表,是线安 Collection接口的功能
booleanadd(Objectobj):添加单个元素booleanaddAll(Collectionc):添加多个元素voidclear():booleanremove(Objecto):booleanremoveAll(Collectionc)
booleancontains(Objectobj):判断元素是否存在booleancontainsAll(Collectionc):判断一个集booleanisEmpty()Iteratoriterator():返回集合上的一个迭代器intsize():元素个数booleanretainAll(Collectionc)Object[]迭代 式,会直 集合中的迭代器的方法booleanhasNext(Enumeration(枚举)换List接口功能 add(intindex,Object booleanaddAll(indexi,Collection<?extendsE> remove(intindex): set(intindex,Object get(intindex):List<E>subList(intfromIndex, inttoIndex)返回列表 fromIndex(包括)和toIndex(不包括)之间的部分视图。intindexOf(Objecto)返回此列表中第一次出现的指定元素ListIteratorlistIterator()ListList特有迭代ListIterator<E>listIterator():返回按适当顺序在列表的ConcurrentModificationException:ConcurretModificationExcetion对象的并发修改,但不允许这种修改时,抛出此异常,这个地方是指在用迭代器的方法操作集合的时候,又用集合本身的方法来操作这个集合,所以抛出这个异常 it=List集合名.listIterator();List代器特有方法: add(Ee)将指定的元素插入列表(可选操作)booleanhasPrevious() 器有多个元素,则返回nextIndex()返回对next E intpreviousIndex()返回对 set(Ee)用指定元素替换 或previous返 remove()从列表中移除由next previous返回List实现ArrayList addElement(Object ObjectelementAt(int JDK1.2get(intindexEnumerationelements()JDK1.2 booleanhasMoreElements()JDK1.2boolean nexement()JDK1.2后就改为了Object Enumerationelements()Enumeration就是枚举,枚举就是Vector特有的取出方式。此接口的功能与Iterator接口的功能是重复的,IteratorIteratorEnumeration[java][java]view1.class2. publicstaticvoidmain(String[] Vectorv=new Enumerationenv.elements();// 稍慢。线同步。[java]view[java]viewimport2.{ publicstaticvoidsop(Object{ } publicstaticvoidmain(String[] ArrayListal=new{ArrayListnewAl=newObjectobj=return}}{Iteratorital.iterator();//publicstaticArrayListsingleElement(ArrayList}al=底层使用的链表数据结构。特点:增删速度很快,查询稍慢,线LinkList addFirst(Ee)将指定元素插入此列表的开头。 addLast(Ee)将指定元素添加到此列表的结尾。EgetFirst()EgetLast(EremoveFirst()移除并返回此列表的第一个元素。EremoveLast()[java][java]view1.1.classpublicstaticvoidmain(String[]LinkedListlinknewLinkedList();//System.out.println("getFirst:"+whileStrings=(String)}}{Iteratorit=System.out.println("getLast:"+ {2.Set集Set接口里面存放的是元素是无序唯一的,可以包含null。Set集合是无序的,因此只能用迭代器获取元素。Set的子类包括HashSetTreeSetSetCollection底层数据结构是哈希表。是线安全的。不同步HashSethashcodeequals。hashcodeequals[java]view练习:往hashSet集合中存入自定对象,和相同为同一个人,hashCodeequals[java]viewimportimportclass{publicstaticvoidsop(Object{}publicstaticvoidmain(String[]{HashSeths=newIteratorit={p=} {privateStringprivateint(Stringname,int =this.age= publicinthashCode()//hashCode{ return publicbooleanequals(Objectobj)//equals{if(!(obj returnp= .equals()&&this.age== publicString{return publicint{return}可以对Set保证元素唯一性的依据:compareToreturn0.TreeSet排序的第式:让元素自身具备比较性,元素需要实现Comparable接口,覆盖compareTo方法。这种方式也成为元素的Comparator第式:实现Comparable接[java][java]view1.1.implements>privateint}=}=publicintgetAge()}this.age=publicvoidsetAge(intage)return}publicvoidsetName(Stringname)}publicStringgetName()this.age=(Stringname,intage)()privateString publicintcompareTo(p) intnum=p.age- //相同的时比较是否相 intnum2=(num==0) pareTo(): return [java][java]view1.TreeSetts=new 定义一个类实 parator接口,覆盖compare方法3. artimplements4. publicintcompare(Objecto1,Objecto2)覆 Students1= Students2= intnum= returnnewInteger(s1.getAge()).compareTo(new)));//比较因为Intege已经具有比较性了.直接比就可 return return return- return TreeSet个人总程序员JAVAJAVA2013-06-0519:21204(0)收藏------------androidjavajava泛java其实<>就是用来接收类型的,当使用集合时,将集合中要存特点[java]view[java]view1.import2.class3. publicstaticvoidmain(String[] TreeSet<Stringtsnew 型 Iterator<String>it= Strings= paratorimplements publicintcompare(Stringo1,String intnum=newInteger(o2.length()).compareTo(new return
当类中要操作的数据类型不确定的时候,早期定义静态方法不可以类上定义的泛型[java][java]view1.1.classpublicvoidshow(Tpublic<Q>voidprint(Qpublicstatic<W>voidmethod(W}{}{}{2.17.class publicstaticvoidmain(String[] Demo<String>d=new [java][java]viewinterfaceinterface{voidshow(T4.classInterImplimplements{publicvoidshow(String{System.out.println("show}17.classInterImpl<T>implementspublicvoidshow(T{System.out.println("show 24.class publicstaticvoidmain(String[] InterImpl<Integer>i=new //InterImpli=new Map集Map<K,V>:Map的是键值对形式的元素,它的一个元素,个键唯一指向一个值Map2.Map的是键值3.Map元素使用put方法,Collection使用add法MapSetMapMap接口的功能Vput(Kkey, Vvalue):将指定的值与此映射中 putAll(Map<?extendsK,?extendsV>从指定映射中将所有映射关系到此映射中(可选操作)voidclear():从此映射中移除所有映射关系(作)Vremove(Objectkey):如果存在一个键的映射关系,booleancontainsValue(Objectvalue):如果此映射将一个或多个键映射到指定值,则返回true。booleancontainsKey(Objectkey):如果此映射包含指定键的映射关系,则返回true。booleanisEmpty():如果此映射未包含键-值映射关系, Vget(Objectkey):返回指定键所映射的值;如果此映射不包含该键的映射关系,则返回size():返回此映射中的键-值映射关系数。values():返回此映射中包含的值的Collection视图。Map用类: |---->HashMap:底层是哈希表数据结构,允许使用null 键,线同步,将hashtable替代,jdk1.2.效率高。|---->TreeMap:底层是二叉树数据结构。线同步。可以用于给mapTreeSet[java][java]view1.import2.class3. publicstaticvoidmain(String[] Map<String,String>map=new 回null值}publicstaticvoidsop(Objectobj)//}{sop(coll);//打印结果:[zhangsan4zhangsan1,zhangsan2 {04=zhangsan4,01=zhangsan1,02=zhangsan2,1:Set<k>keySet流程:将map中所有的键存入到Set合。因为set迭代器。所以可以用迭代方式取出所有的键,再根据get方法。获取每一[java][java]view1.1.import3.classpublicstaticvoidmain(String[]{4. Map<String,String>map=new Set<Strings=map.keySet();//mapSet合 for(Iterator<String>it=s.iterator(); String Stringvalue=map.get(key);//有了键可以通过map集合的get 2:Set<Map.Entry<k,v>>entrySet[java][java]view1.1.import3.{Map<Integer,String>map=newpublicstaticvoidmain(String[]2.class Set<Map.Entry<Integer,StringentrySet=map.entrySet();//Map的映射关系取出。存入到Set for(Iterator<Map.Entry<Integer,String>> Map.Entry<Integer,String> Integer String EntryMapstatic因为只有有了Map集合,有了键值对,才会有键值的映射关系。关系属于Map集合中的一个内部事物。而且该事物在直接Map集底层数据结构是哈希表,可以存入null键null值,线安全,[java]view练习:使用HashMapStudent元素,并遍历,要求键是同名同为同一人,为避免加入相同元素,需要复写hashCode和equals[java]view1.2.1.描述学生3.2.定义map容器.将学生作为键.住址作为值,存入4.3.获取map5.6.importclassStudentimplementsComparable<Student>//TreeSet集合中去实现Comparable.将学生具备比较性{privateStringprivateintStudent(Stringname,intage)//将学生name和age{ publicintcompareTo(Students)覆盖 parable中的compareTo方法.来比较,先比较age在比较name.{ intnum=newInteger(this.age).compareTo(newInteger(s.age));//Integer是因为age是基本数据类型不具备比较.要转成Integer来实现compareTo方法 直接使用compareTo方法return //复写hashCodehash值和内容是否一样,因为存的是hash值所以每次建立对象的时候都不一样//所以要复写hashCode,来比 publicint{return publicbooleanequals(Objectobj)//如 {if(!(objinstanceofStudent))//如果穿进去不的不是学生类.thrownewClassCastException("类型不匹配S.equals()&& publicString{return publicint return publicString return 51.publicclass publicstaticvoidmain(String[] HashMap<Student,Stringhm=newHashMap<Student,String>();//Map合中的HashMap hm.put(newStudent("zhangsan01",21),"beijing");//往里面添加键值对. hm.put(new hm.put(new hm.put(new //第一种去取出方式:Set<k> Set<Student> for(Iterator<Student> Student String //第二种取出方式:Set<Map.Entry<k,v Set<Map.Entry<Student,String>> for(Iterator<Map.Entry<Student,String>>{Map.Entry<Student,String>StudentString}}底层数据结构是二叉树,线安全.底层是二叉树,当定义的TreeMap。练习:对学生对象的进行升序排序[java][java]viewimport paratorimplementsComparator<Student>//定义一个比较器类,将其{publicintcompare(Students1,Student{ intnums1.getName().compareTo(s2.getName());//字符串本身具有比较returnnewInteger(s1.getAge()).compareTo(new)));//Integer是因为age是基本数据类型不具备比较.要转成Integer来实现compareTo方return}class{ publicstaticvoidmain(String[] TreeMap<Student,String>tm=newTreeMap<Student,String>(new tm.put(new tm.put(new tm.put(new tm.put(new tm.put(new Set<Map.Entry<Student,String>>entrySet= Iterator<Map.Entry<Student,String>>it= Map.Entry<Student,String>me= Studentstu= Stringaddr= CollectionsCollectionsCollection别CollectionCollections是集合框架中的一个工具类。该类中的方法Collections的常用方sort:Listvoidsort(List<T>根据元素的自然顺序对指定列表按升序进行排序。列Comparable接口。此外,列表中的所有元voidsort(List<T>parator<?superT>Tmax(Collection<?extendsT>collectionComparableTmax(Collection<?extendsT> superT>comp)collection大元素。collection中的所有元素都必须可通过指定比较器相互比较binarySearchlistintbinarySearch(List<?extendsComparable<?superT>>list,T(sort(List))intbinarySearch(List<?extendsT>list,Tparator<?superT>c)sort(List,Comparator)方法)。voidfill(List<?superTlist,Tobj)使用指定元素booleanreplaceAll(List<T>list,ToldVal,T切地讲,使用newVal替换list中满足(oldVal==null?e==null:oldVal.equals(e))的每个e元素。reverse:voidreverse(List<?>list Comparable的对象collection的自然顺序。 reverseOrder(Comparator<T>List<T>synchronizedList(List<T>list)Map<K,V>synchronizedMap(Map<K,V>m)Set<T>synchronizedSet(Set<T>s)setvoidswap(List<?>list,inti,intvoidshuffle(List<?>list)使用默认随机源对指定列collections[java][java]view1.1.importpublicstaticvoidmain(String[]}publicstaticvoidList<String>list=new sop(list);//打印结果:[zzzqqaaa,abcd,sop(list);//打印结果:[zqqzzaaa,abcd,sop(list);//打印结果:[aaaabcdkkkkk,qq,zsop(list);//打印结果:[abcd,aaazzkkkkk,qq,{//sort:对List集合进行排序,swap:{2.publicclasscollection publicstaticvoid List<String>list=new Stringmax1= Stringmax2 //binarySearch:对list集合二分查找(必须是有序的 publicstaticvoid List<String>list=new intindex1= intindex2=Collections.binarySearch(list,"aaaa",new ,应该插入位置取负再减去intindex3=intindex4= ,应该插入位置取负再减去 publicstaticinthalfSearch(List<String>list,String{intmax=list.size()-min={midmax+min)>>1;//Stringstr=list.get(mid);intnum max=mid-elsemin=mid+return return- publicstaticinthalfSearch(List<String> tor<String> int max=list.size()- min= mid=(max+min)>>
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 陕西理工大学《证券与投资》2023-2024学年第二学期期末试卷
- 浙江海洋大学《工程材料科学基础》2023-2024学年第二学期期末试卷
- 运城学院《现代地貌学》2023-2024学年第二学期期末试卷
- 河北师范大学《建筑工程计量与计价学》2023-2024学年第二学期期末试卷
- 绵阳职业技术学院《天线技术实验》2023-2024学年第二学期期末试卷
- 资产评估服务委托合同范本
- 撬装加油站安全协议书范本
- 石家庄经济职业学院《美国文化》2023-2024学年第二学期期末试卷
- 湖南有色金属职业技术学院《无机化学一》2023-2024学年第二学期期末试卷
- 宁波财经学院《项目融资与投资》2023-2024学年第二学期期末试卷
- 体育与健康(水平二)《花样跳绳一级动作(18课时)》大单元教学计划
- 改革开放前后家乡的变化教学课件
- 一年级的成长历程
- 湖北省普通高中2022-2023学年高一下学期学业水平合格性考试模拟物理(二)含解析
- 2024年济南工程职业技术学院高职单招(英语/数学/语文)笔试历年参考题库含答案解析
- 癔症护理查房
- 骆驼祥子祥子成长经历
- 团队协作和领导力
- 奋力前行迎接挑战主题班会课件
- 红木家具通用技术条件解析
- 病毒性肺炎疾病演示课件
评论
0/150
提交评论