JAVA 容器类

来源:互联网 发布:重庆市网络办事大厅 编辑:程序博客网 时间:2024/06/05 09:36

容器 就是存放数据的一个集合

(Java提供了一个接口专门去约束我们容器的实现类)


Set: does not allow duplicate objects toenter the collection of elements

SortedSet: similar to set except that theelements in the set are stored in ascending order

List: is ordered, maintain an order asobjects are added and removed from the collection,can also contain duplicateentries of objects

Map: stores objects that are identifiedby unique keys, and may not store duplicate keys

SortedMap: similar to Map, except the objectsare stored in ascending order according to their keys

即:

•   Collection 接口:定义了存取一组对象的方法,其子接口Set和List分别定义了存储方式。

–  Set 中的数据对象没有顺序且不可以重复。

–  List 中的数据对象有顺序且可以重复。

–  Map 接口定义了存储“键(key)- 值(value)映射对”的方法。

Collection方法

•    Collection 表示一组对象,它是集中,收集的意思,就是把一些数据收集起来。

•    Collection函数库是在java.util 包下的一些接口和类,类是用来产生对象存放数据用的,而接口是访问数据的方式。

•    Collection函数库与数组的两点不同:

1.数组的容量是有限制的,而Collection库没有这样的限制,它容量可以自动的调节 。

2.Collection函数库只能用来存放对象,而数组没有这样的限制。

•    Collection接口是Collection层次结构 中的根接口,它定义了一些最基本的访问方法,让我们能用统一的方式通过它或它的子接口来访问数据。

•    区别:Collection代表一组对象, Collection函数库就是java中的集合框架,Collection接口,是这个集合框架中的根接口。

•    存放在Collection 库中的数据,被称为元素(element) 。

Demo

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;"><span style="font-size:18px;">public class Person {  
  2.   private intid;       
  3.   private Stringname;  
  4.   public Person(int id, String name) {  
  5.          this. id= id;    this. name = name;  
  6.   }  
  7.   public intgetId() {    return id;   }  
  8.   public StringgetName() {   return name;  }  
  9.   public voidsetId (int id) {  
  10.          this. id= id;  
  11.   }  
  12.   public voidsetName (String name) {  
  13.          this.name = name;  
  14.   }  
  15.   public StringtoString() {  
  16.          return “id: ” + id + “|name: ” + name;  
  17.   }  
  18. }  
  19. ========================================================================  
  20. import java.util.*;  
  21. public class CollectionTest1 {  
  22.   public staticvoid main (String[] args) {  
  23.          Collectionc = new HashSet();  
  24.          c. add(new Person(1, “c++"));  
  25.          c. add(new Person(2, “java"));  
  26.          System.out.println(c. size() + ": " + c);  
  27.          System.out.println("contains: " + c. contains (new                                    Person(2"java")));  
  28.          System.out.println(c. remove (new Person(2" java")));  
  29.          System.out.println(c. size() + ": " + c);   
  30.   }  
  31. }</span></span>  

输出结果:

Iterator接口

•    所有实现了Collection接口的容器类都有一个iterator方法用以返回一个实现了Iterator接口的对象。

•    Iterator对象称作迭代器,用以方便的实现对容器内元素的遍历操作。

•    Iterator接口定义了如下方法:


Demo

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;"><span style="font-size:18px;">import java.util.*;  
  2. public class IteratorTest1 {  
  3.   public staticvoid main(String[] args) {  
  4.          Collectionc = new ArrayList();  
  5.          c.add("good");   c.add("morning");  
  6.          c.add("key");    c.add("happy");  
  7.          for(Iterator it = c.iterator(); it.hasNext(); ) {  
  8.                 Stringtem = (String) it.next();  
  9.                 if(tem.trim().length() <= 3) {  
  10.                        it.remove();  
  11.                 }  
  12.          }  
  13.          System.out.println(c);  
  14.   }  
  15. }  
  16. </span></span>  


Set方法

•    Set 接口是Collection接口的子接口,Set接口没有提供额外的方法,Set接口的特性是容器类中的元素是没有顺序的,而且不可以重复。

•    Set 容器可以与数学中“集合”的概念相对应。

•    J2SDK API中所提供的 Set 容器类有 HashSet,TreeSet 等。

Demo

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;">import java.util.*;  
  2. public class SetTest {  
  3.   public staticvoid main (String[] args) {  
  4.          Set s =new HashSet();  
  5.          s.add("hello");  
  6.          s.add("world");  
  7.          s.add(new Integer(4));  
  8.          s.add(new Double(1.2));  
  9.          s.add("hello"); // 相同的元素不会被加入  
  10.          System.out.println(s);  
  11.   }  
  12. }  
  13. </span>  



List接口

•    List接口是Collection的子接口,实现List接口的容器类中的元素是有顺序的,而且可以重复。

•    List 容器中的元素都对应一个整数型的序号记载其在容器中的位置,可以根据序号存取容器中的元素。

•    J2SDK 所提供的 List 容器类有 ArrayList,LinkedList 等。

Demo

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;"><span style="font-size:18px;">import java.util.*;  
  2. public class ListTest {  
  3.   public staticvoid main(String[] argc) {  
  4.          List l1= new ArrayList();  
  5.          for (inti = 0; i <= 5; i++)  
  6.                 l1.add("a"+ i);  
  7.          System.out.println(l1);  
  8.          list.add(3,"a100");  
  9.          System.out.println(l1);  
  10.          list.set(6,"a200");  
  11.          System.out.println(list);  
  12.          System.out.print((String)list.get(2) + " ");  
  13.          System.out.println(list.indexOf("a3"));  
  14.          list.remove(1);  
  15.          System.out.println(list);  
  16.   }  
  17. }</span></span>  


常用算法

类 java.util.Collections 提供了一些静态方法实现了基于List容器的一些常用算法

Demo

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;"><span style="font-size:18px;">import java.util.*;  
  2. public class CollectionsTest {  
  3.      publicstatic void main(String[] argc) {  
  4.   List aList =new ArrayList();  
  5.   for (int i = 0;i < 5; i++)  
  6.          aList.add("a"+ i);  
  7.   System.out.println(aList);  
  8.   Collections.shuffle(aList);// 随机排列  
  9.   System.out.println(aList);  
  10.   Collections.reverse(aList);// 逆续  
  11.   System.out.println(aList);  
  12.   Collections.sort(aList);// 排序  
  13.   System.out.println(aList);  
  14.   System.out.println(Collections.binarySearch(aList,"a2"));  
  15.   Collections.fill(aList,"hello");  
  16.   System.out.println(aList);  
  17.    }  
  18. }  
  19. </span></span>  

Comparable接口  

问题:上面的算法根据什么确定集合中对象的“大小”顺序?

•     所有可以“排序”的类都实现了java.lang.Comparable 接口,Comparable接口中只有一个方法

      public int compareTo(Objectobj);

      该方法:

•   实现了Comparable 接口的类通过实现 comparaTo 方法从而确定该类对象的排序方式。

Demo

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;"><span style="font-size:18px;">public class Student implements Comparable {  
  2.                private Stringname;  
  3.                private Integerscore;  
  4.                publicStudent(String name, int score) {  
  5.                      this.name =name;  
  6.                      this.score =new Integer(score);  
  7.                }  
  8.                public intcompareTo(Object o) {  
  9.                      Student n =(Student) o;  
  10.                      int a =score.compareTo(n.score);  
  11.                      return (a !=0 ? a :                                                                  name.compareTo(n.name));  
  12.                }  
  13.                public StringtoString() {  
  14.                      return"name: " + name + " score: " +                                   score.toString();  
  15.                }  
  16. }  
  17. import java.util.*;  
  18. public class StudentTest {  
  19.                public static voidmain(String[] args) {  
  20.                      List l1 = newLinkedList();  
  21.                      l1.add(newStudent(“ttt", 66));  
  22.                      l1.add(newStudent(“bbb", 77));  
  23.                      l1.add(newStudent(“ccc", 99));  
  24.                      l1.add(newStudent(“fff", 88));  
  25.                      l1.add(newStudent(“aaa", 66));  
  26.                      System.out.println(l1);  
  27.                      Collections.sort(l1);  
  28.                      System.out.println(l1);  
  29.                }  
  30. }</span></span>  


Map接口

•    实现Map接口的类用来存储键(key)-值(value) 对。

•    Map 接口的实现类有HashMap和TreeMap等。

•    Map类中存储的键-值对通过键来标识,所以键值不能重复。

Demo

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;"><span style="font-size:18px;">import java.util.*;  
  2. public class MapTest {  
  3.   public staticvoid main(String args[]) {  
  4.          Map m1 =new HashMap();  
  5.          Map m2 =new TreeMap();  
  6.          m1.put("one",new Integer(1));  
  7.          m1.put("two",new Integer(2));  
  8.          m1.put("three",new Integer(3));  
  9.          m2.put("A",new Integer(1));  
  10.          m2.put("B",new Integer(2));  
  11.          System.out.println(m1.size());  
  12.          System.out.println(m1.containsKey("one"));  
  13.          System.out.println(m2.containsValue(newInteger(1)));  
  14.          if(m1.containsKey("two")) {  
  15.                 inti = ((Integer) m1.get("two")).intValue();  
  16.                 System.out.println(i);  
  17.          }  
  18.          Map m3 =new HashMap(m1);  
  19.          m3.putAll(m2);  
  20.          System.out.println(m3.size());  
  21.   }  
  22. }</span></span>  

泛型

泛型的作用就是为了约束你传入对象的类型;

通俗来讲:传进去什么,拿出来什么。

•    起因:

–  JDK1.4以前类型不明确:

•   装入集合的类型都被当作Object对待,从而失去自己的实际类型。

•   从集合中取出时往往需要转型,效率低,容易产生错误。

•    解决办法:

–  在定义集合的时候同时定义集合中对象的类型

•   可以在定义Collection的时候指定

•   也可以在循环时用Iterator指定

•    好处:

–  增强程序的可读性和稳定性

Demo

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;"><span style="font-size:18px;">import java.util.*;  
  2. public class MapTest1 {  
  3.     public staticvoid main(String args[]) {  
  4.            Map<String,Integer> m = new HashMap<String, Integer>();  
  5.            for(int i = 0; i < 5; i++) {  
  6.                   m.put(String.valueOf(i),1);  
  7.            }  
  8.            for(int i = 0; i < 5; i++) {  
  9.                   m.put(String.valueOf(i),1);  
  10.            }  
  11.            System.out.println(m.size()+ " distinct words detected:");  
  12.            System.out.println(m);  
  13.            Set<String>set = m.keySet();  
  14.            Iteratorit = set.iterator();  
  15.            while(it.hasNext()) {  
  16.                   System.out.println(m.get(it.next()));  
  17.            }  
  18.     }  
  19. }</span></span>  

增强for循环

•     增强的for循环对于遍历array 或 Collection的时候相当简便

•     缺陷:

–    数组:

•     不能方便的访问下标值

–    集合:

•     与使用Iterator相比,不能方便的删除集合中的内容

•     总结:

–    除了简单遍历并读出其中的内容外,不建议使用增强for

业务思想

JAVA提供的这个接口专门约束我们容器的实现类,从很大程度上减轻了我们设计人员的负担,提高效率。

容器类的总结相对来说是比较零散的知识点,很难完整的讲述出来,在不断的使用中,可以得到更好地理解。


0 0
原创粉丝点击