LinkedHashSet_重复号码

问题描述

已知数组存放一批QQ号码,QQ号码最长为11位,最短为5位 String[] strs = {"12345","67891","12347809933","98765432102","67891","12347809933"}。 将该数组里面的所有qq号都存放在LinkedList中,将list中重复的元素删除,将list中所有元素分别用迭代器增强for循环打印出来。

参考思路

1 将数组中的所有qq号存放在LinkedList

import java.util.LinkedList;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        String[] strings = {"12345","67891","12347809933","98765432102","67891","12347809933"};

        List<String> list = new LinkedList<>();

        for (String s : strings) {
            list.add(s); 
        }

        System.out.println(list);
    }
}

把数组里的元素添加到一个集合类中,存在一种简洁的写法——使用Collections类中定义的addAll方法。如果查看源代码,会发现它其实也是去使用了foreach循环,因此可以理解上述写法的缩写形式。

    @SafeVarargs
    public static <T> boolean addAll(Collection<? super T> c, T... elements) {
        boolean result = false;
        for (T element : elements)
            result |= c.add(element);
        return result;
    }

通过addAll方法上述代码可以简化为:

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        // 已知数组存放了一批qq号
        String[] strings = {"12345","67891","12347809933","98765432102","67891","12347809933"};

        // 1 将数组中所有qq号都存放在LinkedList里面
        List<String> list = new LinkedList<>();

        Collections.addAll(list, strings);

        System.out.println(list);
        // => [12345, 67891, 12347809933, 98765432102, 67891, 12347809933]
    }
}

PS. 实际编码时可经常性地输出中间变量进行调试,而不是一定要写完代码才运行!

2 将list中重复的元素删除

存在多种思路(人工删除list中重复元素相对繁琐),这里采用一种更简洁的办法——通过LinkedHashSet清除list中的重复元素

import java.util.*;

public class Main {
    public static void main(String[] args) {
        String[] strings = {"12345","67891","12347809933","98765432102","67891","12347809933"};
        List<String> list = new LinkedList<>();
        Collections.addAll(list, strings);
//        System.out.println(list);
        // => [12345, 67891, 12347809933, 98765432102, 67891, 12347809933]
        Set<String> set = new LinkedHashSet<>(list);
        list.clear();
        list.addAll(set);
        System.out.println(list);
        // => [12345, 67891, 12347809933, 98765432102, 67891, 12347809933]
    }
}

3 分别用迭代器和增强for循环打印list中所有元素

import java.util.*;

public class Main {
    public static void main(String[] args) {
        String[] strings = {"12345","67891","12347809933","98765432102","67891","12347809933"};
        List<String> list = new LinkedList<>();
        Collections.addAll(list, strings);
//        System.out.println(list);
        // => [12345, 67891, 12347809933, 98765432102, 67891, 12347809933]
        Set<String> set = new LinkedHashSet<>(list);
        list.clear();
        list.addAll(set);
//        System.out.println(list);
        // => [12345, 67891, 12347809933, 98765432102]
        System.out.println("迭代器:");
        Iterator ite = list.iterator();
        while (ite.hasNext()) {
            System.out.print(ite.next() + " ");
        }

        System.out.println("\n增强for:");
        for (String s : list) {
            System.out.print(s + " ");
        }
    }
}

 

posted @ 2024-12-06 00:03  xkfx  阅读(100)  评论(0编辑  收藏  举报