Java类集 总结.doc_第1页
Java类集 总结.doc_第2页
Java类集 总结.doc_第3页
Java类集 总结.doc_第4页
Java类集 总结.doc_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Java类集 总结 Java类集总结(xx-10-0417:27:12)标签分类java杂谈今天下午没什么事情做,把java类集了一下,方便以后自己忘记的时候再用(不清楚可看API)类集的目的是用来创建动态的对象数组操作。 单值操作Collection接口是类集中的最大单值操作的父接口,但是一般开发中不会直接使用此接口,而常使用List和Set接口。 List:可以存放重复内容Set:不能存放重复内容,所遥重复内容靠hashCode()和equals()两个方法区分Queue:队列接口SortedSet:可以对集合中的数据进行排序Collection接口常用方法1.public booleanadd(E0)2.public boolean addAll(Collectionc)3.public voidclear()4.public booleancontains(Object o)/判断一个对象是否在集合中存在5.public booleancontainsAll(Collectionc)6.public booleanequals(Object o)7.public inthashCode()/哈希码8.public booleanisEmpty()9.public Iteratoriterator()/为Iterator接口实例化10.public booleanremove(Object o)/删除指定对象11.public booleanremoveAll(Collectionc)12.public booleanretainAll(Collectionc)/保存指定对象13.public intsize()/集合大小14.public ObjecttoArray()/将一个集合变为对象数组15.publicTtoArray(Ta)List1.扩展了Collection接口,里面的内容允许重复。 2.常用子类ArrayList和Vector,增加的方法:1.public voidadd(int index,E element)/在指定的位置增加元素2.E get(index)/返回指定元素3.public int indexOf(Object o)4.public ListIteratorlistIterator()/为ListIterator接口实例化5.public Eremove(int index)6.public ListsubList(int fromIndex,int toIndex)7.public Eset(int index,E element)/替换指定位置的元素示例:学生类,包含学号,姓名,成绩。 覆盖Object类中的toString()方法,在List集合中加和学生对象,并用比较器将对象内容进行自定义排序输出。 import java.util.ArrayList;import java.util.List;import java.util.Collections;public classStudent implementsComparableprivate Stringstu_num;private Stringstu_name;private doublestu_score;public Student(String num,String name,double score)this.stu_num=num;this.stu_name=name;this.stu_score=score;public StringtoString()return学生学号:+this.stu_num+姓名:+this.stu_name+成绩:+this.stu_score;public intpareTo(Student stu)/if(this.stu_scorestu.stu_score)return1;elsereturn this.stu_num.pareTo(stu.stu_num);public staticvoid main(Stringargs)Liststuinfo=new ArrayList();/还可用SetsetList=new TreeSet();该方法必须实现比较器接口,无需sort()stuinfo.add(new Student(001,张三,52.3);stuinfo.add(new Student(002,李四,90);stuinfo.add(new Student(004,赵五,89);stuinfo.add(new Student(003,黄六,89);Student peple=new Student(005,必须,69.4);stuinfo.add(peple);stuinfo.add(new Student(005,必须,69.4);Collections.addAll(stuinfo,new Student(006,李必,99.5);stuinfo.remove(peple);/移除元素Collections.sort(stuinfo);/Collections中的sort()方法必须覆盖器方法for(Student stu:stuinfo)/foreach方法输出System.out.println(stu);Map接口,每次操作二元偶对象,以key-value的形式存储在集合中主要方法1.public viodclear()2.public booleancontainsKey(Object key)/判断指定的key是否存生3.public booleancontainsValue(Object value)4.public SetentrySet()/将Map对象变为Set集合5.public booleanequals(Object o)6.public Vget(Object key)/根据key值取得value7.public inthashCode()8.public booleanisEmpty()9.public SetkeySet()/取得所有的key10.public Vput(K key,V value)/向集合中加入元素11.public voidputAll(Mapt)12.public voidremove(Object key)/根据key值删除value13.public Collectionvalues取出所有的value Map.Entry接口简介Map.Entry是Map内部定义的一个接口,专门用来保存key-value的内容,Map.Entry的定义如下public staticinterface Map.Entry对于集合来讲,实际上是将key-value的数据保存在了Map.Entry的实例之后,再在Map集合中插入的是一个Map.Entry的实例化对象。 Map的子类HashMap:无序存放,是新的操作类(异步处理,性能高),key不允许重复Hashtable:无序存放,是旧的操作类(同步处理,性能低),kdy不允许重复TreeMap:可排序的Map集合,按集合中的key排序,key不允许重复weakHashMap:弱引用的Map集合,当集合中的某些内容不再使用时清除掉无用的数据,使用System.gc()进行回收IdentityHashMap:key可以重复注;Map中有每对数据的输出应该使用Map.Entry完成示例import java.util.HashMap;import java.util.Map;public classStudentprivate Stringstu_name;private doublestu_score;public Student(String name,double score)this.stu_name=name;this.stu_score=score;public StringtoString()return学生姓名:+this.stu_name+成绩:+this.stu_score;public staticvoid main(Stringargs)Mapmap=null;map=new HashMap();map.put(001,new Student(王五,96.1);map.put(002,new Student(张三,63.5);map.put(003,new Student(李四,86.6);map.put(004,new Student(田七,76.8);if(map.containsKey(003)System.out.println(003对应:+map.get(003);System.out.println(map.get(001);/根据key得出value集合工具集Collections可方便的操作集合主要方法1.public staticfinal ListEMPTY_LIST返回一个空的List集合2.public staitcfinal SetEMPTY_SET3.public staticfinal MapEMPTY_MAP4.public staticbooleanaddAll(Collectionc,Ta)/为集合添回内容5.public staticT max(Collectioncoll)/找到最大的内容,按比较器排序6.public staticT min(Collectioncoll)/找到最小的内容,按比较器排序7.public staticboolean replaceAll(Listlist,T oldVal,T newVal)/用新的内容替换集合的指定内容8.public staticvoid reverse(Listlist)/集合反转9.public staticint binarySearch(Listlist,T key)/查找10.public staticfinallistemptyList()/返回一个空List集合11.public staticvoid sort(Listlist)/根据Comparable接口进行排序12.public staticvoid swap(Listlist,inti,int j)/交换指定位置的元素简单示例ListallList=Collections.emptyList();allList.add(Hello);Listlist=new ArrayList();Collections.addAll(list,Hello,World,元素);Collections.reverse(all);int point=Collections.binarySearch(list,Hello);/point为检索结果的位置Collections.replaceAll(list,Hello,nihao);Collections.sort(list);Collections.swap(list,0,2);/交换0和2位置上的元素其他集合类栈Stack:Stacks=new Stack();s.push(Hello);s.pop();属性类Properties:Properties pro=new Properties();pro.setProperty(BJ,BeiJing);pro.setProperty(TJ,TianJing);pro.getProperty(BJ);File file=new File(D:+File.separator+a.txt;trypro.store(new FileOutputStream(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论