JAVA------Set of Collection

Set inherits from Collection

Set features:

  1. Does not contain repeating elements collection
  2. There is no method with index, so you can't use ordinary for loop to traverse
  3. HashSet: No guarantee is made for the order of collection iteration

Example:

package Set;

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

public class SetDemo01 {
    
    
	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遍历
		for(String s:set) {
    
    
			System.out.println(s);
		}	
		System.out.println(set);	
	}
}


Hash table :

  • Before JDK8, the bottom layer was implemented with an array and a linked list
  • After JDK8, when the length is longer, the bottom layer is optimized
  • HashSet default construction method: create a new empty set, the default initial capacity is 16

When storing a hash table, first calculate a hash value as an index. For elements with the same index, first check whether the hash value is the same, and store the difference if the element is the same. If the element content is the same, store it if it is different. Indicates that it already exists and will no longer be deposited.

Insert picture description here


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

  • Public int hasCode() in the Object class: returns the hash value of the object

Hash value characteristics:

  1. The hash value returned by calling the hasCode() method multiple times for the same object is the same
  2. By default, different objects have different hash values. Through method rewriting, the hash values ​​of different objects can be the same.
  3. The string rewrites the hashCode method

Example:

Student category:

package Set;

public class student {
    
    
	private String name;
	private int age;
	
	//构造方法
	public student() {
    
    
		System.out.println("无参构造");
	}
	
	public student(String name,int age) {
    
    
		this.name=name;
		this.age=age;
	}
	
	//提供get/set方法
	public void setAge(int age) {
    
    
		if(age<0||age>100)
		{
    
    
			System.out.println("年龄有误");
		}else {
    
    
			this.age=age;
		}
		
	}
	
	public int getAge() {
    
    
		return age;
	}
	
	public void setName(String name) {
    
    
		this.name=name;
	}
	
	public String getName() {
    
    
		return name;
	}
	
	public void show() {
    
    
		System.out.println(name+","+age);
	}

}

main:

package Set;

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

public class SetDemo02 {
    
    
	public static void main(String[] args) {
    
    
		
		student s1=new student("小李",20);
		student s2=new student("小李",20);
		student s3=new student("小张",40);
		
		//同一个对象多次调用hasCode()方法返回的哈希值相同
		System.out.println(s1.hashCode());
		System.out.println(s1.hashCode());
		//默认情况下,不同对象哈希值不同
		//通过方法重写,可以实现不同对象的哈希值相同
		System.out.println(s2.hashCode());
		System.out.println("-------");
		
		System.out.println("hello".hashCode());
		System.out.println("world".hashCode());
		System.out.println("hello".hashCode());
		System.out.println("-------");
		
		//字符串重写了hashCode方法
		System.out.println("你好".hashCode());
		System.out.println("晚安".hashCode());
		
		
	}
}


HashSet features:

  • HashSet implements the Set interface
  • The underlying data structure is a hash table
  • No guarantee is made for the sequence of the collection iteration, and the order of the elements stored and retrieved is not guaranteed to be the same
  • There is no method with index, so you can't use ordinary for loop to traverse
  • Does not contain repeated elements

Example:

package Set;

import java.util.HashSet;

public class HashSetDemo {
    
    
	public static void main(String[] args) {
    
    
		HashSet<String> hs=new HashSet<>();
		
		hs.add("hello");
		hs.add("world");
		hs.add("java");
		
		for(String s:hs) {
    
    
			System.out.println(s);
		}
	}

}


LinkedHashSet features:

  • The Set interface implemented by the hash table and the linked list has a predictable iteration order
  • The order is guaranteed by the linked list, and the storage and retrieval order of the elements is consistent
  • The element is guaranteed to be unique by the hash table, and there are no duplicate elements

Guess you like

Origin blog.csdn.net/weixin_45102820/article/details/113446085