Java集合类

集合类主要用于存放为止数目的对象,并对其操作的类。主要由Collection和Map派生出来的接口和实现类组成。

1 Collection接口

Collection接口是List、Set、Queue等接口的父接口。其提供了一系列操作集合元素的方法。

2 List接口

List接口中的元素是有序且可重复的,元素存入顺序和取出顺序一致。
ArryList是其实现类,是一个相对动态数组,它的容量能动态的增长,继承于AbstractList,实现List接口。

import java.util.ArrayList;

public class Demo01 {
    public static void main(String[] args) {
        ArrayList a = new ArrayList();      //创建一个空ArrayList集合
        a.add(100);                         //添加元素
        a.add("hello");
        System.out.println(a);              //打印集合
        System.out.println(a.size());       //打印集合长度
        System.out.println(a.get(1));       //获取对应位置的元素
        a.add(1,"xxx");       //在位置1处插入"xxx"字符串
        System.out.println(a);
        a.remove(2);                  //删除位置2处的内容
        System.out.println(a);
    }
}
/*运行结果:

[100, hello]
2
hello
[100, xxx, hello]
[100, xxx]

进程已结束,退出代码为 0

*/

LinkedList实现类可以解决ArruList存在的删除和增加操作时效率低的问题。

import java.util.LinkedList;

public class Demo02 {
    public static void main(String[] args) {
        LinkedList l = new LinkedList();
        l.add(999);
        l.add("hello");
        l.add(1.11);
        System.out.println(l);
        System.out.println(l.removeFirst());    //删除并返回第一个元素
        System.out.println(l);
        System.out.println(l.getLast());        //打印最后一个元素
    }
}
/*运行结果:

[999, hello, 1.11]
999
[hello, 1.11]
1.11

进程已结束,退出代码为 0

*/

2.1 Iterator接口

在开发过程中经常需要遍历集合中所有元素,Java中为此提供了专门遍历集合的接口——Iterator,也被成为迭代器。

import java.util.*;

public class Demo03 {
    public static void main(String[] args) {
        Collection coll = new ArrayList();
        coll.add(999);
        coll.add("hello");
        coll.add(new Date());
        Iterator i = coll.iterator();
        while (i.hasNext()){
            System.out.println(i.next());
        }
    }
}
/*运行结果:

999
hello
Wed Jun 23 10:43:57 CST 2021

进程已结束,退出代码为 0

*/

首先通过ArryList的iterator()方法获得迭代器的对象,再用hasNext()方法判断集合中时候存在下一个元素,存在就使用next()方法取出。

2.2 foreach循环

foreach是JDK5.0加入的,它既能遍历集合,也能遍历普通数组。但是这种方法不能修改集合中原来的值。

import java.util.*;

public class Demo04 {
    public static void main(String[] args) {
        Collection coll = new ArrayList();
        coll.add(999);
        coll.add("hello");
        coll.add("111");
        for (Object o:coll) {
            System.out.println(o);
        }
    }
}
/*运行结果:

999
hello
111

进程已结束,退出代码为 0

*/

2.3 ListIterator接口

List还额外提供一个listIterator()方法,该方法返回ListIterator对象。相对于Iterator,其可以并发执行操作,而Iterator不能。

3 Set接口

Set集合中元素是无序的、不可重复的,Set接口继承Collection接口,但是其没有扩充方法。
Set接口的主要实现类有两个,HashSet和TreeSet,其中HashSet是根据对象的哈希值来确定元素在集合中的储存位置,因此该校的存取,TreeSet底层是二叉树来实现元素储存的,它可以对集中元素排列。

3.1 HashSet集合

HashSet的存取性能和查找性能都很好,但是不是线程安全的,集合中元素可以为null。

import java.util.*;

public class Demo06 {
    public static void main(String[] args) {
        Set set = new HashSet();
        set.add(null);
        set.add("hello");
        set.add("word");
        set.add("word");
        for (Object o :set){
            System.out.println(o);
        }
        System.out.println(set);
    }
}
/*运行结果:

null
hello
word
[null, hello, word]

进程已结束,退出代码为 0

*/

提示:HashSet之所以能正常工作,是因为String类重写了hashCode()和equals()方法。

3.2 TreeSet集合

TreeSet类是Set接口的另一个实现类,他和HashSet一样,都能保证元素的唯一性。但是TreeSet底层是用自平衡二叉树实现的,所以它既能保证元素的唯一性,又可以对元素进行排序,其还有一些特别的方法。

import java.util.*;
public class Demo07 {
    public static void main(String[] args) {
        TreeSet tree= new TreeSet();                        //新建一个TreeSet集合
        tree.add("hello");                                  //向集合内添加字符串
        tree.add("word");
        tree.add("999");
        System.out.println(tree);                           //打印tree集合,发现其元素顺序为随机顺序
        System.out.println(tree.first());                   //打印其第一个元素
        System.out.println(tree.comparator());              //查询tree集合排序为指定排序还是自然排序,并将其打印
        System.out.println(tree.lower("word"));          //打印word元素的前一个元素
        System.out.println(tree.headSet("word")); //打印集合中world前元素所组成的集合
    }
}
/*运行结果:

[999, hello, word]
999
null
hello
[999, hello]

进程已结束,退出代码为 0

*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值