set 无序,集合的浅理解,add方法,流程可视化解读

set add方法流程图

Set:不包含重复元素,但是无序,
一个不包含重复元素(值)的 collection,
set在每次添加元素(值)的时候,都会把前面的元素和新增的元素进行比较,如果是true,重复,丢弃;false:添加到集合当中。
Set集合当中是没有重复数据。

/**
Set:不包含重复元素,但是无序,
一个不包含重复元素(值)的 collection,
*/
import java.util.Set;
import java.util.HashSet;

public class Demo{
    public static void main(String [] args){
        //创建一个String的set集合
        Set<String> set = new HashSet<String>();
    // add String
        set.add("xiaoming");  
        set.add("xiaohua");  
        set.add("xiaogang");    
        set.add("xiaohua");
        //找到他们的hash码
        System.out.println("xiaoming".hashCode());
        System.out.println("xiaohua".hashCode());
        System.out.println("xiaogang".hashCode());
        System.out.println("xiaohua".hashCode());
        //输出set集合,发现重复的没有添加进去
        System.out.println(set);
    }


}
set在每次添加元素(值)的时候,都会把前面的元素和新增的元素进行比较,如果是true,重复,丢弃;false:添加到集合当中。
import java.util.*;
public class Demo2{
    public static void main(String [] agrs ){
            Set<Student> set = new HashSet<Student>();
            //add  Student
            Student s = new Student("xiaogang",18);
            Student s1 = new Student("xiaohua",19);
            Student s2 = new Student("xiaodu",18);
            Student s3 = new Student("xiaohua",19);

            set.add(s); // 1已经有值
            set.add(s1);// 先判断1是否有值,有再调用equals方法   返回true 丢弃  返回false  添加
            set.add(s2);
            set.add(s3);

    //set  删除方法     set.remove(s);
            //遍历set集合
            for(Student ss: set){
                        System.out.println(ss);
                }
        }
}
//创建学生对象
class Student{
        private String name;
        private Integer age;
        public Student(String name,Integer age){
                this.name = name;
                this.age = age;
        }
        // 获取你的哈希值,,,,
     public int hashCode() {
       //名字相同
       return name.hashCode();
     }
    //重写父类的  hashCode方法, equals方法
    //Object anObject 表示新加入的对象    this 表示要和他比较的对象
        public boolean equals(Object anObject) {
       //把对象强制转换为Student
        Student s = (Student)anObject;
       //比较两个对象 是不是同一个对象
       if(this == s){
                return true;
        }
       //判断是不是同一个类型
       if(!(anObject instanceof Student)){
            return false;
        }
        //判断两个的年龄是否相等
        if(this.age == s.age){
             return true;//true 表示重复
            }
       return false;
    }
    }
//treeset 双叉树 自然排序理解            
import java.util.*;

public class DemoTreeSet{
        public static void main(String [] args){

            Set <Student>set = new TreeSet<Student>();

            set.add(new Student("xiaohua",18));
            set.add(new Student("xdfgdf",17));
            set.add(new Student("fhfd",19));
            set.add(new Student("sdfger",11));          
            for(Student s : set){
                    System.out.println(s);
                }           
        }
}
//自定义排序规则
class MyComparator implements  Comparator{

        public int compare(Object o1,Object o2){

            return 1;
        }
}


//实现Comparable类,重写compareTo 方法,然后进行排序 

class Student implements Comparable{
        String name;
        Integer age;

        public Student(String name,Integer age){
            this.name = name;
            this.age = age;
            }

        public String toString(){

                return "name = "+ name +";age = "+age;
        }
        //重写compareTo 方法
        public int compareTo(Object o){
              //把后面对象的值强制转换,为了比较
                Student s = (Student)o;
                //把年龄 再一次进行排序
                return this.age.compareTo(s.age);
        }

    }

猜你喜欢

转载自blog.csdn.net/qq_41441312/article/details/78842888