集合(1)

1.迭代器,使用next()获得序列中的下一个元素,使用hasNext()检查序列中是否还有元素,可以理解为iterator()返回一个指向第一个元素前面位置的指针。除了 map 系列的集合,我们都能通过迭代器来对集合中的元素进行遍历

List<String> list = new ArrayList<>();
list.add("Tom");
list.add("Bob");
list.add("Marry");
Iterator itt = null;
itt = list.iterator();
while (itt.hasNext()) {
    String obj = (String)itt.next();
    System.out.println(obj);
}

2.Collection:List 接口, Set 接口,Queue接口的父接口

List :有序,可以重复的集合。继承于 Collection 接口

  

List 接口的三个典型实现:

  ①、List list1 = new ArrayList();

    底层数据结构是数组,查询快,增删慢;线程不安全,效率高

   ②、List list2 = new Vector();

    底层数据结构是数组,查询快,增删慢;线程安全,效率低,几乎已经淘汰了这个集合

   ③、List list3 = new LinkedList();

    底层数据结构是链表,查询慢,增删快;线程不安全,效率高

Set接口

1、Set hashSet = new HashSet();

  ①HashSet无序;不可重复;不是线程安全的;集合元素可以为 NULL;

2、Set linkedHashSet = new LinkedHashSet();

  ①、不可以重复,有序(遍历时按插入顺序);集合元素可以为 NULL;

  因为底层采用 链表 和 哈希表的算法。链表保证元素的添加顺序,哈希表保证元素的唯一性

3、Set treeSet = new TreeSet();

  TreeSet:有序(遍历时按大小);不可重复,底层使用 红黑树算法,擅长于范围查询。

  *  如果使用 TreeSet() 无参数的构造器创建一个 TreeSet 对象, 则要求放入其中的元素的类必须实现 Comparable 接口,所以在其中不能放入 null 元素

Queue接口 https://www.cnblogs.com/lemon-flm/p/7877898.html

优先队列 Queue<User> queue=new PriorityQueue<>(),有序(按大小),不能为null

       poll         移除并返问队列头部的元素    如果队列为空,则返回null
  peek       返回队列头部的元素             如果队列为空,则返回null

Map接口常用类

(1)HashMap,无序,key可以为null;

(2)TreeMap,有序列(遍历时按key大小),key不可以为null;

(3)LinkedHashMap,有序列(遍历时按插入顺序),key可以为null;

(4)Hashtable,被淘汰的,key不可以为null,线程安全,效率低

(5)CurrentHashMap,线程安全,无序,key不可以为null(别问为什么,因为别人程序里没考虑,而HashMap考虑了)。

3.自定义类比较器的使用

(1)内部比较器:实现Comparable接口, 重写compareTo方法

(2)外部比较器:实现Comparator接口, 定义compara比较类

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;

class User implements Comparable<User>{
    private int x,y;
    User(int x,int y){
        this.x=x;
        this.y=y;
    }
    @Override
    public int compareTo(User o) {
        return (x>o.x)||(x==o.x && y>o.y)?1:-1;
    }
    @Override
    public String toString() {
        return "User{" +
                "x=" + x +
                ", y=" + y +
                '}';
    }
    static class Com implements Comparator<User>{

        @Override
        public int compare(User o1, User o2) {
            return (o1.x<o2.x)||(o1.x==o2.x&&o1.y<o2.y)?1:-1;
        }
    }
}
public class Test {
    public static void main(String[] args){
        //Queue<User> queue=new PriorityQueue<>();    //内部比较器
        Queue<User> queue=new PriorityQueue<>(new User.Com());    //外部比较器
        queue.add(new User(3,5));
        queue.add(new User(3,2));
        queue.add(new User(3,7));
        queue.add(new User(1,100));
        queue.add(new User(4,1));
        for (;queue.size()>0;){
            System.err.println(queue.poll());
        }
    }

}

4.set底层使用map实现的。因为null不能比较,所以TreeMap,TreeSet,PriorityQueue这些要比较的key不能为null,还有一个CurrentHashMap key也不能为null,保证有序的集合有LinkHashSet,TreeSet,LinkedHashMap,TreeMap。通过一个双向链表实现有序。对于采用hashset和hashmap的自定义类,一般都要重写hashcode和equals,不然就没有去重的效果了,不重写默认比较地址。

https://www.cnblogs.com/ysocean/p/6555373.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值