目录
Java Program to get Unique Values from ArrayList
HashSet
语法
Syntax for ArrayList
Approach 2
Example 
下面的示例演示了使用HashSet类从ArrayList中获取唯一值的方法。

" >

下面的示例演示了使用HashSet类从ArrayList中获取唯一值的方法。

Output
This is another example in which we will try to get unique values from an ArrayList.

" >

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

Conclusion
首页 Java java教程 在Java中从ArrayList获取唯一值

在Java中从ArrayList获取唯一值

Sep 04, 2023 am 08:41 AM
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中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热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 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

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

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

Java 中的平方根 Java 中的平方根 Aug 30, 2024 pm 04:26 PM

Java 中的平方根指南。下面我们分别通过例子和代码实现来讨论平方根在Java中的工作原理。

Java 中的完美数 Java 中的完美数 Aug 30, 2024 pm 04:28 PM

Java 完美数指南。这里我们讨论定义,如何在 Java 中检查完美数?,示例和代码实现。

Java 中的阿姆斯特朗数 Java 中的阿姆斯特朗数 Aug 30, 2024 pm 04:26 PM

Java 中的阿姆斯特朗数指南。这里我们讨论一下java中阿姆斯特朗数的介绍以及一些代码。

Java 中的随机数生成器 Java 中的随机数生成器 Aug 30, 2024 pm 04:27 PM

Java 随机数生成器指南。在这里,我们通过示例讨论 Java 中的函数,并通过示例讨论两个不同的生成器。

Java中的Weka Java中的Weka Aug 30, 2024 pm 04:28 PM

Java 版 Weka 指南。这里我们通过示例讨论简介、如何使用weka java、平台类型和优点。

Java 中的史密斯数 Java 中的史密斯数 Aug 30, 2024 pm 04:28 PM

Java 史密斯数指南。这里我们讨论定义,如何在Java中检查史密斯号?带有代码实现的示例。

Java Spring 面试题 Java Spring 面试题 Aug 30, 2024 pm 04:29 PM

在本文中,我们保留了最常被问到的 Java Spring 面试问题及其详细答案。这样你就可以顺利通过面试。

突破或从Java 8流返回? 突破或从Java 8流返回? Feb 07, 2025 pm 12:09 PM

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

See all articles