java——set series collection

Set series features

  • Unordered: inconsistent access order
  • No repetition: can remove repetition
  • No index: There is no method with an index, so you cannot use a normal for loop to traverse, nor can you get elements by index

Set collection implementation class characteristics

  • HashSet: unordered, non-repeated, no index
  • LinkedHashSet: ordered, not repeated, no index
  • TreeSet: The function of sorting, non-repeating, and index-free
    Set collection is basically the same as Collection's API
public class SerDemo {
    
    
    public static void main(String[] args) {
    
    
        //HashSet LinkeHashSet TreeSet
        Set<String> sets=new HashSet<>();//经典[Java, MySQL, HTML, SpringBoot]
        //Set<String> sets=new LinkedHashSet<>();//有序 不重复 无索引[MySQL, Java, HTML, SpringBoot]
        sets.add("MySQL");
        sets.add("MySQL");
        sets.add("Java");
        sets.add("Java");
        sets.add("HTML");
        sets.add("HTML");
        sets.add("SpringBoot");
        sets.add("SpringBoot");
        System.out.println(sets);//[Java, MySQL, HTML, SpringBoot]



    }
}


The bottom layer of the HashSet collection adopts the data stored in the hash table . Before JDK8, the bottom layer was composed of array + linked list. After JDK1.8, the bottom layer was
composed of array + linked list + red and black numbers.
The int type data calculated by the rules
HashSet version array + linked list + (combined with hash algorithm)

HashSet principle analysis starting from version 1.8

  • Underlying structure: Underlying structure: hash table (combination of array, linked list, red-black tree)
  • When there is too much data hanging under the element, the query performance will be reduced. Starting from JDK8, when the length of the linked list exceeds 8, it will automatically convert to the default
    insert image description here
    rule of the red-black tree TreeSet collection
  • For array types: Interger, Double, official someone sorts in ascending order by size
  • For custom types such as Student objects,
    when TreeSet cannot directly sort TreeSet collection storage objects, there are two ways to design custom comparison rules.
    Method 1
  • Let custom classes (such as student classes) implement the Comparable interface to rewrite the compareTo method in it to formulate comparison rules
public int coparaTo(Apple o){
    
    
	//按照重量进行比较
	return this.weight-o.weight;//去除重量重复的元素
	return this.weight-o.weight>=0?1:-1;//保留重量重复的元素
}

way two

  • The TreeSet collection has a parameter constructor, and you can design a comparator object corresponding to the Comparator interface to customize the comparison rules.
Set<Apple> apples=new TreeSet<>(new Comparator<Apple>()){
    
    
	@override
	public int compare(Apple o1,Apple o2){
    
    
		return o1.getWeight()-o2.getWeight();
		//注意:浮点型建议直接使用Double.compare进行比较
		return Double.compare(o2.getPrice(),o1.getPrice());
	}
}

In the two ways, the rules about the return value:

  • If it is considered that the first element is greater than the second element, return a positive integer.
  • If it is considered that the first element is smaller than the second element, return a negative integer.
  • If you think that the first element is equal to the second element and put it back to 0, then the Treeset collection will only retain one element, and the two are considered to be duplicates.
    Note: If the objects stored in the TreeSet collection have more expensive responsibilities in advance, the collection also has its own comparator. By default, the comparator that comes with the collection is used for sorting

variable parameter

  • Variable parameters are used in formal parameters and can accept multiple data
    -variable parameter formats: data type...parameter name
    The role of variable parameters
  • Transferring parameters is very flexible and convenient. No parameters can be transmitted, one or more parameters can be transmitted, or an array can be transmitted
  • The variable parameter is essentially an array inside the method.
    Notes on variable parameters
  • 1. There can only be one variable parameter in a formal parameter list
  • 2. Variable parameters must be placed at the end of the formal parameter list
    Case
    If you need to define a method to sum, this method can flexibly fulfill the following requirements:
  • Calculate the sum of 1 data
  • Calculate the sum of 2 data
  • Calculate the sum of 3 data
  • Calculate the sum of n data, and even support calling without parameters
public class SetDemo2 {
    
    
    /**
     * 注意:一个形参列表中只能有一个可变参数,可变擦拭农户必须防砸形参列表的后面
     * public static void sum(int...nums,int...nums1)
     * 会报错,因为传参的时候不知道到底传参的时候给哪个参数
     * 但是可以
     * public static void sum(int age,int...nums)
     * 这样就可以把第一个数据传给第一个参数
     */

    public static void main(String[] args) {
    
    
        sum();//1.不传参数
        sum(10);//2.可以传输一个参数
        sum(10,20,30);//3.可以传输多个参数
        sum(new int[]{
    
    10,20,30,40,50});//4.可以传输一个数组
    }
    public static void sum(int...nums){
    
    
        //注意:可变参数在方法内部其实就是一个数组。nums
        int sum=0;
        System.out.println("元素个数:"+nums.length);
        System.out.println("元素内容:"+ Arrays.toString(nums));
        for(int i=0;i<nums.length;i++){
    
    
            sum+=nums[i];
        }
        System.out.println(sum);
    }
}

Guess you like

Origin blog.csdn.net/weixin_46362658/article/details/123232368