优雅的找出ArrayList中重复的元素
前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程。
方法1
代码:
public class Main { public static void main(String[] args){ List<String> list = new ArrayList<String>(); list.add("aaa"); list.add("bbb"); list.add("ccc"); list.add("ddd"); list.add("aaa"); list.add("aaaa"); list.add("eee"); list.add("bbb"); list.add("ccc"); StringBuilder builder = new StringBuilder(); for(String str : list) { // 如果不存在返回 -1。 if(builder.indexOf(","+str+",") > -1) { System.out.println("重复的有:"+str); } else { builder.append(",").append(str).append(","); } } }
}
运行结果:
重复的有:aaa
重复的有:bbb
重复的有:ccc
方法2
代码如下:
public class Main { public static void main(String[] args){ List<String> list=new ArrayList<String>(); list.add("string1"); list.add("string2"); list.add("string3"); list.add("string1"); list.add("string1"); list.add("string1"); list.add("string2"); //list.add("string3"); Map<String,Integer> hashMap=new HashMap<String, Integer>(); for(String string:list){ if(hashMap.get(string)!=null){ //hashMap包含遍历list中的当前元素 Integer integer=hashMap.get(string); hashMap.put(string,integer+1); System.out.println("the element:"+string+" is repeat"); } else{ hashMap.put(string,1); } } }
}
运行结果:
the element:string1 is repeat
the element:string1 is repeat
the element:string1 is repeat
the element:string2 is repeat
方法3
因为我没有用java8,所以idea不支持stream,所有此方法没有验证,暂时保存啦。
代码如下:
public class Main { public static <E> List<E> getDuplicateElements(List<E> list) { return list.stream() // list 对应的 Stream .collect(Collectors.toMap(e -> e, e -> 1, (a, b) -> a + b)) // 获得元素出现频率的 Map,键为元素,值为元素出现的次数 .entrySet().stream() // 所有 entry 对应的 Stream .filter(entry -> entry.getValue() > 1) // 过滤出元素出现次数大于 1 的 entry .map(entry -> entry.getKey()) // 获得 entry 的键(重复元素)对应的 Stream .collect(Collectors.toList()); // 转化为 List } public static void main(String[] args) throws Exception { List<String> list = Arrays.asList("a", "b", "c", "d", "a", "a", "d", "d"); List<String> duplicateElements = getDuplicateElements(list); System.out.println("list 中重复的元素:" + duplicateElements); } }
运行结果:
list 中重复的元素:[a,d]
方法4
public class Test { public static void main(String[] args) { List<String> strings = new ArrayList<>(); strings.add("1"); strings.add("2"); strings.add("2"); strings.add("3"); strings.add("3"); strings.add("3"); Set<String> set = new HashSet<>(); Set<String> exist = new HashSet<>(); for (String s : strings) { if (set.contains(s)) { exist.add(s); } else { set.add(s); } } System.out.println("重复的值:" + String.join(", ", exist)); } }
转自:https://blog.csdn.net/Lin_88zq/article/details/72677040
https://blog.csdn.net/caoxiaohong1005/article/details/54286384
优雅的找出ArrayList中重复的元素相关推荐
- JavaScript 找出数组中重复的元素
实现检测数组重复元素的功能,需要注意一点的是,多个(2个或2个以上)重复元素,我们只需要挑出一个来就可以了. <!DOCTYPE html> <html><head> ...
- 找出集合中重复元素和不重复元素
//找出集合中重复和不重复元素List<String> list6 = new ArrayList<>();Collections.addAll(list6, "a& ...
- 找出数组中重复的数字---多思路
问题:找出数组中重复的数字. 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次.请找出数组中任意 ...
- 找出重复的数java_剑指offer:1.找出数组中重复的数(java版)
数组中重复的数: 题目:找出数组中重复的数, 题目描述: 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的. 也不知道每个数字重复几次.请找 ...
- 剑指offer 面试题三 找出数组中重复的数字
1 import org.junit.Test; 2 3 import java.util.Arrays; 4 import java.util.HashSet; 5 6 public class D ...
- 13. 找出数组中重复的数字【难度: 简单 / 知识点: 模拟】
13. 找出数组中重复的数字[难度: 简单 / 知识点: 模拟] 方法一: map 计数 时间复杂度O(nlongn) 空间复杂度多了一个二叉树 class Solution {public:int ...
- java如何找重复数字_Java如何找出数组中重复的数字
题目描述:找出数组中重复的数字,具体内容如下 在一个长度为n的数组里的所有数字都在 0~n-1的范围内.数组中某些数字是重复的,但不知道有几个数字是重复的,也不知道每个数字重复了几次.请找出数组中任意 ...
- 图解面试题:找出数组中重复的数字?
今天分享的题目来源于 LeetCode 上的剑指 Offer 系列 面试题03. 数组中重复的数字. 题目链接:https://leetcode-cn.com/problems/shu-zu-zhon ...
- 01、找出数组中重复的数字
1.找出数组中重复的数字 题目:在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. ...
最新文章
- 关于Shiro框架权限标识符中*使用的总结
- 第三方类AFNetworking
- C语言实现PID算法:位置式PID和增量式PID
- IO流操作-图片操作(二)
- 使用FUSE挖掘文件上传漏洞
- 问题:二进制数中1的个数。
- boost的chrono模块等待按键的测试程序
- JVM—堆栈 堆 方法区 静态区 final static 内存分配
- java成员变量的初始化
- keil写文字怎么会乱码_主持稿怎么写才会吸引人
- java.io.FileNotFoundException:/mnt/sdcard/......(Permission denied)
- Qt文档阅读笔记-QVariant::value()与qvariant_cast解析及使用
- linux chown命令: 修改文件或目录的所有者或群组
- FL Studio20.8.2(水果win10)中文版主要软件更新内容
- 在线 IDE,支持 C-sharp, PHP, JavaScript - CodeRun
- css设置背景颜色透明度
- 头条鲁班新户千展费用_今日头条广告投放的费用大概是多少?
- 收到微软律师函怎么办?当接到微软律师函的处理办法
- 小啊呜产品读书笔记001:《邱岳的产品手记-03》第04+05讲 如何当好AI时代的产品经理 06讲 产品案例分析·TheGuardian的文本之美
- Content has been consumed
热门文章
- Java运用自身排序算法将数组或容器进行随机打乱。
- Python3 爬虫学习笔记 C18【爬虫框架 pyspider — 深入理解】
- REVERSE-PRACTICE-BUUCTF-29
- 【hihocoder - offer编程练习赛60 C】路径包含问题(LCA,树上倍增)
- 【牛客 - 302哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(低年级)】小乐乐切割方块(思维,水题)
- linux awk 时间范围,linux下使用awk命令按时间段筛选日志
- html div分钟刷新一次啊代码_接口测试平台代码实现57首页重构5
- python tabula 使用方法_Python中os.walk()的使用方法
- 级差公排php如何_什么是专业级差?专业级差怎么安排专业?
- Oracle中如何获取当天时间的最开始的时间和最结尾的时间: