Set set overview and features

characteristic:

        1. A collection that does not contain duplicate elements

        2. Without an indexed method, you cannot use a normal for loop to traverse

HashSet does not guarantee the iterative order of the set

Set<String> set = new HashSet<String>();
		set.add("java");
		set.add("hello");
		set.add("world");
		set.add("hello");
		System.out.println(set);
		Iterator <String>it2 = set.iterator();
		while(it2.hasNext()) {
			System.out.println(it2.next());
		}
		System.out.println("------enhance for loop------");
		for(String s : set) {
			System.out.println(s);
		}

Hash value: it is a value of int type calculated by JDK according to the address, string or number of the object  

Methods in Object class: public int hashCode(); Returns the hash code value of the Object

When the above methods are called by the same object, the hash code value is the same

By default, different objects have different hash values

Rewriting the hashCode method can make the hash values of different objects the same

Overview and characteristics of HashSet set

characteristic:

The underlying data structure is a hash table

There is no guarantee that the iterative order of the set is the same, and the order of storage and extraction is not guaranteed

There is no indexed method, so you cannot use a normal for loop

A collection that does not contain duplicate elements

Hash table of common data structures

Before JDK8, the bottom layer was implemented by array + linked list, which can be said to be an array with linked list elements

After JDK8, when the length is long, the bottom layer is optimized

LinkedHashSet set overview and features

Features: the Set interface implemented by hash table and linked list has predictable iteration order

The order of elements is guaranteed by the linked list

The uniqueness of elements is guaranteed by the hash table

Overview and characteristics of TreeSet set

Collection features:

Element order: the order here does not refer to the order of storage and retrieval, but is sorted according to certain rules. The specific sorting method depends on the construction method

        1. The parameterless construction method sorts the elements according to their natural order

        2. TreeSet (comparator): sort according to the specified comparator

There is no indexed method, so you can't use a normal for loop to traverse

There are no duplicate elements because it is a set set

If the generic type is Integer, the natural order is sorted from small to large

Use of natural sorting Comparable

1. Store and traverse the student object, create a TreeSet collection, and use the parameterless construction method

2. Requirements: sort by age from small to large. When the age is the same, sort by alphabetical order

class Student implements Comparable<Student>{            //Only by implementing the Comparable interface can we traverse
	String name;
	
	int age;
	public Student() {
		
	}
	public Student(String name , int age) {
		this.name = name;
		this.age = age;
	}
	@Override
	public int compareTo(Student o) {            //The ComparaTo method needs to be overridden
		// TODO Auto-generated method stub
		int num = this.age - o.age;
        
		int num1 = num == 0 ? this.name.compareTo(o.name):num;
		return num1;
	}
	
	
}











TreeSet<Student> stu1 = new TreeSet<Student>();

		stu1.add(new Student("xiaohong", 19));
		stu1.add(new Student("xiaogang", 20));
		stu1.add(new Student("mingming", 18));
		stu1.add(new Student("xiaoming" , 19));
		

		for (Student stu3 : stu1) {
			System.out.println(stu3.name + " " + stu3.age);
		}






The traversal result is
mingming 18
xiaohong 19
xiaoming 19
xiaogang 20

Summary:

The parameterless construction method of storing custom objects with TreeSet collection uses natural sorting to sort elements

Natural sorting is to let the class to which the element belongs implement the comparable interface and override the comparable method

Use of Comparator sorting Comparator

1. Store the student object and traverse it, create a TreeSet collection, and use the construction method with parameters

2. Requirements: sort by age from small to large. When the age is the same, sort by alphabetical order

TreeSet<Student>arr1 = new TreeSet<Student>(new Comparator<Student>() {

			@Override
			public int compare(Student o1, Student o2) {
				// TODO Auto-generated method stub
				int no = o1.age - o2.age;
				int no2 = no == 0 ? o1.name.compareTo(o2.name) : no;
			return no2;
			}
			
		
		
		});            //Create an object using an anonymous inner class, and override the methods of the interface while creating the object
		arr1.add(new Student("xiaohong", 19));
		arr1.add(new Student("xiaogang", 20));
		arr1.add(new Student("mingming", 18));
		arr1.add(new Student("xiaoming" , 19));
		for(Student st : arr1) {
			Student s1 = st;
			System.out.println(s1.name + " " + s1.age);
		}

The difference between natural sorting and comparator sorting is that natural sorting requires the student class to inherit the interface. Comparator sorting rewrites the methods of the interface while creating an anonymous internal class, and does not require the student class to inherit the interface

Note that the method names rewritten by the two methods are different. One is compare and the other is compareTo

Tags: Java Algorithm

Posted on Fri, 22 Oct 2021 22:59:03 -0400 by ikebaldo