这几篇是我按网上的教程来实习的。

URL:

http://www.cnblogs.com/jbelial/archive/2013/03/27/2981395.html

打代码的感觉挻好的。。

注意在遍历Collection类时,JDK 1.5 提供的 foreach 循环来代替访问集合元素更加便捷:

注意:当使用Iterator 来访问 Collection 集合元素时,Collection 集合里面的元素不能被改变(不可通过集合对象的方法操作集合中的元素,会发生异常)。只能通过Iterator 的方法操作元素。

  把取出方式定义在集合的内部,这样取出方式可以直接访问集合内容的元素。去除方式就被定义成内部类。每个容器的数据结构不同,所以取出的动作细节也不一样,但是都有共性内容:判断和取出

import java.util.*;

public class CollectionText {
  public static void main(String[] args){
    Collection e = new ArrayList();
    System.out.println("Set is empty?: " + e.isEmpty());
    collectionAddMethod(e);
    System.out.println("Set is empty?: " + e.isEmpty());

    Iterator it = e.iterator();
    while (it.hasNext()) {
      String ei = (String) it.next();
      System.out.println("iterator value: " + ei);
    }

    for (Object obj: e) {
      String ei = (String) obj;
      System.out.println("for value: " + ei);
    }
    System.out.println("=============================");
    collectionRemoveMethod(e);
    System.out.println("=============================");
    collectionRetainAllMethod(e);
    System.out.println("=============================");
    collectionContainsMethod(e);
  }
  public static void collectionAddMethod(Collection e) {
    e.add("I am add 1");
    e.add("I am add 2");
    e.add("I am add 3");
    e.add("I am add 4");
    System.out.println("after add element: ");
    System.out.println(e);
  }
  public static void collectionRemoveMethod(Collection e) {
    e.remove("I am add 3");
    System.out.println("after remove element: ");
    System.out.println(e);
  }
  public static void collectionRetainAllMethod(Collection e) {
    Collection e1 = new ArrayList();
    e1.add("I am add 4");
    System.out.println("before retainAll,e is:  " + e);
    e.retainAll(e1);
    System.out.println("after retainAll, e is: " + e);
  }
  public static void collectionContainsMethod(Collection e) {
    System.out.println("If e have I am add 4?: " + e.contains("I am add 4"));
    System.out.println("If e have I am add 1?: " + e.contains("I am add 1"));
    System.out.println("If e have I am add 45?: " + e.contains("I am add 45"));
  }
}

Java 集合之Collection 接口和遍历方法的更多相关文章

  1. Java集合之Collection接口

    java的集合分为三大接口,分别是Collection,Map,Iterator,集合接口和类在java.util包中,此次主要介绍三大接口之一的Collection接口. 一些Collection允 ...

  2. Java集合中Map接口的使用方法

    Map接口 Map提供了一种映射关系,其中的元素是以键值对(key-value)的形式存储的,能够实现根据key快速查找value: Map中的键值对以Entry类型的对象实例形式存在: 建(key值 ...

  3. 集合中list、ArrayList、LinkedList、Vector的区别、Collection接口的共性方法以及数据结构的总结

    List (链表|线性表) 特点: 接口,可存放重复元素,元素存取是有序的,允许在指定位置插入元素,并通过索引来访问元素 1.创建一个用指定可视行数初始化的新滚动列表.默认情况下,不允许进行多项选择. ...

  4. java集合 之 Collection和Iterator接口

    Collection是List,Queue和Set接口的父接口,该接口里定义的方法即可用于操作Set集合,也可以用于List和Queue集合.Collection接口里定义了如下操作元素的方法. bo ...

  5. Java精选笔记_集合概述(Collection接口、Collections工具类、Arrays工具类)

    集合概述 集合有时又称为容器,简单地说,它是一个对象,能将具有相同性质的多个元素汇聚成一个整体.集合被用于存储.获取.操纵和传输聚合的数据. 使用集合的技巧 看到Array就是数组结构,有角标,查询速 ...

  6. Java集合(Collection)综述

    1.集合简介 数学定义:一般地,我们把研究对象统称为元素.把一些元素组成的总体叫做集合. java集合定义:集合就是一个放数据的容器,准确的说是放数据对象引用的容器. java中通用集合类存放于jav ...

  7. Java中的List接口特有的方法

    import java.util.ArrayList; import java.util.List; /* List接口中特有方法: 添加 add(int index, E element) addA ...

  8. Java集合框架Collection

    转自:http://www.cdtarena.com/javapx/201306/8891.html [plain] view plaincopyprint?01.在 Java2中,有一套设计优良的接 ...

  9. Java集合:Collection、List、Set、Map、泛型

    1.集合的理解和好处 2.集合的框架体系图 ★ 3.Collection接口的特点和使用 ★ 4.List和Set接口的特点和使用★ 5.List接口的实现类学习★ 6.Set接口的实现类学习★ 7. ...

随机推荐

  1. nyoj--990--蚂蚁感冒(模拟)(思维题)

    蚂蚁感冒 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 长100厘米的细长直杆子上有n只蚂蚁.它们的头有的朝左,有的朝右. 每只蚂蚁都只能沿着杆子向前爬,速度是1厘米/ ...

  2. treap平衡树

    今天集训讲平衡树,就瞎搞了一下.直接下代码. #include<iostream> #include<cstdio> #include<cmath> #includ ...

  3. Java-java-com-util-common-service:TreeService.java

    ylbtech-Java-java-com-util-common-service:TreeService.java 1.返回顶部 1. package com.shineyoo.manager.ut ...

  4. UIDynamicBehavior的简单使用:接球小游戏

    一.概念扩充: 1.在开发中,我们可以使用UIKit中提供的仿真行为,实现与现实生活中类似的物理仿真动画,UIKit动力学最大的特点是将现实世界动力驱动的动画引入了UIKit,比如重力,铰链连接,碰撞 ...

  5. Node.js安全清单

    前言 安全性,总是一个不可忽视的问题.许多人都承认这点,但是却很少有人真的认真地对待它.所以我们列出了这个清单,让你在将你的应用部署到生产环境来给千万用户使用之前,做一个安全检查. 以下列出的安全项, ...

  6. Arbitrage(floyd)

    http://poj.org/problem?id=2240 #include <stdio.h> #include <string.h> <<; ][]; ][] ...

  7. [Apple开发者帐户帮助]三、创建证书(8)撤销证书

    您可以根据证书类型和角色撤消证书.有关详细信息,请转到撤消权限. 要了解撤销证书时会发生什么,请转到Apple Developer支持中的证书. 所需角色:帐户持有人或管理员. 在“ 证书”,“标识符 ...

  8. 优先队列 + 并查集 + 字典树 + 欧拉回路 + 树状数组 + 线段树 + 线段树点更新 + KMP +AC自动机 + 扫描线

    这里给出基本思想和实现代码 . 优先队列 : 曾经做过的一道例题       坦克大战 struct node { int x,y,step; friend bool operator <(no ...

  9. java线程中断

    public void Thread.interrupt() // 无返回值 public boolean Thread.isInterrupted() // 有返回值 public static b ...

  10. css2.0文档查阅及字体样式

    css2.0文档查阅下载     网址:http://soft.hao123.com/soft/appid/9517.html <html xmlns="http://www.w3.o ...