Home  >  Article  >  Java  >  What are the methods to remove duplicates from java arrays?

What are the methods to remove duplicates from java arrays?

DDD
DDDOriginal
2023-12-22 16:26:171572browse

Java array deduplication methods include: 1. Using the Stream API of Java 8, you can use the "distinct()" method of the Stream API to remove duplicate elements in the array; 2. Use HashSet, which cannot contain A collection of repeated elements can remove repeated elements from an array; 3. Use TreeSet. TreeSet is an ordered set and cannot contain repeated elements. It should be noted that the use of TreeSet requires additional space to store elements, etc.

What are the methods to remove duplicates from java arrays?

# Operating system for this tutorial: Windows 10 system, Dell G3 computer.

In Java, there are many ways to remove duplicate elements from an array. The following are some common methods:

1. Using Java 8’s Stream API

Java 8 introduced the Stream API, which makes processing data more concise and flexible . You can use the distinct() method of the Stream API to remove duplicate elements from an array.

import java.util.Arrays;  
import java.util.stream.Collectors;  
  
public class Main {  
    public static void main(String[] args) {  
        int[] array = {1, 2, 3, 2, 1, 4, 5, 4};  
        int[] distinctArray = Arrays.stream(array).distinct().toArray();  
        System.out.println(Arrays.toString(distinctArray));  // 输出 [1, 2, 3, 4, 5]  
    }  
}

2. Use HashSet

HashSet is a set that cannot contain duplicate elements. You can use it to remove duplicate elements from an array. It should be noted that using this method will change the order of the original array.

import java.util.Arrays;  
import java.util.HashSet;  
import java.util.Set;  
  
public class Main {  
    public static void main(String[] args) {  
        int[] array = {1, 2, 3, 2, 1, 4, 5, 4};  
        int[] distinctArray = new int[new HashSet<>(Arrays.asList(array)).size()];  
        int i = 0;  
        for (int num : array) {  
            if (Arrays.binarySearch(distinctArray, num) < 0) {  
                distinctArray[i++] = num;  
            }  
        }  
        System.out.println(Arrays.toString(distinctArray));  // 输出 [1, 2, 3, 4, 5]  
    }  
}

3. Use TreeSet

TreeSet is an ordered set, it cannot contain duplicate elements. Compared with HashSet, using TreeSet will maintain the order of the original array. However, it should be noted that the use of TreeSet requires additional space to store elements, so it may be more expensive in memory usage than HashSet.

import java.util.Arrays;  
import java.util.TreeSet;  
import java.util.Set;  
  
public class Main {  
    public static void main(String[] args) {  
        int[] array = {1, 2, 3, 2, 1, 4, 5, 4};  
        TreeSet<Integer> set = new TreeSet<>();  
        for (int num : array) {  
            set.add(num);  
        }  
        int[] distinctArray = new int[set.size()];  
        int i = 0;  
        for (int num : set) {  
            distinctArray[i++] = num;  
        }  
        System.out.println(Arrays.toString(distinctArray));  // 输出 [1, 2, 3, 4, 5]  
    }  
}

The above is the detailed content of What are the methods to remove duplicates from java arrays?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:what is java compilerNext article:what is java compiler