LinkedHashSet collection and variadic parameters

LinkedHashSet collection




We know that HashSet guarantees that the elements are unique, but the elements are stored in no order, so we want to ensure the order, what should we do?

There is a subclass java.util.LinkedHashSet under HashSet, which is a data storage structure combining a linked list and a hash table.

java.utiL.LinkedHashset collection extends Hashset collection LinkedHashset collection Features:

The bottom layer is a hash table (array + linked list/red-black tree) + linked list: an additional linked list (recording the storage order of elements) to ensure that the elements are in order

 public static void main(String[] args) {
        HashSet<String> set = new HashSet<>();
        set.add( "www" );
        set.add( "abc" );
        set.add( "abc" );
        set.add( "itcast" );
        System.out.println(set);//disorder,Duplicates are not allowed
        LinkedHashSet<String> linked = new LinkedHashSet<>();
        linked.add( "www" );
        linked.add( "itcast" );
        System.out.println(linked);//orderly,Duplicates are not allowed
public class LinkedHashT {
    public static void main(String[] args) {
        //Create LinkedHashSet gather:
        LinkedHashSet<Integer> lhs = new LinkedHashSet<>();
        System.out.println(lhs.add(9));//false This 9 is not put into the set
        System.out.println(lhs.size());//4 unique, orderly
        System.out.println(lhs); //[9, 5, 8, 6]


variable parameter


Variable arguments allow you to specify methods that can take multiple arguments of the same type without having to determine the number of arguments in advance.

After JDK1.5, if we define a method that needs to accept multiple parameters, and the types of multiple parameters are the same, we can simplify it


modifier return value type method name(parameter type...parameter name){}

In fact, this writing is completely equivalent to

modifier return value type method name (parameter type gate] formal parameter name) {}

It's just the latter definition that an array must be passed when calling, while the former can pass data directly.

After JDK1.5. Simplified operations have occurred. ...used on parameters, called variadic parameters.
  It also represents an array, but when calling this method with variable parameters, there is no need to create an array (this is the simple point), and the elements in the array are directly passed as actual parameters. In fact, the compiled class file, these Elements are first encapsulated into an array and then passed. These actions are all done in vain when compiling the .class file


Variable parameters: a new feature after JDK1.5

Prerequisites for use:

When the data type of the parameter list of the method has been determined, but the number of parameters is uncertain, you can use variable parameters.

Usage format: used when defining a method

modifier return value type method name(data type...variable name){}

The principle of variable parameters:

The bottom layer of variable parameters is an array. According to the number of passed parameters, arrays of different lengths will be created to store these parameters.

The number of parameters passed, can be o (not passed), 1, 2... more

public static void main(String[] args) {
            Define a method for computing the sum of (0-n) integers
            Known: Calculate the sum of integers, the data type has been determined int
            However, the number of parameters is uncertain. If you don't know how to calculate the sum of several integers, you can use variable parameters.
            public static int add(int. . .arr){
            //Define a method to calculate the sum of three integers of type int
        int i =  add(12,12);
    public static  int add(int...arr){
//        System.out.println(arr);
//        System.out.println(arr.length);
        int num = 0;
        for (int i :arr){
        return num;

//        public static int add(int a,int b,int c){
//            return a+b+c;
//           }
//        //define a method,calculate two int sum of type integers
//        public static int add (int a,int b) {


package com.zhong.method;

public class Demo04{     
       public static void main(String  args[]) {
            //Calling methods with variadic arguments
            printMax(new double[]{1,2,3});
    public static void printMax(double... numbers){  
    //double...  It is to declare the method parameter type.  numbers is a variable parameter                      
        if(numbers.length==0) {   //The length of the variable parameter is equal to 0
           System.out.println("No argument passed");
       double result = numbers[0];  //return value result;numbers[0]is an array.
       for (int i = 1;i < numbers,length; i++){
          if (numbers[i]>result){
              result = numbers[i];
        System.out.println("The max value is" + result);


package com.zhong.method;

  public class Demo04 {     
       public static void main(String [] args){
           Demo04 demo04 = new Demo04(); //Create a new class to return the current object
           demo04.tset(x:1,...i:1,2,3,4,5,90); //This is the value
          public void test(int x,int... i){//variable parameter i
            Scanner scanner = new Scanner(;


Posted by tkmk on Wed, 06 Jul 2022 15:47:40 +0930