Java中各种数据结构的常用方法和遍历方式,用处

List

🐖:ArrayList

        ArrayList<Integer> list = new ArrayList<>();
        System.out.println("常用方法============");
        list.clear();//清空ArrayList
        list.add(1);//添加元素
        list.add(2);
        list.add(3);
        System.out.println(list.get(1));//通过数组下标得到对应的值,因为ArrayList底层是数组存储所以支持快速的随机检索
        System.out.println(list.size());//得到ArrayList的现存容量
        System.out.println(list.isEmpty());//判断是否为空
        System.out.println(list.contains(4));//判断是否存在某个元素
        list.remove(1);
        System.out.println();
        System.out.println("遍历方式=============");
        Iterator<Integer> iterator = list.iterator();//通过iterator迭代器进行遍历
        while (iterator.hasNext()){
            Integer next = iterator.next();
            System.out.println(next);
        }

        for (int i=0;i < list.size();i++){//for循环进行遍历
            System.out.println(list.get(i));
        }

        for (Integer item : list){//foreach遍历
            System.out.println(item);
        }

        list.forEach(x->{
            x=x+1;
            System.out.println(x);
        });//jdk1.8语法糖遍历

        list.forEach(System.out::println);//


🐖:LinkedList

System.out.println("LinkedList:底层数据结构是链表,查询慢,增删快,线程不安全,效率高。");
        LinkedList<Integer> linkedList = new LinkedList<>();
        System.out.println("普通的压栈出栈");
        linkedList.add(1);
        linkedList.add(2);
        linkedList.add(3);
        System.out.println(linkedList.get(0));

        System.out.println("作为栈使用的方法");
        linkedList.push(4);//添加到头部,底层调用addfirst()作为栈(stack)使用的方法
        System.out.println(linkedList.poll());//头部出栈
        System.out.println(linkedList.pollFirst());//同上
        System.out.println(linkedList.pollLast());//尾部出栈
        

        System.out.println("作为队列使用的方法");
        linkedList.offer(5);//作为队列使用的方法
        linkedList.offerLast(7);
        linkedList.offerFirst(6);
        System.out.println(linkedList.pop());//头部出栈
        System.out.println(linkedList.pop());

        System.out.println("peek");
        System.out.println(linkedList.peek());//用于查看头元素,不弹出
        System.out.println(linkedList.peekFirst());//同上
        System.out.println(linkedList.peekLast());//查看尾元素,不弹出


        System.out.println("遍历方法");
        System.out.println("使用迭代器遍历");
        Iterator<Integer> iterator = linkedList.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
        System.out.println("使用链表迭代器遍历");
        ListIterator<Integer> integerListIterator = linkedList.listIterator();
        while (integerListIterator.hasNext()){
            System.out.println(integerListIterator.next());
        }

        System.out.println("for循环遍历 使用size(),get()");
        for (int i=0;i < linkedList.size();i++){
            System.out.println(linkedList.get(i));
        }
        System.out.println("foreach");
        for (Integer item: linkedList){
            System.out.println(item);
        }

Set

HashSet<String> hashSet = new HashSet<>();
        hashSet.add("abc");
        hashSet.add("def");
        System.out.println(hashSet.contains("abc"));//查看某一元素是否存在
        System.out.println(hashSet.isEmpty());//判空
        System.out.println(hashSet.size());//查看大小
        hashSet.remove("def");//删除元素
        System.out.println(hashSet.size());//查看大小


        System.out.println("遍历方式======");
        System.out.println("迭代器遍历");
        Iterator<String> iterator = hashSet.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
        System.out.println("for循环遍历");
        for (String item : hashSet){
            System.out.println(item);
        }

Map

        HashMap<Integer,String> hashMap = new HashMap<>();

        hashMap.put(1,"a");
        hashMap.put(2,"b");
        hashMap.put(3,"c");
        System.out.println(hashMap.get(1));//元素的访问
        hashMap.remove(1);
//        hashMap.clear();//清空
        hashMap.size();//计算大小
        System.out.println(hashMap.isEmpty());//判断集合是否为空
        System.out.println("遍历方法=======");
        System.out.println("迭代器遍历");
        Iterator<Map.Entry<Integer, String>> iterator = hashMap.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<Integer, String> next = iterator.next();
            System.out.println(next.getKey() + next.getValue());
        }

        System.out.println("Map.Entry +for循环遍历");
        Set<Map.Entry<Integer, String>> entries = hashMap.entrySet();
        for (Map.Entry<Integer,String> item : entries){
            System.out.println(item.getKey()+item.getValue());
        }
        System.out.println("for循环遍历");
        for (Integer item : hashMap.keySet()){
            System.out.println(item);
        }
        for (String item : hashMap.values()){
            System.out.println(item);
        }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值