• 廖雪峰Java5集合-4Set-1使用Set


    集合

    Set用于存储不重复的元素集合:

    • boolean add(E e)
    • boolean remove(Object o)
    • boolean contains(Object o)
    • int size()
    public class Main {
        public static void main(String[] args) throws IOException {
            Set<String> aset = new HashSet<>();
            System.out.println("1"+aset.add("tom"));
            System.out.println("2"+aset.add("tom"));//重复添加失败,set不能存储重复元素
            System.out.println("3"+aset.add("jerry"));
    
            System.out.println("集合长度"+aset.size());
    
            System.out.println("4"+aset.remove("jerry"));
            System.out.println("5"+aset.remove("kitty"));
            System.out.println("6"+aset.contains("tom"));
            System.out.println("7"+aset.contains("jerry"));
        }
    }
    
    ## Set不保证有序: * HashSet是无序的 * Tree Set是有序的 * 实现了SortedSet接口的是有序Set ```#java public class Main { public static void main(String[] args) throws IOException { //HashSet不能保证有序 Set aset = new HashSet<>(); aset.add("apple"); aset.add("pear"); aset.add("orange"); for(Object o:aset){ System.out.println(o.toString()); } } } ``` ```#java public class Main { public static void main(String[] args) throws IOException { Set aset = new TreeSet<>(); //TreeSet按元素顺序排序 aset.add("apple"); aset.add("pear"); aset.add("orange"); for(Object o:aset){ System.out.println(o.toString()); } } } ``` TreeSet可以自定义排序算法

    示例:利用set去除List中的重复元素

    public class Main {
        public static void main(String[] args) throws IOException {
            List<String> list1 = Arrays.asList("pear","apple","banana","orange","apple","banana");
            System.out.println(removeDuplicate(list1));
        }
        static List<String> removeDuplicate(List<String> list){
            Set<String> set = new HashSet<>(list);
            return new ArrayList<>(set);
        }
    }
    
    替换为TreeSet,对元素进行排序 ```#java public class Main { public static void main(String[] args) throws IOException { List list1 = Arrays.asList("pear","apple","banana","orange","apple","banana"); System.out.println(removeDuplicate(list1)); } static List removeDuplicate(List list){ Set set = new TreeSet<>(list); return new ArrayList<>(set); } } ``` 自定义排序算法 ```#java public class Main { public static void main(String[] args) throws IOException { List list1 = Arrays.asList("pear","apple","banana","orange","apple","banana"); System.out.println(removeDuplicate(list1)); } static List removeDuplicate(List list){ Set set = new TreeSet<>(new Comparator() { ////自定义排序算法 @Override public int compare(String o1, String o2) { //反序 return -o1.compareTo(o2); } }); set.addAll(list); return new ArrayList<>(set); } } ```

    总结

    • Set用于存储不重复的元素集合
    • 放入Set的元素与作为Map的Key要求相同:正确实现equals()和hashCode()
    • 利用Set可以去除重复元素
    • 遍历SortedSet按照元素的排序顺序遍历,也可以自定义排序算法
  • 相关阅读:
    Python之paramiko基础
    mysql-创建库之问题
    Jmeter--HTTP Cookie管理器
    Mysql-简单安装
    [JS]jQuery,javascript获得网页的高度和宽度
    去除表单自动填充时,-webkit浏览器默认给文本框加的黄色背景
    读罢泪两行,人生成长必须面对的10个残酷事实
    前端开发面试题JS2
    前端开发面试题JS
    是内耗让你活得特别累
  • 原文地址:https://www.cnblogs.com/csj2018/p/10548183.html
Copyright © 2020-2023  润新知