首頁 > Java > java教程 > 在Java中從ArrayList取得唯一值

在Java中從ArrayList取得唯一值

WBOY
發布: 2023-09-04 08:41:07
轉載
794 人瀏覽過

在Java中從ArrayList取得唯一值

ArrayList is a class of Java Collection Framework that implements List Interface. It is a linear structure that stores and accesses each element sequentially. It allows the storage of duplicate elements however, there are a few approaches that may help to get unique values from an ArrayList. In this article, we are going to see the practical implementation 透過Java範例程式的方式之一。

Java Program to get Unique Values from ArrayList

Before jumping to the solution program for the given problem, let’s discuss the following Collection Interface的概念−

HashSet

它是Java Collection介面的一個類,實作了set介面。它與之相似 到數學集合中。其中,所有的元素都以排序的方式存儲,以減少 the retrieval time and also, it doesn’t allow duplicate values. This is the reason for using HashSet to filter duplicates and to get unique values from a list.

文法

HashSet< Type >nameOfSet = new HashSet<>();
登入後複製

Here, Type is the wrapper class object, not a primitive datatype.

Syntax for ArrayList

ArrayList<Type> nameOflist = new ArrayList< Type >();
登入後複製

Approach 1

  • Create an ArrayList object of type Integer and store some elements to it by using a built-in method named ‘add()’. This method takes an argument and adds it to the 指定的列表。

  • 現在,建立一個相同類型的HashSet,並儲存前一個元素的所有內容

  • # 使用內建方法‘addAll()’將ArrayList加入其中。這個方法接受一個ArrayList as an argument and adds all its elements to the new 指定的清單。

  • 最後,透過呼叫清單來列印值。

Example 

下面的範例示範了使用HashSet類別從ArrayList中取得唯一值的方法。

import java.util.*;
public class Unique {
   public static void main(String[] args) {
      // Creating arraylist
      ArrayList<Integer> araylist = new ArrayList<Integer>();
      
      // Adding elements in arraylist
      araylist.add(8);
      araylist.add(5);
      araylist.add(2);
      araylist.add(9);
      araylist.add(2);
      araylist.add(4);
      araylist.add(9);
      araylist.add(7);
      System.out.println("List with duplicate elements: ");
      
      // loop to iterate through elements
      for(int i = 0; i < araylist.size(); i++ ) {
         // to print the elements in the list
         System.out.println(araylist.get(i));
      }
      HashSet<Integer> set = new HashSet<>(); // creating hashset
      set.addAll(araylist); // adding all elements to set
      System.out.println("List with unique elements: " + set);
   }
}     
登入後複製

Output

#
List with duplicate elements:
8
5
2
9
2
4
9
7
List with unique elements: [2, 4, 5, 7, 8, 9]
登入後複製

Approach 2

  • Create an ArrayList object of type Integer and store some elements to it by using a built-in method named ‘add()’. This method takes an argument and adds it to the 指定的列表。

  • Now, create another ArrayList object of the same type.

  • Define a for loop that will iterate till the ArrayList size. Inside this loop, take an if 檢查索引'i'處的值是否唯一的程式碼區塊。如果是唯一的 然後將該值新增至新的ArrayList中,否則將其丟棄。

  • 結束時,透過呼叫清單列印值。

Example 

This is another example in which we will tr​​y to get unique values from an ArrayList.

import java.util.*;
public class Unique {
   public static void main(String[] args) {
      // Creating arraylist
      ArrayList<Integer> araylist1 = new ArrayList<Integer>();
      // Adding elements in arraylist
      araylist1.add(8);
      araylist1.add(5);
      araylist1.add(2);
      araylist1.add(9);
      araylist1.add(2);
      araylist1.add(4);
      araylist1.add(9);
      araylist1.add(7);
      System.out.println("List with duplicate elements: " + araylist1);
      ArrayList<Integer>araylist2 = new ArrayList<Integer>();
      for(int i = 0; i < araylist1.size(); i++ ) {
         int elem;
         // to filter the duplicate elements
         if(!araylist2.contains(araylist1.get(i))) {
            // storing unique elements to elem and adding to new list
            elem = araylist1.get(i);
            araylist2.add(elem);
         }
      }
      System.out.println("List with unique elements: " + araylist2);
   }
}
登入後複製

Output

#
List with duplicate elements: [8, 5, 2, 9, 2, 4, 9, 7]
List with unique elements: [8, 5, 2, 9, 4, 7]
登入後複製

Conclusion

We started this article by defining ArrayList and in the next section we understand the use of HashSet in finding unique values from a list. We discussed two approaches to get unique values from list. We discussed two approaches to get unique values from list。 We using this class

的中文翻譯為:類別

以上是在Java中從ArrayList取得唯一值的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板