import java.util.ArrayList;
import java.util.Collection;
/**
 *Java2的集合框架,抽其核心,主要有三种:List、Set和Map。如下图所示: 
 *需要注意的是,这里的 Collection、List、Set和Map都是接口(Interface),不是具体的类实现。
 * List lst = new ArrayList(); 这是我们平常经常使用的创建一个新的List的语句,在这里, List是接口,ArrayList才是具体的类。
 *
 * 在实际开发中,需要将使用的对象存储于特定数据结构的容器中。JDK提供了这样的容器--集合(Collection)
 * Collection是一个接口,定义了集合相关的操作方法,其中有两个子接口:List与Set
 * List:可重复集,且有序      Set:不可重复集,大部分实现类是无序的        元素是否可重复,取决于元素的equals()比较的结果
 * 
 *集合存放的是元素的引用(地址)
 *集合中存储的都是引用类型元素,并且集合只保存每个元素对象的引用,而非将元素对象本身存入集合
 */
public class CollectionDemo {
    public static void main(String[] args) {
        
/**
         * Collection定义了一个add方法用于向集合中添加新元素
         * boolean add(E e)
         * 该方法会将给定的元素添加集合,若添加成功则返回true,否则返回false
         */
        Collection c=
new ArrayList();
        c.add(
"12");
        c.add(
"46");
        c.add(
"45.5");
        System.out.println(c);
        
/**
         * int size()
         * 该方法用于返回当前集合中的元素总数
         */
        System.out.println(
"size:"+c.size());
        
/**
         * void clear()
         * 该方法用于清空当前集合
         */
        c.clear();
        System.out.println(c);
        
/**
         * boolean isEmpty()
         * 该方法用于判断当前集合中是否不包含任何元素
         */
        System.out.println(
"集合是否不包含任何元素:"+c.isEmpty());
        
/**
         * boolean contains(Object o)
         * 该方法会用于判断给定的元素是否被包含在集合中。包含返回true,不包含返回false
         * 集合在判断元素是否被包含在集合中是根据每个元素的equals()方法进行比较后的结果。
         * 通常有必要重写equals()保证contains()方法的合理结果
         */
        Collection points=
new ArrayList();
        points.add(
new Point(
1,
3));
        points.add(
new Point(
2,
3));
        points.add(
new Point(
3,
3));
        System.out.println(points);
        Point p=
new Point(
1,
3);
        
        System.out.println(
"集合是否包含P元素:"+points.contains(p));
    }
}
class Point {
    
private int x,y;
    
public Point(
int x, 
int y) {
        
super();
        
this.x = x;
        
this.y = y;
    }
    
public int getX() {
        
return x;
    }
    
public void setX(
int x) {
        
this.x = x;
    }
    
public int getY() {
        
return y;
    }
    
public void setY(
int y) {
        
this.y = y;
    }
    
public String 
toString() {
        
return "("+x+
","+y+
")";
    }
    
public boolean equals(Object obj) {
        
        
        
        
if(obj==
null) {
            
return false;
        }
        
if(
this==obj) {
            
return true;
        }
        
if(obj 
instanceof Point) {
            Point other=(Point)obj;
            
return this.x==other.x&&
this.y==other.y;
        }
        
return false;
    }
}