Set set framework

Set set framework

1: Set characteristics

1: The set set is not repeated

public class dome {
	public static void main(String[] args) {
		/**
		 * set Set characteristics
		 * 1:----set The collection is not repeated
		 */
		//New set set
		Set<String> s=new HashSet<String>();
		s.add("a");
		s.add("b");
		s.add("c");
		s.add("a");
		System.out.println(s);
	}
}

The output results are as follows:

It can be seen from the output result that [a,b,c,a] was input successively before

But only [a,b,c] can conclude that the set set is not repeated.

2: The set set is unordered

public class dome1 {
	public static void main(String[] args) {
		/**
		 * set Set characteristics
		 * 2:----set The collection is out of order
		 */
		//New set set
		Set<String> s=new HashSet<String>();
		s.add("d");
		s.add("b");
		s.add("f");
		s.add("r");
		System.out.println(s);
	}

}

The output results are as follows:

The result of sequential input is [D, B, F, R], but the result is [b, R, D, F], so the set set is unordered

2: Traversal mode of set set

The traversal mode of the set set is a be fore loop and an iterator

public class dome1 {
	public static void main(String[] args) {
		/**
		 * set Set characteristics
		 * 2:----set The collection is out of order
		 */
		//New set set
		Set<String> s=new HashSet<String>();
		s.add("d");
		s.add("b");
		s.add("a");
		s.add("c");
		
		//Traversal mode of set set
		
		//1: Before loop
		for (String string : s) {
			System.out.println(string);
		}
		//2: Iterator
		Iterator<String> it=s.iterator();
		while(it.hasNext()) {
			System.out.println(it.next());
		}
	}

}

3: HashSet hash table storage Duplicate element storage

set sets are de duplicated in different cases

The first one above is the de repeated call to the value of string type, which is the equals() method

Also, call hashcode() and equals() respectively

public class dome2 {
	public static void main(String[] args) {
		Set<Object> s=new HashSet<Object>();
		s.add(new student("xxx", 18));
		s.add(new student("yyy", 18));
		s.add(new student("zzz", 18));
		s.add(new student("xxx", 18));
		for (Object object : s) {
			System.out.println(object);
		}
	}
}


	//Create a new student class
	public class student {
	private String name;
	private int 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;
	}
	public student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public student() {
		// TODO Auto-generated constructor stub
	}
	@Override
	public int hashCode() {
		System.out.println("hashCode Method called");
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		System.out.println("equals Method called");
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		student other = (student) obj;
		if (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	@Override
	public String toString() {
		return "student [name=" + name + ", age=" + age + "]";
	}
	
	
}

As shown in the figure, each time the data added to the set is new student("zzz", 18); Is to create a new student. No matter whether their values are the same or not, but their addresses are different, they are not the same person

What is as like as two peas: suppose that two people with the same length are of the same name, same appearance, same age and everything but they are not the same person.

When the addresses returned by hashcode() are different, the first four output results show that the hashcode() method is called, indicating that the hashcode() method is used to judge whether there is a similar address

        set.add(new student("xxx", 18));
        set.add(new student("yyy", 18));
        set.add(new student("zzz", 18));
        set.add(new student("xxx", 18));

Because the equals() method is overridden, the fourth value is the same, so the output is only

student [name=xxx, age=18]
student [name=zzz, age=18]
student [name=yyy, age=18]

 

The value entered is

        set.add(new student("xxx", 18));
        set.add(new student("yyy", 18));
        set.add(new student("zzz", 18));
        set.add(new student("xxx", 18)); 

When the hashcode() method has the same address

The first result of the output is that the hashcode() method is called -- because there is no value in the set set, this is the first address

The second result of the output is that the hashcode() method is called -- because an address is created, but if the address is the same, then

The third result of the output is that the equals() method is called. Calling equals() is to judge the previous value to see whether the values are the same

The fourth result of the output is that the hashcode() method is called -- because a new address is created, but if the address is the same, then

The fifth and sixth output results are that the equals() method is called. Calling equals() is to judge the previous value to see whether the values are the same

The seventh result of the output is that the hashcode() method is called -- because an address is created, but if the address is the same, then

The eighth result of the output is that the equals() method is called. Calling equals() is to judge the previous value to see whether the value is the same. Because there is only one same value, the judgment ends once and the duplicate value is deleted

student [name=xxx, age=18]
student [name=zzz, age=18]
student [name=yyy, age=18]

III: TreeSet (NATURAL sorting, comparator sorting)

1: Natural sorting (NATURAL sorting means that each module is sorted according to that sort)

//Test class
public class dome3 {
	public static void main(String[] args) {
		Set<user> set=new TreeSet<>();
		set.add(new user("xxx", 20000));
		set.add(new user("yyy", 10000));
		set.add(new user("zzz", 25000));
		for (user user : set) {
			System.out.println(user);
		}
	}

}


//Entity class
public class user {
	private String name;
	private int money;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getMoney() {
		return money;
	}
	public void setMoney(int money) {
		this.money = money;
	}
	@Override
	public String toString() {
		return "user [name=" + name + ", money=" + money + "]";
	}
	public user(String name, int money) {
		super();
		this.name = name;
		this.money = money;
	}
	public user() {
		// TODO Auto-generated constructor stub
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + money;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		user other = (user) obj;
		if (money != other.money)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

}

The following error will be reported when running directly, because Java is not implemented Lang. comparable package

Comparable needs to be implemented

And rewrite the method

Ascending by money

According to money descending order

2: comparator sorting (sorting method can be selected independently)

Sort by name

public class dome5 {
	public static void main(String[] args) {
		Set<user> set=new TreeSet<>(new Comparator<user>() {
			@Override
			public int compare(user o1, user o2) {
				// TODO Auto-generated method stub
				return o1.getName().compareTo(o2.getName());
			}
			
		});
		set.add(new user("xxx", 20000));
		set.add(new user("yyy", 10000));
		set.add(new user("zzz", 25000));
		for (user user : set) {
			System.out.println(user);
		}
	
	}
}

Sort by money

public class dome4 {
	public static void main(String[] args) {
		Set<user> set=new TreeSet<>(new Comparator<user>() {
			@Override
			public int compare(user o1, user o2) {
				// TODO Auto-generated method stub
				return o1.getMoney() - o2.getMoney();
			}
			
		});
		set.add(new user("xxx", 20000));
		set.add(new user("yyy", 10000));
		set.add(new user("zzz", 25000));
		for (user user : set) {
			System.out.println(user);
		}
	}

}

That's all for today. Still work hard!!!!

Keywords: set

Added by whansen02 on Fri, 14 Jan 2022 20:50:54 +0200