初识Collection

2017-08-28  本文已影响0人  阿来_828

今天上午初识了Collection(容器),代码如下:
package collection;
import java.util.Collection;
import java.util.Iterator;
public class TestCollection {
public static <E> void main(String[] args) {
//Collection有2个子类 List和Set
Collection collection = new Collection<E>() {
//@return the number of elements in this collection
public int size() {
return 0;
}
//@return <tt>true</tt> if this collection contains no element
public boolean isEmpty() {
return false;
}
// Returns <tt>true</tt> if this collection contains the specified element.
public boolean contains(Object o) {
return false;
}
//@return an <tt>Iterator</tt>over the elements in this collection(返回一个迭代器,用于遍历数组)
public Iterator<E> iterator() {
return null;
}
//@return an array containing all of the elements in this collection
public Object[] toArray() {
return null;
}
public <T> T[] toArray(T[] a) {
return null;
}

    public boolean add(E e) {
        return false;
    }
    //Removes a single instance of the specified element from this
    // collection, if it is present (optional operation).
    public boolean remove(Object o) {
        return false;
    }
    public boolean containsAll(Collection<?> c) {
        return false;
    }
    public boolean addAll(Collection<? extends E> c) {
        return false;
    }
    public boolean removeAll(Collection<?> c) {
        return false;
    }
    public boolean retainAll(Collection<?> c) {
        return false;
    }
    public void clear() {
        
    }
};

}
}

package collection;
import java.util.ArrayList;
import java.util.Date;
public class List {
public static void main(String[] args) {
/**
* List 常用子类 ArrayList(数组表,其中可以放任意对象)底层实现是数组(查询操作快,增删改操作慢、线程不安全效率低)
* LinkedList(链表)底层实现是链表(链表查询慢,增删改操作快、线程不安全效率低)
* Vector底层实现是数组,线程安全,效率低
* List list = new ArrayList();这是典型写法,左边是接口右边为实现类(父类引用指向子类对象即多态)
*/
List list = new List();
System.out.println(list.getClass().getName());
ArrayList list2 = new ArrayList();

//list接口中定义了一个Object类的数组,其中可以放任意类型的对象
list2.add("123");//添加字符串对象String
list2.add(123);//此处编译器自动将123包装为integer类
list2.add(new Date());//添加时间对象Date
list2.add(new Dog());//添加自定义对象
System.out.println(list2.size());//打印数组中含有元素的长度(不是数组的长度,ArrayList中定义的数组长度可变的)
list2.remove("123");//移除数组
System.out.println(list2.size());
System.err.println(list2.isEmpty());

ArrayList list3 = new ArrayList();
list3.add("abc");
list2.addAll(list3);//将list3中的所有对象放到list2
System.out.println(list2.size());
//根顺序有关的操作
list2.get(0);//返回数组索引为0的对象
list2.set(0, "456");//将数组索引为0的对象替换成String对象"456"
list2.remove(0);//将数组索引为0的对象移除掉"
//List list4 = new ArrayList();//接口List引用来指向ArrayList对象

}

}

class Dog{

}
阶段知识总结:
【collection接口的概念】
1.collection表示一组对象,其作用是收集数据
2.Collection函数库即java.util包下的一些类与接口。类用来产生对象,而接口是访问数据的方式
3.Collection函数库与数组的不同:
.数组容量是有限的,Collection库的容量可以调节
.Collection函数库只能用来存放对象,数组没有限制
Collection接口是Collection层次结构中的根接口,它定义了一些最基本的访问方法,让我们能用统一的方式通过它和它的子类访问数据
【Collection接口下的子类接口List、Set(子类HashSet)与Map(子类HashMap)的区别】
List中存放的数据有序可重复 Set中存放的数据无序不可重复,后面传入的相同数据会覆盖掉前面的数据
Map(键值对):通过一个对象找到另一个对象
【接口可以实现设计和实现的分离】
【Object与String类中equals方法区别】
Object中equals方法比较的是2个对象的地址(是否为同一对象) String重写了Object的equals比较的是2个字符串变量的内容
【Array与LinkedList】
数组查询操作快,增删改操作慢;链表查询慢,增删改操作快

上一篇下一篇

猜你喜欢

热点阅读