Java 集合框架 (JCF)

1. Java 集合框架 (Java Collections Framework)

  • 集合 (Collection),存储元素的容器
  • 图 (Map),存储K-V 键值对的容器

1.1 Java 集合类 (Collection)

  • List 集合 (线性集合)
  • Set 集合 (去重集合)
  • Queue 集合 (队列集合)

2. 集合 (Collection)

2.1 Collection 接口

特点:代表一组任意类型的对象

public interface Collection extends Iterable {

    Iterator iterator();

    boolean removeAll(Collection> c);
    boolean retainAll(Collection> c);
}
@Test
public void demo() {
    Collection collection = new ArrayList();
    collection.add("hello");
    collection.add("world");

    Iterator iterator = collection.iterator();
    while (iterator.hasNext()) {
        String next = iterator.next();
        if ("world".equals(next)) {
            // collection.remove(next); 会抛异常 java.util.ConcurrentModificationException
            iterator.remove();
        }
    }

    System.out.println(collection);
}

2.2 集合与数组

  • 数组长度固定,集合长度不固定
  • 数组可以存储基本类型和引用类型,集合只能存储引用类型 (放入基本类型时,会自动装箱)

2.3 List vs Set

List 接口特点:有序、有下标、元素可重复
Set 接口特点:无序、无下标、元素不能重复

3. Map 接口

3.1 HashMap (散列映射)

  • k-v 存储

3.2 TreeMap (树映射)

  • 按排序顺序存储 键值对

【信息由网络或者个人提供,如有涉及版权请联系COOY资源网邮箱处理】

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容