集合类的作用: 集合类可以解决:可以动态完成对象集合的操作,并可以不受对象的数量的 限制。java的类集框架对一些实现好的数据结构进行了包装,使用方便。 集合类可以分为保存单值的Collection和能保存双值的Map。并可以通过Properties 类产生属性文件,这种文件可以保存运行时需要的属性,要用时可以加载进来。 一.单值的Collection集合 为了方便从使用上来区分,我把所学到的保存单值的集合类的各接口分为操作组 和 输出组。 1、操作组: 在进行集合类的 增加元素、删除元素、查找元素等操作时,一般用以下接口: Collection :是存放一个单值对象的最大父接口。一般不直接使用此接口进行操作。 在实际开发中会使用以下子接口进行增删查改的操作: *List:可以存放重复的内容和多个null值,并扩展了许多Collection接口中没有 的方法。 *ArrayList类:实现了List接口,可以使用ArrayList类来实例化List接口。 Vector类:也可以使用此类来实例化List接口。操作与ArrayList类似但是 Vector类有其独立的方法来对集合进行操作。 ArrayList与Vector类的主要区别 ArrayList Vector 1推出时间: JDK1.2后推出 JDK1.0时推出 2性能: 异步处理,性能较高 采用同步处理,性能较低 3线程安全: 非线程安全的 线程安全的 4输出: Iterator、foreach Iterator、foreach、Enumerantion *Set:
不能存放重复的内容,可以有一个null值,所有的内容可以靠hashCode()和 equals()两个方法来区分。 就是说:如果自己设计的类,产生了很多实例对象,需要把这些实例对象存放在 Set集合中,那么就需要自己覆写(Override)hashCode()和equals()方法,由编 译器通过这两个方法来确定此类产生的对象是否相同。如果是同一个对象,则不 能放入实现了Set接口的集合中。 HashSet类 :实现了Set接口,它不能存放相同的元素,而且采用了散列存放的存 储方式。所谓散列存放就是存放的顺序是不固定的。 TreeSet类:实现了Set接口,并可以对元素进行有序排列。但如果要将自己编写 的类的对象放入到Set接口中,则此类必须实现Comparable接口,要实现Comparable 接口,就必须覆写(Override)public int compareTo()方法。这样可以使用这个 方法用于排列对象的顺序。 二、输出组: Iterator接口:用于为以上类型的集合进行输出和删除操作,采用迭代的方式输出。 迭代就是先判断元素是否存在,然后再把元素取出。如果要取得Iterator接口的实例, 则必须用Collection接口的子类实例调用iterator()方法来获得。 在输出时:先用-->public boolean hasNext()判断下一个元素是否存在,然后使用 public E next() 取出元素,或者使用public void remove()删除 当前元素。采用循环,就可以把集合中多个或者所有元素历遍。 注意,hasNext()方法会让指示器指向下一个对象,如果使用了next() 方法取出了所有的元素,虽然集合中的元素没有减少,但是指示器会指向 集合的末端,此时再调用remove方法来删除元素,则不会成功。 Enumeration接口:作用和Iterator类似,只针对Vector来输出,而且没有删除元素的 方法。
二、双值的集合Map
/*以上是只能保存单值的集合类,Map接口可以一次保存以两个对象为一组的双值。其中 第一个值为key,第二值为value,可以通过get(key)的方式,得到value的值。要得到 Map集合中的元素,有三种方式: a、通过getKeys()方法获得所有的key值,也就是得到一个key值的集合,此方法返回一个 Set的实例对象。 b、通过getValues()方法获得所有的value值,此方法返回一个Colletion的实例对象。 c、Map集合中有个内部接口:Map.Entry(),可以同过getEntry()方法得到一个Set的实 例对象,这时Set中的每个元素就是Map.Entry类型的,Map.Entry类由一对key,value组成。 此时可以使用Set类的.iterator()方法生成Iterator对象,通过Iterator对象迭代输出 每个Map.Entry对象,最后使用Map.Entry对象的getkey()和getValue()方法得到单个的 key值/value值。(不使用Iterator的话,可以使用foreach)。
Map本身是接口,要使用的话,必须用其子类为它实例化。以下是目前学到的Map的实现类: HashMap:Map的实现类,对一组key、vaule值散列保存,一对key、value可以用一个Map. Entry对象来存放。 HashTable:也实现了Map接口,操作和HashMap类似,其区别跟List与Vector的区别类似。
三、Properties类与属性文件
Perperties类是HashTable的子类,可以保存都为String类型的key-value属性。并可以 使用OutputStream和InputStream类来输出输入属性。
以下是学习集合类所做的一些简单练习:
*用于作为集合中的元素的Person类,覆写了Object的equals()方法和hasCode()方法, 以及toString()方法。
package cn.hxq.collection; public class Person { private String name ; private int age; public Person(String name,int age){ this.name = name; this.age = age; } public String toString(){ return "姓名:"+this.name+"、年龄:"+this.age; } @Override public int hashCode() { return this.name.hashCode()+this.age; } @Override public boolean equals(Object obj) { if(this == obj){ return true; } if(obj instanceof Person){ Person pers = (Person)obj; if(this.name.equals(pers.name) && this.age == pers.age){ return true; }else{ return false; } }else{ return false; } } }
*ListTest,使用Person类作为元素测试了List的操作和使用Iterator迭代器进行输出 和删除操作。
package cn.hxq.collection; import java.util.*; public class ListTest { public static void main(String[] args) { // TODO Auto-generated method stub Person per1 = new Person("ZS",31); Person per2 = new Person("ms",18); Person per3 = new Person("dz",29); Person per4 = new Person("fs",47); Person per5 = new Person("qs",25); Person per6 = new Person("dk",79); List<Person> arrayList = new ArrayList<Person>(); //集合的增操作。 arrayList.add(per1); arrayList.add(per2); arrayList.add(per3); arrayList.add(per4); arrayList.add(per5); arrayList.add(per6); //产生两个Iterator对象,一个用于迭代输出,一个用于删除。 Iterator<Person> itr1 = arrayList.iterator(); Iterator<Person> itr2 = arrayList.iterator(); //集合的输出操作,需要一个专门的Iterator类来输出。 System.out.println("迭代输出:"); while(itr1.hasNext()){ System.out.println(itr1.next()+"、"); System.out.println(); } //集合的删除操作 while(itr2.hasNext()){ Person per = itr2.next(); if(new Person("ms",18).equals(per)){ itr2.remove(); System.out.println("指定内容被删除了"); }else{ System.out.println(per+"、"); } } System.out.println("删除之后的集合:"+arrayList); } }
*HashSetTest:只观察了散列存放。
package cn.hxq.collection; import java.util.*; public class HashSetTest { public static void main(String[] args) { // TODO Auto-generated method stub Set<Person> allSet = new HashSet<Person>(); allSet.add(new Person("张三",31)); allSet.add(new Person("李四",18)); allSet.add(new Person("王五",29)); allSet.add(new Person("王五",29)); allSet.add(new Person("王五",29)); allSet.add(new Person("小阿",47)); allSet.add(new Person("小明",25)); allSet.add(new Person("小明",25)); allSet.add(new Person("小明",25)); allSet.add(new Person("小凯",79)); allSet.add(new Person("小凯",79)); System.out.println(allSet); } }
*HashMapTest:对keys,values和Map.Entry输出。 package cn.hxq.collection; import java.util.*; public class HashMapTest { public static void main(String[] args){ Map<Integer,Person> map= new HashMap<Integer,Person>(); //加入key,value元素 map.put(1, new Person("zs",35)); map.put(2, new Person("ms",24)); map.put(3, new Person("dz",31)); Person per = map.get(1); //查找输出 System.out.println(per); System.out.println("取得key1:"+map.containsKey(1)); System.out.println("查找对象per1:"+map.containsValue(new Person("zs", 35))); System.out.println("map:"+map); //取得key集合、values集合并输出 Set<Integer> set = map.keySet(); Collection<Person> con = map.values(); System.out.println("key:" + set + ",values:" + con); //取得Map.Entry集合并输出 //Set<Map.Entry<Integer, Persons>> entrySet = map.entrySet(); //Iterator<Map.Entry<Integer, Persons>> entryIter = entrySet.iterator(); Iterator<Map.Entry<Integer, Person>> entryIter = map.entrySet().iterator(); int count = 1; while(entryIter.hasNext()){ Map.Entry<Integer, Person> entry = entryIter.next(); System.out.println("The"+(count++)+"Entry:"+entry); } } }
*PropertyTest:使用Properties类增加元素,并输出到一个文件中 再从文件中加载到属性。
package cn.hxq.collection; import java.util.*; import java.io.*; public class PropertyTest { public static void main(String[] args) { // TODO Auto-generated method stub Properties pro = new Properties(); pro.setProperty("bj","beijing"); pro.setProperty("nj","nanjing"); pro.setProperty("tj", "tianjing"); System.out.println("1.bj属性存在:"+pro.getProperty("bj")); System.out.println("2.sc属性不存在:"+pro.getProperty("sc")); System.out.println("3.sc属性不存在,并设置默认值:"+pro.getProperty("sc", "NotFound")); File f = new File("d:"+File.separator+"area.properties"); try { pro.store(new FileOutputStream(f), "Area Info"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Properties pro2 = new Properties(); try{ pro2.load(new FileInputStream(f)); }catch(Exception e){ e.printStackTrace(); } System.out.println(pro2); } }