Java self study - the difference between ArrayList and HashSet

Keywords: Java jvm

The difference between Java ArrayList and HashSet

Example 1: is there a sequence

ArrayList: ordered
HashSet: no order

The specific order of hashsets is neither in the order of insertion nor in the order of hashcode.

Here are some comments in the hasset source code

/**
 * It makes no guarantees as to the iteration order of the set;
 * in particular, it does not guarantee that the order will remain constant over time.
*/

It does not guarantee the iterative order of the Set; specifically, the order of elements may be different under different conditions

In other words, you can also insert 0-9 into the HashSet. In different versions of the JVM, the order you see is different. So when we develop, we can't rely on a hypothetical order, which is unstable

package collection;
   
import java.util.ArrayList;
import java.util.HashSet;
    
public class TestCollection {
    public static void main(String[] args) {
           
        ArrayList<Integer> numberList =new ArrayList<Integer>();
        //The data in the List is stored in the order of insertion
        System.out.println("----------List----------");
        System.out.println("towards List Insert 9 5 1 in");
        numberList.add(9);
        numberList.add(5);
        numberList.add(1);
        System.out.println("List Store data in order:");
        System.out.println(numberList);
        System.out.println("----------Set----------");
        HashSet<Integer> numberSet =new HashSet<Integer>();
        System.out.println("towards Set Insert 9 5 1 in");
        //Data in Set is not stored in insertion order
        numberSet.add(9);
        numberSet.add(5);
        numberSet.add(1);
        System.out.println("Set Not in order:");
        System.out.println(numberSet);
           
    }
}

Example 2: can it be repeated

The data in the List can be repeated
Data in Set cannot be duplicated
The criteria of repeated judgment are:
First, check whether hashcode is the same
If hashcode is different, it is considered different data
If hashcode is the same, then compare equals. If equals is the same, then it is the same data. Otherwise, it is different data

package collection;
   
import java.util.ArrayList;
import java.util.HashSet;
    
public class TestCollection {
    public static void main(String[] args) {
           
        ArrayList<Integer> numberList =new ArrayList<Integer>();
        //The data in the List can be repeated
        System.out.println("----------List----------");
        System.out.println("towards List Insert 9 9 in");
        numberList.add(9);
        numberList.add(9);
        System.out.println("List Two 9s in:");
        System.out.println(numberList);
        System.out.println("----------Set----------");
        HashSet<Integer> numberSet =new HashSet<Integer>();
        System.out.println("towards Set Insert 99");
        //Data in Set cannot be duplicate
        numberSet.add(9);
        numberSet.add(9);
        System.out.println("Set Only one 9 will remain in:");
        System.out.println(numberSet);
           
    }
}

Practice: Random number without repetition

Generate 50 random numbers between 0-9999. There must be no duplicate

Answer:

package collection;
 
import java.util.HashSet;
import java.util.Set;
 
public class TestCollection {
    public static void main(String[] args) {
        Set<Integer> numbers =new HashSet<>();
        while(numbers.size()<50){
            int i = (int) (Math.random()*10000);
            numbers.add(i);
        }
        System.out.println("50 non repetitive random numbers are obtained");
        System.out.println(numbers);
    }
}

Posted by Traveller29 on Sat, 28 Dec 2019 07:05:23 -0800