首页 java常用集合类详解(有例子,经典呐!!!集合类糊涂的来看啊!!)

java常用集合类详解(有例子,经典呐!!!集合类糊涂的来看啊!!)

举报
开通vip

java常用集合类详解(有例子,经典呐!!!集合类糊涂的来看啊!!)java常用集合类详解(有例子,经典呐!!!集合类糊涂的来看啊!!) Collections Framework 集合框架是一个统一的架构,用来表示和操作集合. 集合框架主要是由接口,抽象类和实现类构成. 接口:蓝色;实现类:红色 Collection |_____Set(HashSet) | |_____SortedSet(TreeSet) |_____List(LinkedList,ArrayList) Collection:集合层次中的根接口,JDK没有提供这个接口的实现类。 Set:不能包...

java常用集合类详解(有例子,经典呐!!!集合类糊涂的来看啊!!)
java常用集合类详解(有例子,经典呐!!!集合类糊涂的来看啊!!) Collections Framework 集合框架是一个统一的架构,用来 关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf 示和操作集合. 集合框架主要是由接口,抽象类和实现类构成. 接口:蓝色;实现类:红色 Collection |_____Set(HashSet) | |_____SortedSet(TreeSet) |_____List(LinkedList,ArrayList) Collection:集合层次中的根接口,JDK没有提供这个接口的实现类。 Set:不能包含重复的元素,子接口SortedSet是一个按照升序排列的元素的Set。 List:可以包含重复的元素,是一个有序的集合,提供了按索引访问的方式,有次序,位置不改变。 Collection接口常用的 方法 快递客服问题件处理详细方法山木方法pdf计算方法pdf华与华方法下载八字理论方法下载 : boolean add(E o) 确保此 collection 包含指定的元素(可选操作)。 boolean contains(Object o) 如果此 collection 包含指定的元素,则返回 true。 boolean remove(Object o) 从此 collection 中移除指定元素的单个实例,如果存在 的话(可选操作)。 int size() 返回此 collection 中的元素数。 Object[] toArray() 返回包含此 collection 中所有元素的数组。 Iterator iterator() 返回在此 collection 的元素上进行迭代的迭代器。 List接口特有方法: E get(int index) 返回列表中指定位置的元素。 SortedSet接口特有方法: E first() 返回此有序集合中当前第一个(最小的)元素。 E last() 返回此有序集合中最后一个(最大的)元素。 集合框架中的实现类. ArrayList: 本质:我们可以将其看作是能够自动增长容量的数组,实际是采用对象数组实现的。 自动增长容量就是当数组不够的时候,再定义更大的数组,然后将数组元素拷贝到新的数组. 例子:import java.util.*; class ArrayListTest { public static void main(String[] args) { ArrayList a1=new ArrayList(); a1.add("winsun"); a1.add("weixin"); a1.add("mybole"); for(int i=0;is2.num ? 1 : (s1.num==s2.num ? 0 : -1); if(result==0) { //String类实现了compareTo()方法. result=s1.name.compareTo(s2.name); } return result; } } public static void printElements(Collection c) { Iterator it=c.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } Student(int num,String name) { this.name=name; this.num=num; } public int HashCode() { return num*name.hashCode(); } public boolean equals(Object o) { Student s=(Student)o; return num==s.num && name.equals(s.name); } public int compareTo(Object o) { Student s=(Student)o; return num>s.num?1:(num==s.num?0:-1); } public String toString() { return num+":"+name; } } ** HashSet是基于Hash算法实现的,其性能通常优于TreeSet. 通常都应该使用HashSet,在需要排序的功能时,才使用 TreeSet. ** 迭代器: Collection提供了一个iterator()方法,可以返回一个迭代器,迭代器是指向两个元素之间的指针。凡是继承自Collection的接口或间接的实现类都有这个方法. 其中有3个方法 1.hasNext() 2.next() 3.remove() hasNext()判断是否有更多的元素,如果有返回true remove()方法remove()方法需要删除上一个返回的元素,需要先调用next()方法后在用remove(),返回的列表有时不一定真正实现remove()方法,根据需要决定是否实现. 如:ArrayList al1=new ArrayList(); Iterator it=al.iterator(); it.next(); while(it.hasNext()) { System.out.println(it.next()); } 通用方式访问集合中的元素 另外定义打印函数 public static void printElements(Collection c) { Iterator it=c.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } Map(HashMap) |_____SortedMap(TreeMap) Map:存储的是key-value对,不能包含重复的key,可以有重复的value。子接 口SortedMap是一个按升序排列key的Map。 在系统区windows目录下有win.ini 有键和对应的值 asf=MPEGVideo 注册表存储的也是这种类型. 就是用Map接口所提供的方法来存储. SortedMap是一个按照升序排列key的Map. Map接口实现类: HashMap: 对key进行散列. keySet()..values()..entrySet().. HashMap是实现了Map接口的Hash表. 实现了所有hashmap操作,允许空值和空键. . HashSet底层就是hashmap的实现 map接口没有add()方法. 方法. 要放置元素通过put() Object put(Object key,Object value) 获取元素的时候 Object get(Object key) 通过键获取值 Hash表,通过键计算出相对应的存储位置的值,并输出. 常用的方法 Set keySet() 返回一个键的视图类型是Set. Collection values() 返回一个值的视图类型是Collection. Set entrySet() 返回一个键值对视图类型是Set. 返回的Set集合当中每一个对象都是一个Map.Entry对象. Map.Entry是一个静态的接口. 接口中提供了常用方法 Object getKey() Object getValue() ***************************** import java.util.*; public class HashMapTest { public static void printElements(Collection c) { Iterator it=c.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } public static void main(String []args) { HashMap hm=new HashMap(); hm.put("1", "zhang3"); hm.put("2", "li4"); hm.put("3", "wang5"); System.out.println(hm.get("1")); System.out.println(hm.get("2")); System.out.println(hm.get("3")); Set keys=hm.keySet(); System.out.println("-----------keys---------"); printElements(keys); Collection values=hm.values(); System.out.println("-----------values---------"); printElements(values); Set entrySets =hm.entrySet(); System.out.println("------------entrySets-----------"); printElements(entrySets); Iterator it=entrySets.iterator(); while(it.hasNext()) { Map.Entry me=(Map.Entry)it.next(); System.out.println(me.getKey()+" = "+me.getValue()); } } } ** TreeMap是实现了sorted Map接口的类 TreeMap按照key进行排序. 类似HashMap用法 ********************** HashMap和TreeMap比较 ,HashMap的速度通常都比TreeMap快, 和Set类似 只有在需要排序的功能的时候,才使用TreeMap. 另外有用的类 Collections类不同于Collection类 Collections.sort()主要是对列表排序. 1.自然排寻natural ordering 2.实现比较器Comparator接口 取最大元素Collections.max(). 取最小元素Collections.min(). 在已经排序的List中搜索指定的元素. Collections.binarySearch(). static void sort(List list)方法是按升序对指定列表排序. 自然排序法. 列表中元素必须都实现了comparable接口.与Arrays.sort()是一样的. comparable接口在java.lang包当中. 实现comparable接口就要实现compareTo()方法. compareTo()大于返回正数,等于返回0,小于返回负数. class Student implements Comparable { int num; String name; Student(int num,String name) { this.num=num; this.name=name; } public int compareTo(Object o) { Student s=(Student)o; return num>s.num?1:(num==s.num?0:-1); } public String toString() { return num+":"+name; } } public static void printElements(Collection c) { Iterator it=c.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } public static void main(String []args) { Student s1=new Student(2,"zhangsan"); Student s2=new Student(1,"lisi"); Student s3=new Student(3,"wangwu"); ArrayList a1=new ArryList(); a1.add(s1); a2.add(s2); a3.add(s3); Collections.sort(a1); printElements(a1); } 排序的时候可以传递一个比较器. Comparator接口. java.util包当中. 有两个方法int compare(Object o1,Object o2) 两个对象进行比较当对象1大于对象2的时候返回1, 当对象1等于对象2的时候返回一个0, 当对象1小于对象2的时候返回一个负数. 和boolean equals(Object obj)方法. 实现一个比较器. 比较器总是和一个特定的类相关. 为某一个类指定一个比较器. 利用内部类实现比较器接口. **内部类 声明为静态方法就不需要产生外部类对象. class Student implements Comparable { int num; String name; Student(int num,String name) { this.num=num; this.name=name; } public int compareTo(Object o) { Student s=(Student)o; return num>s.num?1:(num==s.num?0:-1); } public String toString() { return num+":"+name; } static class StudentComparator implements Comparator { public int compare(Object o1,Object o2) { Student s1=(Student)o1; Student s2=(Student)o2; int result=s1.num>s2.num ? 1 : (s1.num==s2.num ? 0 : -1); if(result==0) { //String类实现了compareTo()方法. result=s1.name.compareTo(s2.name); } return result; } } } public static void printElements(Collection c) { Iterator it=c.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } public static void main(String []args) { Student s1=new Student(2,"zhangsan"); Student s2=new Student(1,"lisi"); Student s3=new Student(3,"wangwu"); ArrayList a1=new ArryList(); a1.add(s1); a2.add(s2); a3.add(s3); Collections.sort(a1,Student.StudentComparator()); printElements(a1); } Collections类中提供了一种反序排列方式. 本身返回就是一个比较器对象 static Comparator reverseOrder() Arrays.sort()也可以指定一个比较器对象 static void sort(Object[] a,Comparator c) Collections类主要对列表操作. Arrays类主要对数组操作. Vector集合类 Vector:用ArrayList代替Vector. Vector内所有方法都是同步的,存取元素效率低. 在多线程中可以使用 在Collection类中提供了 static list synchronizedList(List list) 可以返回一个同步列表 可以将列表实参传递进去会返回一个安全的线程同步列表 还有 static Collection synchronizedCollection(Collection c) static Set synchronizedSet(Set s) Hashtable:用HashMap代替Hashtable 需要同步Map的时候 可以用Collection类的方法 static Map synchronizedMap(Map m) 但是还是Hashtable获取的同步Map快一些. Stack集合类: Stack:用LinkedList代替Stack. Stack是从java.util.Vector继承而来. 还从Vector继承了alementAt()方法. 同时Stack还继承了不需要的特性. 所以要使用栈的时候要用LinkedList来自己实现. Properties属性类.是从Hashtable继承而来也是存储键值对的. 表示一个持久的属性集合. 可以存储win.ini的键值对. 在 java.lang.System类当中 有 static Properties getProperties() 检测当前系统的属性 可以使用 void list(PrintStream out)打印系统属性 还可以读取一个配置文件: 先建立一个配置文件 winsun.ini 写入 company=winsun author=sunxin corpyright=2003-2004 先实例化一个Property对象 在用 void load(InputStream inStream)方法 可以从一个输入流中加载一个属性列表. 输入流可以用java.io包中的一个类构造. 可用 Enumeration propertyNames() 返回Enumeration对象.返回所有键的枚举. Enumeration是一个接口 提供了两个方法 boolean hasMoreElements() Object nextElement() 需要返回值的时候 String getProperty(String key) 返回键对应的值 import java.util.*; import java.io.FileInputStream; import java.io.*; public class ProTest { public static void main(String []args) { /* Properties pps=System.getProperties(); pps.list(System.out); */ Properties pps=new Properties(); try { pps.load(new FileInputStream("d:/workspace/ProTest/bin/winsun.ini")); Enumeration ENUM=pps.propertyNames(); while(ENUM.hasMoreElements()) { String str=(String)ENUM.nextElement(); String strValue=pps.getProperty(str); System.out.println(str+" = "+strValue); } } catch(FileNotFoundException e) { e.printStackTrace(); } catch(IOException ex) { ex.printStackTrace(); } } } static String getProperty(String key) static String getProperty(String key,String def)
本文档为【java常用集合类详解(有例子,经典呐!!!集合类糊涂的来看啊!!)】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_633423
暂无简介~
格式:doc
大小:51KB
软件:Word
页数:24
分类:互联网
上传时间:2018-01-08
浏览量:56