一起Talk Android吧(第九十五回:Java中的类集之工具类二)

本文通过具体示例展示了Java集合工具类的使用方法,包括列表、链表、哈希集和树集的操作,如添加元素、查找、替换、排序等,并提供了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

各位看官们,大家好,上一回中咱们说的是Java中类集之工具类的例子,这一回咱们继续说该例子。闲话休提,言归正转。让我们一起Talk Android吧!

看官们,我们在上一章回中介绍了工具类及其提供的方法,不过都是偏向理论性的内容,而没有具体的实践,这显然不符合我们的风格。这一回中,我们将通过代码结合文本的方式来演示如何使用工具类中的功能,这样可以帮助大家进一步加深理解工具类。好了,直接上代码吧,如果看不懂代码了,直接看代码中的注释就能明白。

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class collentionsEx {
    private static List<Integer> list = new ArrayList<>();
    private static List<Integer> link = new LinkedList<>();
    private static Set<Integer> hashSet = new HashSet<>();
    private static Set<Integer> treeSet = new TreeSet<>();

    public static void main(String args[]) {

        //init the collections
        Collections.addAll(list,1,3,5,7,9);
        Collections.addAll(link,1,3,5,7,9);
        Collections.addAll(hashSet,1,3,5,7,9);
        Collections.addAll(treeSet,1,3,5,7,9);

        showCollection();

        //append some content in list, other collection can be used too.
        Collections.addAll(list, 3,4,5);
        System.out.println(" \nadd some new content in list.");
        showList(); 

        //find the content in collection,collection must be sorted, set can't be used
        int index = Collections.binarySearch(list,new Integer(3));
        if(index>=0){
            System.out.println("find 3 at: "+index);
        }else{
            System.out.println("can't find 3."+index);
        }

        //replace the content in collection, it is only used for list. 
        System.out.println(" \nreplace content 3 with 6 in list.");
        Collections.replaceAll(list, 3,6);
        showList();

        // swap the content of index a and b, it is only used for list. 
        Collections.swap(list,1, 2);
        System.out.println(" \nswap the content,which located at 1 and 2 in list.");
        showList();

        // sort the list, it is only used for list.
        Collections.sort(list);
        System.out.println(" \nsort the content in list.");
        showList();

        // revert the list, it is only used for list.
        System.out.println(" \nreverse content in list. set and map can't be reversed");
        Collections.reverse(list);
        Collections.reverse(link);
        showCollection();
    //  Collections.reverse(hashSet);
    //  Collections.reverse(treeSet);
    }

    public static void showCollection() {
        //show content of collection by Iterator
        Iterator<Integer> iter = list.iterator();
        System.out.print("List: ");
        while(iter.hasNext()) {
            System.out.print(iter.next()+" ");
        }
        System.out.println(" ");

        iter = link.iterator();
        System.out.print("Link: ");
        while(iter.hasNext()) {
            System.out.print(iter.next()+" ");
        }
        System.out.println(" ");

        iter = hashSet.iterator();
        System.out.print("HashSet: ");
        while(iter.hasNext()) {
            System.out.print(iter.next()+" ");
        }
        System.out.println(" ");

        iter = treeSet.iterator();
        System.out.print("TreeSet: ");
        while(iter.hasNext()) {
            System.out.print(iter.next()+" ");
        }
        System.out.println(" ");    
    }

    public static void showList() {
        //show content of list by Iterator
        Iterator<Integer> iter = list.iterator();
        System.out.print("List: ");
        while(iter.hasNext()) {
            System.out.print(iter.next()+" ");
        }
        System.out.println(" ");
    }
}

下面是程序的运行结果,请参考:

List: 1 3 5 7 9  
Link: 1 3 5 7 9  
HashSet: 1 3 5 7 9  
TreeSet: 1 3 5 7 9  

add some new content in list.
List: 1 3 5 7 9 3 4 5  
find 3 at: 1

replace content 3 with 6 in list.
List: 1 6 5 7 9 6 4 5  

swap the content,which located at 1 and 2 in list.
List: 1 5 6 7 9 6 4 5  

sort the content in list.
List: 1 4 5 5 6 6 7 9  

reverse content in list. set and map can't be reversed
List: 9 7 6 6 5 5 4 1  
Link: 9 7 5 3 1  
HashSet: 1 3 5 7 9  
TreeSet: 1 3 5 7 9  

各位看官,关于Java中类集之工具类的例子咱们就介绍到这里,欲知后面还有什么例子,且听下回分解!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

talk_8

真诚赞赏,手有余香

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值