首页 > 其他 > 详细

36 Collection接口

时间:2021-04-03 20:30:57      阅读:32      评论:0      收藏:0      [点我收藏+]

Collection接口

●一方面,面向对象语言对事物的体现都是以对象的形式,为了方便对多个对象的操作,就要对对象进行存储。另一方面,使用Array存储对象方面具有一些弊端,而Java集合就像一种容器,可以动态地把多个对象的引用放入容器中。

说明:此时的存储,主要指的是内存层面的存储,不涉及到持久化的存储

≥数组在内存存储方面的特点: √数组初始化以后,长度就确定了。 √数组声明的类型,就决定了进行元素初始化时的类型,String[] arr,int[] arr,

≥数组在存储数据方面的弊端: √数组初始化以后,长度就不可变了,不便于扩展 √数组中提供的属性和方法少,不便于进行添加、删除、插入等操作,且效率不 高。同时无法直接获取存储元素的个数 √数组存储的数据是有序的、可以重复的,对于无序、不可重复的需求,不能满 足。

---->存储数据的特点单一 Java集合类可以用于存储数量不等的多个对象,还可用于保存具有映射关系的关联数组。

 

Java集合可分为 Collection和 Map 集合

≥Collection接口:单列数据,定义了存取一个一个的对象

√List接口: 元素有序、可重复的集合。 --> "动态"数组

--->子接口的具体实现类 ArrayList 、LinkedList 、Vector

√Set接口: 元素无序、不可重复的集合

---->HashSet 、LinkHashSet 、TheeSet

≥Map 接口:双列数据,保存具有映射关系“(key - value)”一对的数据 -->函数 y= f(x)

---->HashMap 、LinkHashMap 、TressMap 、 Hashtable 、Properties

 

Collection接口的常用方法1

 @Test
   public void test(){
       Collection coll = new ArrayList();
?
       //add(Object e):将元素添加到集合coll中
       coll.add("AA");
       coll.add("bb");
       coll.add(123);//自动装箱
       coll.add(new Date());
?
       //size();获取添加的元素的个数
       System.out.println(coll.size());//4
?
       //addAll();将coll1集合中的元素添加到当前的集合中
       Collection coll1 = new ArrayList();
       coll1.add(456);
       coll1.add("cc");
       coll.addAll(coll1);
       System.out.println(coll.size());//6
       System.out.println(coll);//相当于调toString()
?
       //clear();清空集合元素
       coll.clear();
?
       //isEmpty():判断当前集合是否为空
       System.out.println(coll.isEmpty());//true
  }

 

Collection接口的常用方法2

向Collection接口的实现类的对象中添加数据obj时,要求obj所在类要重写equals()

  @Test
   public void test(){
       Collection coll = new ArrayList();
       coll.add(123);
       coll.add(456);
       coll.add(new String("Tom"));
       coll.add(false);
       coll.add(new Person("jerry",20));
?
       //1.contains(Object obj):当前集合中是否包含obj
       //我们在判断时会调用obj对象所在类的equals()
       boolean contains = coll.contains(123);
       System.out.println(contains);//true
       System.out.println(coll.contains(new String("Tom")));//true
?
       //2.containsAll(Collection coll1):判断形参coll中的所有元素是否都存在于当前集合中
       Collection coll1 = Arrays.asList(123,456);
       System.out.println(coll.containsAll(coll1));
  }

 

Collection接口的常用方法3

    @Test
   public void test2() {
       Collection coll = new ArrayList();
       coll.add(123);
       coll.add(456);
       coll.add(new String("Tom"));
       coll.add(false);
       coll.add(new Person("jerry", 20));
?
       //remove(Object o): 从当前集合中移除obj元素
       coll.remove(123);
       System.out.println(coll);
?
       //removeAll(Collection coll): 从当前集合中移除coll中所有元素
       Collection coll1 = Arrays.asList(123, 4567);
       coll.removeAll(coll1);
       System.out.println(coll);
  }
?
?
   @Test
   public void test3(){
       Collection coll = new ArrayList();
       coll.add(123);
       coll.add(456);
       coll.add(new String("Tom"));
       coll.add(false);
       coll.add(new Person("jerry", 20));
?
       //retainAll(Collection coll):交集,获取当前集合和coll1的交集,并返回给当前集合
       Collection coll1 = Arrays.asList(123, 456,789);
       coll.retainAll(coll1);
       System.out.println(coll);
?
       //equals(Object obj):要想返回true,需要当前集合和形参集合的元素都相同
       System.out.println(coll.equals(coll1));
  }

 

Collection接口的常用方法4

    @Test
   public void test4(){
       Collection coll = new ArrayList();
       coll.add(123);
       coll.add(456);
       coll.add(new String("Tom"));
       coll.add(false);
       coll.add(new Person("jerry", 20));
?
       //hashCode():返回当前对象的hash值
       System.out.println(coll.hashCode());
?
       //集合 ---> 数组 toArray()
       Object[] arr = coll.toArray();
       for (int i = 0;i < arr.length;i++){
           System.out.println(arr[i]);
      }
?
       //拓展:数组 ---> 集合
       List<String> list = Arrays.asList(new String[]{"AA", "BB", "CC"});
       System.out.println(list);
?
       //iterator():返回Iterator接口的实例,用于遍历集合元素。放在IteratorTest.java中测试
       
  }

 

 

集合元素的遍历操作

使用迭代器Iterator接口

Iterator对象称为迭代器(设计模式的一种),主要用于遍历Collection集合中的元素。

GOF给迭代器模式的定义为:提供一种方法访问一个容器(container)对象中各个元素,而又不需暴露该对象的内部细节。迭代器模式,就是为容器而生。类似于“公交车上的售票员”、“火车上的乘务员”、“空姐”。 ? Collection接口继承了java.lang.lterable接口,该接口有一个iterator()方法,那么所有实现了Collection接口的集合类都有一个iterator()方法,用以返回一个实现了lterator接口的对象。 ? lterator仅用于遍历集合,lterator木身并不提供承装对象的能力。如果需要创建lterator对象,则必须有一个被迭代的集合。 ? 集合对象每次调用iterator()方法都得到一个全新的迭代器对象**,默认游标都在集合的第一个元素之前。

 

内部的方法:hasNext():判断是否还有下一个元素

next():指针下移 将下移位置上的元素返回

    @Test
   public void test(){
       Collection coll = new ArrayList();
       coll.add(123);
       coll.add(456);
       coll.add(new String("Tom"));
       coll.add(false);
       coll.add(new Person("jerry", 20));
?
       Iterator iterator = coll.iterator();
       //方式一
//       System.out.println(iterator.next());
//       System.out.println(iterator.next());
//       System.out.println(iterator.next());
//       System.out.println(iterator.next());
//       System.out.println(iterator.next());
//       异常 NoSuchFieldException
//       System.out.println(iterator.next());
//
?
       //方式二: 不推荐
       for(int i = 0; i < coll.size();i++){
           System.out.println(iterator.next());
      }
?
       //方式三
       while(iterator.hasNext()){
           System.out.println(iterator.next());
      }
  }
}

 

迭代器remove方法

如果还未调用next()或在上一次调用next方法之后已经调用了 remove方法,在调用remove都会报异常

  @Test
   public void test2(){
       Collection coll = new ArrayList();
       coll.add(123);
       coll.add(456);
       coll.add(new String("Tom"));
       coll.add(false);
       coll.add(new Person("jerry", 20));
?
       //删除集合中的“Tom”
       Iterator iterator = coll.iterator();
       while(iterator.hasNext()){
          Object o = iterator.next();
          if("Tom".equals(o)){
              iterator.remove();
          }
      }
      iterator = coll.iterator();
       while(iterator.hasNext()){
           System.out.println(iterator.next());
      }
  }

 

for each循环

    @Test
  public void test(){
      Collection coll = new ArrayList();
      coll.add(123);
      coll.add(456);
      coll.add(new String("Tom"));
      coll.add(false);
      coll.add(new Person("jerry", 20));
?
      //for(集合元素的类型 局部变量 : 集合对象)
      //内部仍然调用了迭代器
      for(Object obj:coll){
          System.out.println(obj);
      }
  }
?
@Test
  public void test2(){
      int[] arr = new int[]{1,2,3,4,5};
      //for(数组元素的类型 局部变量 : 数组对象)
      for(int i : arr){
          System.out.println(i);
  }
}

36 Collection接口

原文:https://www.cnblogs.com/flypigggg/p/14613979.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!