Java集合整理(主要是常用集合的使用)

开篇

之前写过一篇java的数据结构,里面的内容有点不够清晰,今天在这边整理出java标准库里面的一些数据结构,也就是java中的集合。主要是为了实际的应用,所以如果想看源码解析的童鞋可以关闭这篇博客了。

collection接口

盗张图,很好的展示了collection

List

ArrayList

初始化的四种方法

        ArrayList a = new ArrayList();
        a.add("xiao");
        a.add("fei");
        a.add("java");
        System.out.println(a);

        List<Integer> b = new ArrayList(4);
        b.add(4);
        b.add(3);
        b.add(2);
        b.add(1);
        System.out.println(b);


        List<Integer> c = new ArrayList(Arrays.asList(1,2,3,4,5,6));
        System.out.println(c);

常用方法示例

import java.util.*;

public class ListTest
{
    public static void main(String[] args) 
    {
        List books = new ArrayList();
        //向books集合中添加三个元素
        books.add(new String("轻量级Java EE企业应用实战"));
        books.add(new String("疯狂Java讲义"));
        books.add(new String("疯狂Android讲义"));
        System.out.println(books);

        //将新字符串对象插入在第二个位置
        books.add(1 , new String("疯狂Ajax讲义"));
        for (int i = 0 ; i < books.size() ; i++ )
        {
            System.out.println(books.get(i));
        }

        //删除第三个元素
        books.remove(2);
        System.out.println(books);

        //判断指定元素在List集合中位置:输出1,表明位于第二位
        System.out.println(books.indexOf(new String("疯狂Ajax讲义")));  //①
        //将第二个元素替换成新的字符串对象
        books.set(1, new String("LittleHann"));
        System.out.println(books);

        //将books集合的第二个元素(包括)
        //到第三个元素(不包括)截取成子集合
        System.out.println(books.subList(1 , 2));
    }
LinkedList

LinkedList 与 ArrayList 一样实现 List 接口,只是 ArrayList 是 List 接口的大小可变数组的实现,LinkedList 是 List 接口链表的实现。基于链表实现的方式使得 LinkedList 在插入和删除时更优于 ArrayList,而随机访问则比 ArrayList 逊色些。

LinkedList 实现所有可选的列表操作,并允许所有的元素包括 null。

除了实现 List 接口外,LinkedList 类还为在列表的开头及结尾 get、remove 和 insert 元素提供了统一的命名方法。这些操作允许将链接列表用作堆栈、队列或双端队列。

此类实现 Deque 接口,为 add、poll 提供先进先出队列操作,以及其他堆栈和双端队列操作。

所有操作都是按照双重链接列表的需要执行的。在列表中编索引的操作将从开头或结尾遍历列表(从靠近指定索引的一端)。

同时,与 ArrayList 一样此实现不是同步的。

常用api的使用

import java.util.*;
public class LinkedListTest {

    public static void main(String[] args)
    {
        List<Integer> test = new LinkedList<>(Arrays.asList(1,2,3,4,5,6));
        System.out.println(test.toString());

        System.out.println("test.getFirst():"+((LinkedList<Integer>) test).getFirst());
        System.out.println("test.element():"+((LinkedList<Integer>) test).element());

        System.out.println("test.peek():"+((LinkedList<Integer>) test).peek());

        //删除第一个元素
        System.out.println("test.removeFirst():"+((LinkedList<Integer>) test).removeFirst());
        System.out.println(test.toString());

        //取出第一个元素
        System.out.println("test.poll():"+((LinkedList<Integer>) test).poll());
        System.out.println(test.toString());

        ((LinkedList<Integer>) test).addFirst(new Integer(1));
        System.out.println(test.toString());

        ((LinkedList<Integer>) test).offer(new Integer(7));
        System.out.println(test.toString());

        test.add(new Integer(8));
        System.out.println(test.toString());

        ((LinkedList<Integer>) test).addLast(9);
        System.out.println(test.toString());

    }
}

stack

import java.util.*;
public class StackTest {

    public static void main(String[] args)
    {
        Stack<Integer> stack = new Stack<>();
        stack.push(new Integer(11));
        stack.push(new Integer(12));
        System.out.println(stack.peek());
        System.out.println(stack.toString());

        System.out.println(stack.pop());
        System.out.println(stack.toString());

    }
}

Queue

优先队列

import java.util.*;
public class PriorityQueueDemo {
    public static void printQ(Queue queue)
    {
        while (queue.peek()!=null)
        {
            System.out.print(queue.remove()+" ");

        }
        System.out.println();
    }
    public static void main(String[] args)
    {
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
        Random rand = new Random(47);
        for(int i = 0; i<10; i++)
        {
            priorityQueue.offer(rand.nextInt(i+10));
        }
        printQ(priorityQueue);


        List<Integer> ints = Arrays.asList(25,22,20,18,14,9,3,1,1,2,3,9,14,18,21,23,25);
        priorityQueue = new PriorityQueue<Integer>(ints);
        printQ(priorityQueue);


        priorityQueue = new PriorityQueue<Integer>(ints.size(),Collections.reverseOrder());
        priorityQueue.addAll(ints);
        printQ(priorityQueue);
    }
}

set

Hashset

参考博客

可以解决寻找数组中第一个重复数的问题,但是需要额外的空间,如果不允许额外空间,就要像剑指offer里面那样。

关于equal方法的覆盖

import java.util.*;
public class SetTest {
    public static void main(String[] args)
    {
        Set<Integer> test = new HashSet<>();
        Random rand = new Random(47);
        for(int i = 0; i<10000; i++)
        {
            test.add(rand.nextInt(30));
        }
        System.out.println(test);


        Set<String> set1 = new HashSet<>();
        Collections.addAll(set1,"A B C D E F G H I J K L".split(" "));
        set1.add("M");

        System.out.println("H: "+set1.contains("H"));
        System.out.println("N: "+set1.contains("N"));

        Set<String> set2 = new HashSet<>();
        Collections.addAll(set2,"H I J K L".split(" "));

        System.out.println("set2 in set1: "+ set1.containsAll(set2));

        set1.remove("H");
        System.out.println("set2 in set1: "+ set1.containsAll(set2));

        set1.removeAll(set2);
        System.out.println("set1-set2:"+set1);

        Collections.addAll(set1, "X Y Z".split(" "));
        System.out.println(set1);


    }



}

Map

HashMap

参考博客

序列的迭代器

有了迭代器就不用为序列中的元素个数而操心了。

import java.util.*;
public class JavaIterator {

    public static void main(String[] args)
    {
        List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5,6));
        Iterator<Integer> it = list.iterator();
        while (it.hasNext())
        {
            Integer i = it.next();
            System.out.println(i);

        }
        System.out.println("---------------------");

        for(Integer i : list)
        {
            System.out.println(i);
        }

        it = list.iterator();
        for(int i = 0;i<6;i++)
        {
            it.next();
            //获得序列中的下一个元素
            it.remove();
            //将迭代器新近返回的元素删除
        }
        System.out.println(list.toString());
    }
}

可以向前迭代的迭代器

import java.util.*;
public class JavaIterator {

    public static void main(String[] args)
    {
        List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8));
        ListIterator<Integer> it = list.listIterator();
        while (it.hasNext())
        {

            System.out.println(it.next()+","+it.nextIndex()+","+it.previousIndex()+";");

        }
        System.out.println("---------------------");

        //从后向前迭代
        while (it.hasPrevious())
        {
            System.out.println(it.previous());
        }

        it = list.listIterator(3);
        //从index为3的地方向后修改序列里面的元素
        while(it.hasNext())
        {
            it.next();
            it.set(111);
        }

        System.out.println(list.toString());
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值