Java自学-集合(5)

2021/8/17 9:06:09

本文主要是介绍Java自学-集合(5),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Java自学-集合(5)

1、Set集合概述和特点

image-20210816152505259

image-20210816152654429

package lesson01;

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

/**
 * Author: Gu Jiakai
 * Date: 2021/8/16 15:26
 * FileName: SetDemo
 * Description:
 */
public class SetDemo {
    public static void main(String[] args) {
        Set<String> set=new HashSet<String>();

        set.add("hello");
        set.add("world");
        set.add("java");

        set.add("world");

        for(String s:set){
            System.out.println(s);
        }
    }
}

2、哈希值

image-20210816154010288

Student.java

package lesson01;

import java.util.Objects;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/16 15:31
 * FileName: Student
 * Description:
 */
public class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(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 boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return 0;
    }
}

HashDemo.java

package lesson01;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/16 15:30
 * FileName: HashDemo
 * Description:
 */
public class HashDemo {
    public static void main(String[] args) {
        Student s1=new Student("蔡伟",1);

        ///同一个对象多次调用hashCode()方法返回的哈希值是相同的。
        System.out.println(s1.hashCode());
        System.out.println(s1.hashCode());

        System.out.println("--------");
        //默认情况下,不同对象的哈希值是不相同的。
        //通过方法重写,可以实现不同对象的哈希值是相同的。
        Student s2=new Student("蔡伟",1);
        System.out.println(s2.hashCode());
        System.out.println(s2.hashCode());

        System.out.println("hello".hashCode());
        System.out.println("world".hashCode());
        System.out.println("java".hashCode());

        System.out.println("world".hashCode());
        System.out.println("--------");

        System.out.println("重地".hashCode());
        System.out.println("通话".hashCode());
    }
}

3、HashSet集合概述和特点

image-20210816154318409

package lesson01;

import java.util.HashSet;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/16 15:43
 * FileName: HashSetDemo01
 * Description:
 */
public class HashSetDemo01 {
    public static void main(String[] args) {
        HashSet<String> hs = new HashSet<>();

        hs.add("hello");
        hs.add("world");
        hs.add("java");

        hs.add("world");


        for(String s:hs){
            System.out.println(s);
        }
    }
}

4、HashSet集合保证元素唯一性源码分析

image-20210816155701245

5、常见数据结构之哈希表

image-20210816155811290

image-20210816160119688

6、HashSet集合存储学生对象并遍历

image-20210816161211611

Student.java

package lesson02;

import java.util.Objects;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/16 15:31
 * FileName: Student
 * Description:
 */
public class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(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 boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}

HashSetDemo02.java

package lesson02;

import java.util.HashSet;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/16 16:06
 * FileName: HashSetDemo02
 * Description:
 */
public class HashSetDemo02 {
    public static void main(String[] args) {
        HashSet<Student> hs=new HashSet<Student>();


        Student s1 = new Student("蔡伟1号", 1);
        Student s2 = new Student("蔡伟2号", 1);
        Student s3 = new Student("蔡伟3号", 1);

        Student s4 = new Student("蔡伟3号", 1);

        hs.add(s1);
        hs.add(s2);
        hs.add(s3);
        hs.add(s4);

        for(Student s:hs){
            System.out.println(s.getName()+","+s.getAge());
        }
    }
}

7、LinkedHashSet集合概述和特点

image-20210816161425079

package lesson02;

import java.util.LinkedHashSet;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/16 16:14
 * FileName: LinkedHashSetDemo
 * Description:
 */
public class LinkedHashSetDemo {
    public static void main(String[] args) {
        LinkedHashSet<String> strings = new LinkedHashSet<>();

        strings.add("hello");
        strings.add("world");
        strings.add("java");

        strings.add("world");

        for(String s:strings){
            System.out.println(s);
        }
    }
}

8、TreeSet集合概述和特点

image-20210816161912173

集合只能存储引用类型。

package lesson02;

import java.util.TreeSet;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/16 16:19
 * FileName: TreeSetDemo01
 * Description:
 */
public class TreeSetDemo01 {
    public static void main(String[] args) {
        TreeSet<Integer> ts = new TreeSet<>();

        ts.add(10);
        ts.add(40);
        ts.add(30);
        ts.add(50);
        ts.add(20);

        ts.add(30);
        
        for(Integer i :ts){
            System.out.println(i);
        }

    }
}

9、自然排序Comparable的使用

image-20210816164426600

Student.java

package lesson03;

import java.util.Objects;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/16 15:31
 * FileName: Student
 * Description:
 */
public class Student implements Comparable<Student>{
    private String name;
    private int age;

    public Student() {
    }

    public Student(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 int compareTo(Student s) {
//        return 0;
//        return 1;//升序
//        return -1;//降序
//        按照年龄从小到大排序。
        int num=this.age-s.age;
//        int num=s.age-this.age;
//        年龄相同时,按照姓名的字母顺序排序。
        int num2=num==0?this.name.compareTo(s.name):num;
        return num2;
    }
}

TreeSetDemo02.java

package lesson03;

import java.util.TreeSet;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/16 16:23
 * FileName: TreeSetDemo02
 * Description:
 */
public class TreeSetDemo02 {
    public static void main(String[] args) {
        TreeSet<Student> ts = new TreeSet<Student>();

        Student s1 = new Student("xishi",1);
        Student s2 = new Student("wangzhaojun",3);
        Student s3 = new Student("diaochan",2);
        Student s4 = new Student("yangyvhuan",4);


        Student s5 = new Student("caiwei",4);
        Student s6 = new Student("caiwei",4);

        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        ts.add(s4);
        ts.add(s5);

        for(Student s:ts){
            System.out.println(s.getName()+","+s.getAge());
        }
    }
}

10、比较器排序Comparator的使用

image-20210816165342786

Student.java

package lesson04;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/16 15:31
 * FileName: Student
 * Description:
 */
public class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(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;
    }

}

TreeSetDemo.java

package lesson04;

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

/**
 * Author: Gu Jiakai
 * Date: 2021/8/16 16:47
 * FileName: TreeSetDemo
 * Description:
 */
public class TreeSetDemo {
    public static void main(String[] args) {
        TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
               int num=s1.getAge()-s2.getAge();
               int num2=num==0?s1.getName().compareTo(s2.getName()):num;
               return num2;
            }
        });

        Student s1 = new Student("xishi",1);
        Student s2 = new Student("wangzhaojun",3);
        Student s3 = new Student("diaochan",2);
        Student s4 = new Student("yangyvhuan",4);


        Student s5 = new Student("caiwei",4);
        Student s6 = new Student("caiwei",4);

        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        ts.add(s4);
        ts.add(s5);

        for(Student s:ts){
            System.out.println(s.getName()+","+s.getAge());
        }
    }
}

11、成绩排序

image-20210816165910025

IDEA快捷键:Ctrl+alt+l

Student.java

package lesson05;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/16 16:59
 * FileName: Student
 * Description:
 */
public class Student {
    private String name;
    private int chinese;
    private int math;

    public Student() {
    }

    public Student(String name, int chinese, int math) {
        this.name = name;
        this.chinese = chinese;
        this.math = math;
    }

    public String getName() {
        return name;
    }

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

    public int getChinese() {
        return chinese;
    }

    public void setChinese(int chinese) {
        this.chinese = chinese;
    }

    public int getMath() {
        return math;
    }

    public void setMath(int math) {
        this.math = math;
    }

    public int getSum() {
        return this.getChinese() + this.getMath();
    }
}

TreeSetDemo.java

package lesson05;

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

/**
 * Author: Gu Jiakai
 * Date: 2021/8/16 16:59
 * FileName: TreeSetDemo
 * Description:
 */
public class TreeSetDemo {
    public static void main(String[] args) {
        TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                //主要条件
               int num=s2.getSum()-s1.getSum();
               //次要条件
               int num2=num==0?s1.getChinese()-s2.getChinese():num;
               int num3=num2==0?s1.getName().compareTo(s2.getName()):num2;
               return num3;
            }
        });

        Student s1= new Student("小火猴",95,20);
        Student s2 = new Student("布布种子",60,75);
        Student s3 = new Student("巴鲁斯",62,75);
        Student s4 = new Student("哈默",60,60);
        Student s5 = new Student("满分君",100,100);

        Student s6 = new Student("盖亚",100,100);

        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        ts.add(s4);
        ts.add(s5);
        ts.add(s6);
        for(Student s:ts){
            System.out.println(s.getName()+","+s.getChinese()+","+s.getMath()+","+s.getSum());
        }
    }
}

12、不重复的随机数

image-20210816171645591

package lesson05;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/16 17:17
 * FileName: SetDemo
 * Description:
 */
public class SetDemo {
    public static void main(String[] args) {
//        Set<Integer> set=new HashSet<Integer>();
        Set<Integer> set=new TreeSet<Integer>();

        Random random = new Random();
        while(set.size()<10){
            int i = random.nextInt(20) + 1;
            set.add(i);
        }

        for(Integer i:set){
            System.out.println(i);
        }
    }
}


这篇关于Java自学-集合(5)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程