博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数组去重Demo引出的思考
阅读量:6858 次
发布时间:2019-06-26

本文共 4567 字,大约阅读时间需要 15 分钟。

package com.pers.Stream;import java.util.*;import java.util.stream.Collectors;import java.util.stream.IntStream;/** * 数组去重demo * * @author hoobey */public class RemoveDuplicatedDataTest {    public static void main(String[] args) {        String[] strArr = new String[]{
"abc", "ab", "abc"}; //Arrays.asList(T...a) 接受的是一个泛型的变长参数 而基本类型是无法被泛型化的 可使用基本包装类 List
strList = Arrays.asList(strArr);//String[] --> List
for (int i = 0; i < strList.size(); i++) {
//因为List是 有序 可重复的 所以并不会去掉重复内容 System.out.println(strList.get(i)+",");//java.io.PrintStream println()打印对象时会自动调用其toString() } int[] intArr = {
1, 2, 1, 3, 2, 4}; List list = Arrays.asList(intArr);//List
不行! --> 原理是List中只能存储对象! for (int i = 0; i < list.size(); i++) { Object obj = list.get(i); System.out.println("下标i=" + i + ",存储的内容:" + obj);//[I@14ae5a5 地址 int[] temp = (int[]) obj;//强转一下 得到地址中指向的数据 for (int j = 0; j < temp.length; j++) { System.out.print(temp[j]+","); } } // 把数组先变成List,List是继承Collection的接口,所以就可以把转换后的list给addAll() /* List intList = new ArrayList(); for (int i = 0; i < intArr.length; i++) { intList.add(intArr[i]); }*/ List
intList = IntStream.of(intArr).boxed().collect(Collectors.toList()); //要求各位把intArr中重复的数字去掉,仅留下相互不重复的数字 核心就是Set集合存储的是 无序不重复的数据 Set dupDataRemoved = new HashSet(intList);// dupDataRemoved.addAll(intList);// Collections.addAll(dupDataRemoved, intList); System.out.println(); System.out.print("去重后的数据是:"); for(Iterator it = dupDataRemoved.iterator(); it.hasNext(); ){ System.out.print(it.next() + ","); } //========================================= /*如果数组存储的元素是对象,那么可以直接用Arrays.aslist() 但如果是基本类型就需要使用相应的Stream来转。*/ String[] strArr2 = {
"xiaoming", "xiaoyu", "xiaoming", "xiaoming", "xiaoyu"}; List
list1 = Arrays.asList(strArr2);//List
Set strSet = new HashSet(list1);//构造一个包含指定 collection 中的元素的新 set for(Iterator it = strSet.iterator(); it.hasNext(); ){ System.out.print(it.next() + ","); } }}

abc,

ab,

abc,

下标i=0,存储的内容:[I@14ae5a5

1,2,1,3,2,4,

去重后的数据是:1,2,3,4,=========================================

xiaoyu,xiaoming,

Process finished with exit code 0                                                                      

/*在Stream API中,一个流基本上代表一个元素序列,StreamAPI提供了丰富的操作函数来计算这些元素。以前我们在开发业务应用时,通常很多操作的实现是这样做的:我们使用循环对集合做遍历,针对集合中的元素实现各种操作,定义各种变量来实现目的,这样我们就得到了一大堆丑陋的顺序代码。如果我们使用Stream API做同样的事情,使用Lambda表达式和其它函数进行抽象,可以使得代码更易于理解、更为干净。有了这些抽象,还可以做一些优化,比如实现并行等。*/    @Test    public void syso(){        IntStream rangeStream = IntStream.range(0, 10);        List
list = rangeStream.boxed().collect(Collectors.toList()); Iterator
it = list.iterator(); while (it.hasNext()){ System.out.print(it.next()+",");//0,1,2,3,4,5,6,7,8,9, } }                                                                                    
 
* 把int数组中的重复数据删除掉?---HashSet存储的数据都是对象,不能存储相同的元素int[] intArr = new int[]{
1,2,2,3,1,134,22,1,3,123,563,67,3,134,123,67,4,06,-1,1};//第一步,转化int数组为int流IntStream.of(intArr)//在Java中,8种基本数据类型都有对应的包装类//boolean;char;byte,short,int,long;float,double//要将存储的int转换为Integer对象(底层原理:HashSet存储的数据都是对象,不能存储相同的元素)IntStream.of(intArr).boxed() --> Stream中存储Integer对象//Stream --> ListList intList = IntStream.of(intArr).boxed().collect(Collectors.toList());//第二步,3种方式去重://HashSet的构造方法new HashSet(intList);//调用HashSet.addAll(Collection c); Collection接口 -继承-> List接口 --> AbstractList --> ArrayListnew HashSet().addAll(intList);//Collections ArraysHashSet hashset = new HashSet();Collections.addAll(hashset, intList);//迭代器Iterator,Collection.iterator()方法iterator.next(); */public class RemoveSame { public static void main(String[] args){ int[] intArr = {
1, 2, 3, 43, 2, 4, 1, 5765, 23, 12, 6, 1, 2, 56, 2, 3, 2, 3, 7, 8, 128843, 1}; List
intList = IntStream.of(intArr).boxed().collect(Collectors.toList()); Set dupDataRemoved = new HashSet(intList); for (Iterator it = dupDataRemoved.iterator(); it.hasNext(); ) { System.out.print(it.next() + ","); } /*Arrays.asList([]); 如果数组存储的元素是对象,那么可以直接用Arrays.aslist() 但如果是基本类型就需要使用相应的Stream来转。*/ }}
 

 

 

转载地址:http://xwxyl.baihongyu.com/

你可能感兴趣的文章
统计学习那些事
查看>>
XLT架构图(自己 画的)
查看>>
GitHub Top 100 简介
查看>>
C语言中链表任意位置怎么插入数据?然后写入文件中?
查看>>
文档对象模型DOM(二)
查看>>
loading.io一个loading图标网站,跟大家分享
查看>>
Hadoop之——CentOS构造ssh否password登录注意事项
查看>>
云计算的设计模式(三)——补偿交易模式
查看>>
ACM-凸多边形的计算几何——hrbust1429
查看>>
WPF笔记(2.8 常用的布局属性)——Layout
查看>>
MySQL随机获取数据的方法,支持大数据量
查看>>
【Struts】服务器文件的上传和下载
查看>>
UICollectionView设置item(cell)之间间距为0(紧挨在一起的效果)
查看>>
Nginx 负载均衡
查看>>
从 datetime2 数据类型到 datetime 数据类型的转换产生一个超出范围的值
查看>>
创业手记 Mr.Hua
查看>>
SpringMVC之Controller传递JSON数据到页面
查看>>
项目管理学习笔记之中的一个.项目管理综述
查看>>
matlab 工具之各种降维方法工具包,下载及使用教程,有PCA, LDA, 等等。。。...
查看>>
C语言 数组之无限循环
查看>>