java list过滤重复的数据_List 去除重复数据的 5 种正确姿势!

以下介绍五种-不同的方法去除 Java 中ArrayList中的重复数据

1.使用LinkedHashSet删除arraylist中的重复数据

LinkedHashSet是在一个ArrayList删除重复数据的最佳方法。LinkedHashSet在内部完成两件事:

删除重复数据

保持添加到其中的数据的顺序

Java示例使用LinkedHashSet删除arraylist中的重复项。在给定的示例中,numbersList是包含整数的arraylist,其中一些是重复的数字。

例如1,3和5.我们将列表添加到LinkedHashSet,然后将内容返回到列表中。结果arraylist没有重复的整数。

import java.util.ArrayList;

import java.util.Arrays;

import java.util.LinkedHashSet;

public class ArrayListExample

{

public static void main(String[] args)

{

ArrayList numbersList = new ArrayList<>(Arrays.asList(1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8));

System.out.println(numbersList);

LinkedHashSet hashSet = new LinkedHashSet<>(numbersList);

ArrayList listWithoutDuplicates = new ArrayList<>(hashSet);

System.out.println(listWithoutDuplicates);

}

}

输出结果

[1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8]

[1, 2, 3, 4, 5, 6, 7, 8]

2.使用java8新特性stream进行List去重

要从arraylist中删除重复项,我们也可以使用java 8 stream api。使用steam的distinct()方法返回一个由不同数据组成的流,通过对象的equals()方法进行比较。

收集所有区域数据List使用Collectors.toList()。

Java程序,用于在不使用Set的情况下从java中的arraylist中删除重复项。

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

import java.util.stream.Collectors;

public class ArrayListExample

{

public static void main(String[] args)

{

ArrayList numbersList = new ArrayList<>(Arrays.asList(1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8));

System.out.println(numbersList);

List listWithoutDuplicates = numbersList.stream().distinct().collect(Collectors.toList());

System.out.println(listWithoutDuplicates);

}

}

输出结果

[1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8]

[1, 2, 3, 4, 5, 6, 7, 8]

3.利用HashSet不能添加重复数据的特性 由于HashSet不能保证添加顺序,所以只能作为判断条件保证顺序:

private static void removeDuplicate(List list) {

HashSet set = new HashSet(list.size());

List result = new ArrayList(list.size());

for (String str : list) {

if (set.add(str)) {

result.add(str);

}

}

list.clear();

list.addAll(result);

}

4.利用List的contains方法循环遍历,重新排序,只添加一次数据,避免重复:

private static void removeDuplicate(List list) {

List result = new ArrayList(list.size());

for (String str : list) {

if (!result.contains(str)) {

result.add(str);

}

}

list.clear();

list.addAll(result);

}

5.双重for循环去重

for (int i = 0; i < list.size(); i++) {

for (int j = 0; j < list.size(); j++) {

if(i!=j&&list.get(i)==list.get(j)) {

list.remove(list.get(j));

}

}

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本文链接:https://blog.csdn.net/qq_37939251/article/details/90713643

近期热文推荐:

觉得不错,别忘了随手点赞+转发哦!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/541032.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

打击侵犯公民个人信息罪的司法困境

当前&#xff0c;公民个人信息泄露并屡遭侵犯已成为社会关注焦点。泄露的信息轻则给被害人生活造成困扰&#xff0c;重则使被害人陷入电信诈骗、敲诈勒索等犯罪漩涡&#xff0c;造成重大人身、财产损失。一些民众认为&#xff0c;对侵犯公民个人信息行为的刑事打击很不给力&…

ruby中、.reject_Ruby中带有示例的Array.reject方法

ruby中、.rejectRuby Array.reject方法 (Ruby Array.reject Method) In the last article, we have seen how we can make use of the Array.select method in order to print the Array elements based on certain conditions provided inside the block? In this article, w…

java获取主机mac_Java 如何获取主机的MAC地址

获取MAC地址首先要理解当前的操作系统&#xff0c;由于在不同的操作系统中CMD命令所在的位置不同&#xff0c;因此首先使用System类中的getProperty("os.name")方法获取当前的操作系统&#xff0c;getProperty()方法可以确定当前系统属性&#xff0c;它的参数是一些固…

微软免费软件项目DreamSpark更名为Microsoft Imagine

9月10日消息&#xff0c;微软免费软件项目DreamSpark近日正式更名为Microsoft Imagine&#xff0c;将与一年一度的微软“创新杯(Imagine Cup)”齐名。微软免费软件项目DreamSpark更名为Microsoft Imagine  2008年2月19日&#xff0c;微软公司董事长比尔盖茨在斯坦福大学发布了…

java jpa_Java JPA 语法知识

前提操作创建一个可持久化的实体类dao层继承JpaRepositoryT&#xff1a;实体类ID&#xff1a;实体类的主键类型例&#xff1a;public interface SysUserRespository extends JpaRepository {}JPA中支持的关键词And --- 等价于 SQL 中的 and 关键字&#xff0c;比如 findByUsern…

array.slice_Ruby中带有示例的Array.slice()方法

array.sliceArray.slice()方法 (Array.slice() Method) In this article, we will study about Array.slice() method. You all must be thinking the method must be doing something which is related to the slicing of elements or objects in the Array instance. It is n…

阿特斯携手EDF启动建设巴西191.5MW光伏项目

2016年10月11日&#xff0c;阿特斯太阳能&#xff08;安大略省&#xff0c;圭尔夫&#xff09;和EDF Energies Nouvelles&#xff08;法国&#xff0c;巴黎&#xff09;共同宣布&#xff0c;将阿特斯巴西Pirapora I太阳能项目80&#xff05;的股权出售给EDF的巴西本地子公司EDF…

apachejmeter_java源码_自定义编写jmeter的Java测试代码

我们在做性能测试时,有时需要自己编写测试脚本,很多测试工具都支持自定义编写测试脚本,比如LoadRunner就有很多自定义脚本的协议,比如"C Vuser","JavaVuser"等协议.同样,Jmeter也支持自定义编写的测试代码,不过与LoadRunner不同的是,Jmeter没有自带编译器,…

julia fit 函数_带有Julia中示例的flipsign()函数

julia fit 函数Julia| flipsign()函数 (Julia | flipsign() function) flipsign() function is a library function in Julia programming language, it accepts two values as parameters and returns a value with the magnitude of first value and sign of the first value…

优化Android应用内存的若干方法

https://my.oschina.net/chaselinfo/blog/198172摘要: 在app开发的各个阶段中要考虑RAM的限制问题, 包括在设计阶段(正式开发之前). 使用下面的不同的方法可以达到很好的效果. 当您在设计和开发Android应用时用下面的方法可以使内存运用最高效.使用保守的Service 如果你的应用需…

一? ilkkn.n_IL&FS的完整形式是什么?

一? il&kkn.nIL&#xff06;FS&#xff1a;基础设施租赁和金融服务 (IL& FS: Infrastructure Leasing & Financial Services) IL&FS is an abbreviation of Infrastructure Leasing & Financial Services. It is the largest infrastructure development …

java notify唤醒原理_Java wait和notify虚假唤醒原理

自己在此记录一下&#xff0c;方便日后复习。虚假唤醒的概念jdk官方文档解释&#xff1a;所以说在wait和notify一块使用时&#xff0c;如果使用if作为条件时&#xff0c;会有虚假唤醒的情况发生&#xff0c;所以必须使用while作为循环条件。下面来举例实验&#xff1a;首先&…

C#里面的三种定时计时器:Timer

在.NET中有三种计时器&#xff1a;1、System.Windows.Forms命名空间下的Timer控件&#xff0c;它直接继承自Componet。Timer控件只有绑定了Tick事件和设置EnabledTrue后才会自动计时&#xff0c;停止计时可以用Stop()方法控制&#xff0c;通过Stop()停止之后&#xff0c;如果想…

wireshark rto_RTO的完整形式是什么?

wireshark rtoRTO&#xff1a;地区运输办公室/公路运输办公室 (RTO: Regional Transport Office/ Road Transport Office) RTO is an abbreviation of the Regional Transport Office. It is an Indian Government departmental organization that is responsible for upholdin…

java8 json转xml_2019-08-17java对象与json,xml互转

依赖的jar包&#xff0c;jackson-all-1.7.6.jar,xstream-1.4.4.jar下载地址:链接&#xff1a;https://pan.baidu.com/s/1LflD135qlQiIPGXw5XwDmw提取码&#xff1a;6v29复制这段内容后打开百度网盘手机App&#xff0c;操作更方便哦package json_xml;import com.thoughtworks.xs…

10.8-全栈Java笔记:序列化/反序列化的步骤和实例

本节我们详细讲解10.3节中提到的序列化和反序列化操作。序列化和反序列化是什么当两个进程远程通信时&#xff0c;彼此可以发送各种类型的数据。 无论是何种类型的数据&#xff0c;都会以二进制序列的形式在网络上传送。比如&#xff0c;我们可以通过http协议发送字符串信息&am…

有效的网络推广超级实用方法

我叫龙雨&#xff0c;先后在百度搜狗工作过3年&#xff0c;后来一直负责一家公司的的网络营销!不知道大家有没有听过111>3这样一个概念&#xff0c;简单来说一下这概念!第一呢就是自己的资源&#xff0c;把自己的资源维护好开发好;第二就是网络营销&#xff0c;网络营销利用…

什么为java运行时的环境_什么是JRE?Java运行时环境简介(一)

Java开发工具包(JDK),Java虚拟机(JVM)和Java运行时环境(JRE)共同构成了用于开发和运行Java应用程序的Java平台组件的强大功能.实际上,运行时环境是一种旨在运行其他软件的软件.作为Java的运行时环境,JRE包含Java类库,Java类加载器和Java虚拟机.在这个系统中:的类加载器是负责正…

c语言atoll函数怎么用_C ++中带有示例的atoll()函数

c语言atoll函数怎么用C Atoll()函数 (C atoll() function) atoll() function is a library function of cstdlib header. It is used to convert the given string value to the integer value. It accepts a string containing an integer (integral) number and returns its…

看清美国“黑客帝国”的真面目

“维基揭秘”网站近日发布了近9000份据称属于美国中央情报局的机密文件&#xff0c;显示中情局拥有强大的黑客攻击能力&#xff0c;秘密侵入了手机、电脑、智能电视等众多智能设备。继美国国家安全局承包商前雇员斯诺登曝光国安局“棱镜”等监控计划之后&#xff0c;此次曝光再…