搜索
首页Javajava教程在Java中从ArrayList获取唯一值

在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 try 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 an ArrayList, first using the HashSet class and the second without using this class

的中文翻译为:类

以上是在Java中从ArrayList获取唯一值的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:tutorialspoint。如有侵权,请联系admin@php.cn删除
如何将Maven或Gradle用于高级Java项目管理,构建自动化和依赖性解决方案?如何将Maven或Gradle用于高级Java项目管理,构建自动化和依赖性解决方案?Mar 17, 2025 pm 05:46 PM

本文讨论了使用Maven和Gradle进行Java项目管理,构建自动化和依赖性解决方案,以比较其方法和优化策略。

如何使用适当的版本控制和依赖项管理创建和使用自定义Java库(JAR文件)?如何使用适当的版本控制和依赖项管理创建和使用自定义Java库(JAR文件)?Mar 17, 2025 pm 05:45 PM

本文使用Maven和Gradle之类的工具讨论了具有适当的版本控制和依赖关系管理的自定义Java库(JAR文件)的创建和使用。

如何使用咖啡因或Guava Cache等库在Java应用程序中实现多层缓存?如何使用咖啡因或Guava Cache等库在Java应用程序中实现多层缓存?Mar 17, 2025 pm 05:44 PM

本文讨论了使用咖啡因和Guava缓存在Java中实施多层缓存以提高应用程序性能。它涵盖设置,集成和绩效优势,以及配置和驱逐政策管理最佳PRA

如何将JPA(Java持久性API)用于具有高级功能(例如缓存和懒惰加载)的对象相关映射?如何将JPA(Java持久性API)用于具有高级功能(例如缓存和懒惰加载)的对象相关映射?Mar 17, 2025 pm 05:43 PM

本文讨论了使用JPA进行对象相关映射,并具有高级功能,例如缓存和懒惰加载。它涵盖了设置,实体映射和优化性能的最佳实践,同时突出潜在的陷阱。[159个字符]

Java的类负载机制如何起作用,包括不同的类载荷及其委托模型?Java的类负载机制如何起作用,包括不同的类载荷及其委托模型?Mar 17, 2025 pm 05:35 PM

Java的类上载涉及使用带有引导,扩展程序和应用程序类负载器的分层系统加载,链接和初始化类。父代授权模型确保首先加载核心类别,从而影响自定义类LOA

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器