栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java-集合框架3

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java-集合框架3

Set子接口
  • 特点:无序,无下标,元素不可重复。
  • 方法:全部继承自callection中的方法。
Set实现类
  • HashSet【重点】:
    • 基于HashCode实现元素不重复。
    • 当存入元素的哈希码相同时,会调用equals进行确认,如果结果为true,则拒绝后者存入。

  • TreeSet:
    • 基于排列顺序实现元素不重复。
    • 实现了SortedSet接口,对集合元素自动排序。
    • 元素对象的类型必须实现Comparable接口,指定排序规则。
    • 通过CompareTo方法确定是否为重复元素。

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;


public class Demo01 {
    public static void main(String[] args) {
        //创建对象
        Set hashSet = new HashSet<>();

        //添加数据
        hashSet.add("张三");
        hashSet.add("李四");
        hashSet.add("王五");
        hashSet.add("赵六");
        System.out.println("元素个数:"+hashSet.size());
        System.out.println(hashSet.toString());

        //删除数据
//        hashSet.remove("张三");
//        hashSet.clear();
//        System.out.println(hashSet.size());
//        System.out.println(hashSet.toString());

        //遍历数据【重点】
        System.out.println("———————————————增强For—————————————————————");
        for (String s : hashSet) {
            System.out.print(s+" ");
        }
        System.out.println("n———————————————迭代器—————————————————————————————");
        Iterator iterator = hashSet.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }

        //判断
        System.out.println(hashSet.contains("张三"));
        System.out.println(hashSet.isEmpty());


    }
}
hashset
import java.util.HashSet;
import java.util.Iterator;


public class Demo02 {
    public static void main(String[] args) {
        //创建集合
        HashSet hashSet = new HashSet<>();
        //添加元素
        hashSet.add("张学友");
        hashSet.add("刘德华");
        hashSet.add("郭富城");
        hashSet.add("黎明");
        System.out.println("元素个数:"+hashSet.size());
        System.out.println(hashSet.toString());
        hashSet.remove("黎明");
        System.out.println("元素个数:"+hashSet.size());

        //遍历
        System.out.println("———————————————————————增强for—————————————————————————————");
        for (String s : hashSet) {
            System.out.print(s+" ");
        }
        System.out.println("n———————————————————————迭代器—————————————————————————————");
        Iterator iterator = hashSet.iterator();
        while (iterator.hasNext()){
            System.out.print(iterator.next()+" ");
        }
        System.out.println();

        //判断
        System.out.println(hashSet.contains("郭富城"));
        System.out.println(hashSet.isEmpty());


    }
}
import java.util.HashSet;
import java.util.Iterator;


public class Demo03 {
    public static void main(String[] args) {
        //创建对象
        HashSet hashSet = new HashSet<>();
        Person P1 = new Person("张学友", 20);
        Person P2 = new Person("刘德华", 19);
        Person P3 = new Person("郭富城", 18);
        Person P4 = new Person("黎明", 17);
        //添加元素
        hashSet.add(P1);
        hashSet.add(P2);
        hashSet.add(P3);
        hashSet.add(P4);
        hashSet.add(new Person("黎明",17));
        System.out.println("元素个数:"+hashSet.size());
        System.out.println(hashSet.toString());

        //删除元素
//        hashSet.remove(P1);
//        hashSet.clear();
//        System.out.println("元素个数:"+hashSet.size());
//        System.out.println(hashSet.toString());
        //遍历
        System.out.println("—————————————————增强for———————————————");
        for (Person person : hashSet) {
            System.out.println(person.toString());
        }

        System.out.println("—————————————————迭代器———————————————");
        Iterator iterator = hashSet.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }

        //判断
        System.out.println(hashSet.contains(new Person("黎明",17)));
        System.out.println(hashSet.isEmpty());

    }
}
import java.util.Objects;

//Person类
public class Person {
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + ''' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null) return false;
        if (o instanceof Person) {
            Person person = (Person) o;
            return age == person.age && Objects.equals(name, person.name);
        }
        return false;
    }

    @Override
    public int hashCode() {
        return this.name.hashCode()+this.age;
    }
}
TreeSet

普通:

import java.util.Iterator;
import java.util.TreeSet;


public class Demo04 {
    public static void main(String[] args) {
        //创建对象
        TreeSet treeSet = new TreeSet<>();

        //添加元素
        treeSet.add("张学友");
        treeSet.add("刘德华");
        treeSet.add("郭富城");
        treeSet.add("黎明");
        System.out.println("元素个数:"+treeSet.size());
        System.out.println(treeSet.toString());
        //删除元素
//        treeSet.remove("张学友");
//        treeSet.clear();
//        System.out.println("元素个数:"+treeSet.size());
//        System.out.println(treeSet.toString());

        //遍历
        System.out.println("——————————————————增强for————————————————————————————————");
        for (String s : treeSet) {
            System.out.println(s);
        }
        System.out.println("——————————————————迭代器————————————————————————————————");
        Iterator iterator = treeSet.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
        //判断
        System.out.println(treeSet.contains("黎明"));
        System.out.println(treeSet.isEmpty());
    }
}

进阶:

import java.util.Iterator;
import java.util.TreeSet;


public class Demo05 {
    public static void main(String[] args) {
        //创建对象
        TreeSet treeSet = new TreeSet<>();
        Person p1 = new Person("张学友", 20);
        Person p2 = new Person("刘德华", 19);
        Person p3 = new Person("郭富城", 18);
        Person p4 = new Person("黎明", 17);
        //添加元素
        treeSet.add(p1);//类需要先实现CompareTo接口才能添加到TreeSet中
        treeSet.add(p2);
        treeSet.add(p3);
        treeSet.add(p4);
        System.out.println(treeSet.size());
        System.out.println(treeSet.toString());

        //删除元素
//        treeSet.remove(p1);
//        treeSet.clear();
//        System.out.println(treeSet.size());

        //遍历元素
        System.out.println("——————————————————增强for——————————————————————————");
        for (Person person : treeSet) {
            System.out.print(person+"t");
        }
        System.out.println("n——————————————————迭代器——————————————————————————————————");
        Iterator iterator = treeSet.iterator();
        while(iterator.hasNext()){
            System.out.print(iterator.next()+"t");
        }
        System.out.println();

        //判断
        System.out.println(treeSet.contains(p1));
        System.out.println(treeSet.contains(new Person("张学友",20)));
        System.out.println(treeSet.isEmpty());
    }
}

高级:

import java.util.Comparator;
import java.util.TreeSet;


public class Demo06 {
    public static void main(String[] args){
        //创建集合并指定比较规则。
        TreeSet treeSet = new TreeSet<>(new Comparator() {
            @Override
            public int compare(Person o1, Person o2) {
                int n1 = o1.getAge()- o2.getAge();
                int n2 = o1.getName().compareTo(o2.getName());
                return n1==0?n2:n1;
            }
        });
        Person p1 = new Person("张学友", 20);
        Person p2 = new Person("刘德华", 19);
        Person p3 = new Person("郭富城", 18);
        Person p4 = new Person("黎明", 18);
        //添加元素
        treeSet.add(p1);
        treeSet.add(p2);
        treeSet.add(p3);
        treeSet.add(p4);
        System.out.println(treeSet.size());
        System.out.println(treeSet.toString());
    }
}

案例:

import java.util.Comparator;
import java.util.TreeSet;


public class Demo07 {
    public static void main(String[] args) {
        TreeSet strings = new TreeSet<>(new Comparator() {
            @Override
            public int compare(String o1, String o2) {
                int n1 =o1.length()-o2.length();
                int n2=o1.compareTo(o2);
                return n1==0?n2:n1;
            }
        });
        strings.add("helloword");
        strings.add("zhang");
        strings.add("lisi");
        strings.add("wangwu");
        strings.add("beijing");
        strings.add("xian");
        strings.add("nanjing");
        strings.add("1");
        strings.add("22");
        strings.add("333");
        strings.add("4444");
        System.out.println(strings.size());
        System.out.println(strings.toString());
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/446148.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号