大数据

Java集合Collection和泛型

2020-02-16  本文已影响0人  喵感数据
集合的原理是什么呢 ?

集合有多种实现,其实就是集合类去描述集合对象,赋予了该类对象存储对象的能力。集合实现有数组、链表、哈希表、二叉树等。

集合的类都有哪些 ?

主要有两大类别:分别是 Collection(单身住宿):一次存储一个对象 和 Map(情侣入住):一次存储两个对象。

Collection介绍

Collection 是单身住宿的顶级接口,是List 接口和 Set 接口的父接口。

public interface Collection<E> extends Iterable<E>{}

Collection 接口的实现类对象代表了一个集合 , 该集合可以存储一堆引用数据,也可以存储一堆对象。Collection 接口的实现类对象,存储大量的对象,这些对象被称为:集合的元素 。

Collection 实现类之间也有区别:
部分允许存储的元素是可重复的 ; 一部分允许存储的元素是唯一不可重复的;部分存储的元素是有序的 ; 一些存储的元素是无序的。

Collection是集合类的上级接口,继承与他有关的接口主要有List和Set。
Collections是针对集合类的一个帮助类,他提供一系列静态方法实现对各种集合的搜索、排序、线程安全等操作。

Collection 方法学习 :

我们这里将 ArrayList集合作为 Collection 的实现类
public class CollectionMethod{
        Collection<String> collection = new ArrayList<String>();
        //添加元素
        collection.add("A");
        collection.add("B");
        //collection.addAll("");

        //删除指定元素
        collection.remove("A");
        
        //删除所有元素
        Collection cl = new ArrayList();
        cl.add("Bob");
        collection.removeAll(cl);
        
        //检测是否存在某个元素
        collection.contains("A);
        
        //判断是否为空
        collection.isEmpty();
}  

删除

//clear() 清空当前集合
 public void clear() {
        modCount++;
        // clear to let GC do its work
        for (int i = 0; i < size; i++)
            elementData[i] = null;
        size = 0;
    }

boolean remove(Object o) 移除单个元素

public boolean remove(Object o) {
        if (o == null) {
            for (int index = 0; index < size; index++)
                if (elementData[index] == null) {
                    fastRemove(index);
                    return true;
                }
        } else {
            for (int index = 0; index < size; index++)
                if (o.equals(elementData[index])) {
                    fastRemove(index);
                    return true;
                }
        }
        return false;
}
Collection遍历

1. 迭代器遍历
我们可以通过集合对象,先获得对应的Iterator迭代器。
Collection的父类是 Iterable , 它定义了一个方法 Iterator iterator(); 该方法返回一个 Iterator 对象,Iterator是一个接口,它的实现类对象被称之为:迭代器。

Iterator 和 Iterable 的区别:
Iterable :存在于 java.lang 包中。

可以看到,里面封装了 Iterator 接口。所以只要实现了只要实现了Iterable接口的类,就可以使用Iterator迭代器了。

Iterator :存在于 java.util 包中。核心的方法next(),hasnext(),remove()。

Iterator:迭代器,它是Java集合的顶层接口(不包括 map 系列的集合,Map接口:是 map 系列集合的顶层接口)。
Object next():返回迭代器刚越过的元素的引用,返回值是 Object,需要强制转换成自己需要的类型。
boolean hasNext():判断容器内是否还有可供访问的元素。
void remove():删除迭代器刚越过的元素。
注意:除了 map 系列的集合,我们都能通过迭代器来对集合中的元素进行遍历。

2. foreach遍历
底层调用的就是迭代器,foreach 编程起来更简单,但是不能进行移除操作。
具体代码如下:

public class UseCollection {
    public static void main(String[] args) {
        // 创建一个 collection 实现类的对象
        Collection coll = new ArrayList();
        Collection c2 = new ArrayList();
        coll.add("123");
        coll.add("234");
        coll.add("345");
        coll.add("456");
        
        // 获得 coll 所对应的迭代器
        Iterator it = coll.iterator();
        // 操作迭代器
        while(it.hasNext()){// 判断是否有下一个元素
            Object next = it.next();// 获得下一个元素
            System.out.println(next);
            if(next == "234"){
                it.remove();
            }
        }

        System.out.println("==================");

        for (Object object : coll) {
            System.out.println(object);
        }

        System.out.println(coll);
    }
}
​
泛型

什么是泛型 ?
泛型是 jdk 1.5 的新特性,泛型代表任意类型 ,可以指明为任意类型。

泛型的使用 ?
通常我们定义一个泛型的方法,那么要定义泛型类来装载这个方法
new 泛型类对象的时候有两种选择

  1. 不做任何格外的处理,那么所有的泛型的方法类型默认为Object 类型。
  2. 指明某类型,那么所有的泛型方法类型默认为你指明的类型,当你指明了类型,那么泛型方法的类型就确定了、不能更改了。

泛型的特点 :
泛型编译期间的安全检查,到了运行期泛型就被擦除。

泛型的优势:

  1. 创建对象时不会再警告了。
  2. 所添加的内容已经有了限制,增加了一层安全检查。
  3. 遍历集合时不需要强转了。
public class Test {
    public static void main(String[] args) {
        //指明String类型的泛型
        ArrayList<String> list = new ArrayList<String>();
        System.out.println(list);
        Iterator<String> it = list.iterator();
        while(it.hasNext()){
            String str = it.next();
            System.out.println(str.length());
        }
    }
}
//指明泛型为TestOne,重写的方法返回如下:
class TestOne implements Comparable<TestOne>{
    @Override
    public int compareTo(TestOne o) {
        return 0;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读