集合框架
*->Collection接口:单列集合:用来存储一个一个的对象
/--List接口:存储有序的、可重复的数据。 类似“动态”数组
/--ArrayList,作为List接口的主要实现类:线程不安全的,效率 高,底层使用Object[] elementData存储
LinkedList,对于频繁的插入、删除操作,使用此类效率比ArrayList 高:底层使用了双向链表存储
Vector,作为List接口的古老实现类:线程安全的,效率低;底层 使用Object[] elementData存储
ArrayList源码分析:jdk7情况下
ArrayList()底层空参构造器创建了长度是10的Object[] elementData数组
Add()方法//如果此次添加导致底层Object[] elementData数组容量不足,则扩容
默认情况下,扩容为原来容量的1.5倍,同时需要将原有数组中的数据复制到新的数组当中。类似于饿汉式
结论:建议使用带参的构造器:
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}
直接确定数组的容量,减少扩容次数
Jdk8中ArrayList的变化
ArrayList()//底层创建了长度为0的Object[] elementData数组
当第一次使用add()方法添加元素时,才创建长度为10的Object[] elementData数组()类似于懒汉式。后续的添加删除等操作与jdk7中一致
ArrayList,LinkedList,Vector的异同
同:单列集合:用来存储一个一个的对象,存储有序的、可重复的数据。 类似“动态”数组
异:如上
/--Set接口:存储无序的、不可重复的数据 一般常说的集合
/--HashSet
,LinkdHashSet
,TreeSet
*->Map接口:双列集合,用来存储一对映射关系(key - value)一对的数据 ---》集合---》集合的映射
/--HashMap,LinkedHashMap,TreeMap,Hashtable,Properties
向Collection接口的实现类的对象中添加数据obj时,要求obj所在的类要重写equals方法
Arrays类下返回的ArrayList 并不能进行 add() 和 remove() 操作
集合元素的遍历操作:使用迭代器Iterator接口
*1.内部的方法hasNext()和next()
*2.集合对象每次调用iterator()方法都得到一个全新的迭代器对象,默认指针都在集合的第一个元素之前
*3.Iterator接口内部定义了remove(),可以在遍历的时候,删除集合中的元素。此方法不同于集合直接调用remove()
Arrays.asList()中传参如果是基本数据类型数组,会被认为是一个元素,想被认为是多个元素要使用包装类,或者对象数组
增强for循环的局部变量仅用于接收集合元素,修改obj的值并不能修改集合元素,想要修改只能用集合里面的方法,其实增强for循环内部仍然使用了迭代器
class JavaCollection_Use {
public static void main(String[] args) {
//创建Collection的实现类ArrayList的对象
//add(E e)
//确保此集合包含指定的元素(可选操作) 即(向集合中添加对象)
Collection collection=new ArrayList();
Collection collection1=new ArrayList();
collection.add(123);
collection.add(LocalDateTime.now());
collection1.add(456);
collection1.add(new Date());
//size()
//返回此集合中的元素数。
System.out.println(collection.size());
System.out.println(collection);
//addAll(Collection<? extends E> c)
//将指定集合中的所有元素添加到此集合(可选操作)。
collection.addAll(collection1);
System.out.println(collection);
//clear()
//从此集合中删除所有元素(可选操作)。
collection.clear();
System.out.println(collection);
System.out.println(collection.size());
//isEmpty()
//如果此集合不包含元素,则返回 true 。
System.out.println(collection.isEmpty());
//asList(T... a)
//返回由指定数组支持的固定大小的列表。
List<Integer> integers = Arrays.asList(123, 456, 789);//返回一个List对象
Collection collection2=integers;
// contains(Object o)
// 如果此集合包含指定的元素,则返回 true 。
System.out.println(collection2.contains(456));
//containsAll(Collection<?> c)
//如果此集合包含指定 集合中的所有元素,则返回true。 即形参集合是否是主集合的子集的判断,都要调用equals方法
List<Integer> integers1 = Arrays.asList(456);
Collection collection3=integers1;
System.out.println(collection2.containsAll(collection3));
//equals(Object o)
//将指定的对象与此集合进行比较以获得相等性。这个对象应该要为集合
List<Integer> integers2 = Arrays.asList(new Integer[]{123, 456, 789});
Collection collection4=integers2;
System.out.println(collection4.equals(collection2));
//hashCode()
//返回此集合的哈希码值。
System.out.println(collection1.hashCode());
//iterator()
//返回此集合中的元素的迭代器。 用于遍历集合元素
//建议使用这样子的遍历
Iterator iterator = collection4.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
Collection peoples = Arrays.asList(new Person[]{new Person("小许", 19), new Person("小白", 20),
new Person("小林", 18), new Person("小白", 20)});
//removeAll(Collection<?> c)
//删除指定集合中包含的所有此集合的元素(可选操作)。
// peoples.remove(new Person("小白", 20));
// System.out.println(peoples);
Collection collection5=new ArrayList();
collection5.addAll(peoples);
System.out.println(collection5.remove(new Person("小白", 20)));
//retainAll(Collection<?> c)
//仅保留此集合中包含在指定集合中的元素(可选操作)。
//直接对原集合进行操作
collection5.retainAll(Arrays.asList(new Person("小林",18)));
collection5.add(123);
collection5.add(456);
collection5.add(789);
collection5.add(246);
//集合---->数组
// toArray()
// 返回一个包含此集合中所有元素的数组。
//Iterator接口中的remove()方法使用,不同于集合中的remove()
Iterator iterator1 = collection5.iterator();
while (iterator1.hasNext()){
if(iterator1.next().equals(123))
iterator1.remove();
}
iterator1 = collection5.iterator();
while (iterator1.hasNext()){
System.out.println(iterator1.next());
}
//遍历集合的方式二,增强for循环
//for(集合元素类型 局部变量:集合对象)
for (Object obj : collection5) {
System.out.println(obj);
}
}
}
class Person{
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
if (age != person.age) return false;
return name != null ? name.equals(person.name) : person.name == null;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + age;
return result;
}
}