数组去重Demo引出的思考
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<String> strList = Arrays.asList(strArr);//String[] --> List<String>
for (int i = ; i < strList.size(); i++) {//因为List是 有序 可重复的 所以并不会去掉重复内容
System.out.println(strList.get(i)+",");//java.io.PrintStream println()打印对象时会自动调用其toString()
} int[] intArr = {, , , , , };
List list = Arrays.asList(intArr);//List<int[]>不行! --> 原理是List中只能存储对象!
for (int i = ; i < list.size(); i++) {
Object obj = list.get(i);
System.out.println("下标i=" + i + ",存储的内容:" + obj);//[I@14ae5a5 地址 int[] temp = (int[]) obj;//强转一下 得到地址中指向的数据
for (int j = ; 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<Integer> 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<String> list1 = Arrays.asList(strArr2);//List<String> 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中,一个流基本上代表一个元素序列,Stream
API提供了丰富的操作函数来计算这些元素。以前我们在开发业务应用时,通常很多操作的实现是这样做的:我们使用循环对集合做遍历,针对集合中的元素实现各种操作,定义各种变量来实现目的,这样我们就得到了一大堆丑陋的顺序代码。 如果我们使用Stream API做同样的事情,使用Lambda表达式和其它函数进行抽象,可以使得代码更易于理解、更为干净。有了这些抽象,还可以做一些优化,比如实现并行等。*/
@Test
public void syso(){
IntStream rangeStream = IntStream.range(, );
List<Integer> list = rangeStream.boxed().collect(Collectors.toList());
Iterator<Integer> 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[]{,,,,,,,,,,,,,,,,,,-,}; //第一步,转化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 --> List
List intList = IntStream.of(intArr).boxed().collect(Collectors.toList()); //第二步,3种方式去重:
//HashSet的构造方法
new HashSet(intList);
//调用HashSet.addAll(Collection c); Collection接口 -继承-> List接口 --> AbstractList --> ArrayList
new HashSet().addAll(intList);
//Collections Arrays
HashSet hashset = new HashSet();
Collections.addAll(hashset, intList); //迭代器Iterator,Collection.iterator()方法
iterator.next();
*/
public class RemoveSame {
public static void main(String[] args){
int[] intArr = {, , , , , , , , , , , , , , , , , , , , , };
List<Integer> 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来转。*/
}
}
数组去重Demo引出的思考的更多相关文章
- JavaScript中好用的对象数组去重
对象数组去重 Demo数据如下: var items= [{ "specItems": [{ "id": "966480614728069122&qu ...
- 也谈面试必备问题之 JavaScript 数组去重
Why underscore (觉得这部分眼熟的可以直接跳到下一段了...) 最近开始看 underscore.js 源码,并将 underscore.js 源码解读 放在了我的 2016 计划中. ...
- JavaScript之数组去重
前言:昨天看到了别人发的帖子,谈到了面试题中经常出现的数组去重的问题.作为一个热爱学习.喜欢听老师话的好孩纸,耳边忽然想起来高中老师的谆谆教导:不要拿到题就先看答案,要先自己思考解答,然后再对照答案检 ...
- js引用类型数组去重-对象标记法
前言 Js数组去重已经有很多种实现方式:包括逐个检索对比(使用Array.property.indexOf),先排序后对比,使用hash表,利用ES6中的Set()等.这些数组去重办法中速度最快的是h ...
- Javascript中的数组去重-indexof方法
在Javascript中,有时我们会用到数组去重.我在这里给大家介绍一下本人认为最简单实用的一种方法-indexOf()去重. var arr = [1,1,1,2,2,2,3,3,4,5,6,2,1 ...
- javaScript数组去重方法
在JAvascript平时项目开发中经常会用到数组去重的操作.这时候就要用到JS数组去重的方法了. demo1: 第一种:JS数组去重操作方法是利用遍历原数组,利用数组的indexOf()方法来来判断 ...
- 再谈 javascript 数组去重
前言 数组去重方法老生常谈,既然是常谈,我也来谈谈 双层循环 也许我们首先想到的是使用 indexOf 来循环判断一遍,但在这个方法之前,让我们先看看最原始的方法: var array = [1,1, ...
- JS数组去重算法实现
之前一段时间一直在准备面试, 因而博客太久没更新: 现在基本知识点都复习完毕, 接下来就分享下 面试的一些常见问题: 去正规的互联网公司笔试.面试有很大的概率会碰到 使用javascript实现数组去 ...
- js面试题-数组去重
今天,在聊天群里看到数组去重的话题,面试者的答案如下: 参考答案如下: 程序员思维,做出如下测试: 未考虑到:1,‘1’是不同的,应该不去重 未考虑到对象 所以,参考答案只能去重基础类型 根据以往看过 ...
随机推荐
- 20165235 实验二Java面向对象程序设计
20165235 Java面向对象程序设计 姓名:祁瑛 学号:20165235 班级:1652 实验课程:JAVA程序设计 实验名称:Java面向对象程序设计 实验时间:2018.4.14 指导老师: ...
- 简单的使用Nginx框架搭建Web服务器~
系统环境Debian 8,内核版本 一.首先来安装nginx服务程序: 1.安装nginx服务需要的相关程序(记得在root权限下操作下面的指令) aptitude install libpcre3 ...
- Binder原理
--摘自<android插件化开发指南> 1.Binder分为Client和Server两个进程: client和server是相对的.谁发消息,谁就是client:谁接收消息,谁就是se ...
- Dicom文件转mhd,raw文件格式
最近在整理与回顾刚加入实验室所学的相关知识,那会主要是对DICOM这个医疗图像进行相应的研究,之前有一篇博客已经讲述了一些有关DICOM的基本知识,今天这篇博客就让我们了解一下如何将Dicom文件转为 ...
- springmvc controller动态设置content-type
springmvc RequestMappingHandlerAdapter#invokeHandlerMethod 通过ServletInvocableHandlerMethod#invokeAn ...
- basename
我使用过的Linux命令之basename - 去掉文件名的目录和后缀 本文链接:http://codingstandards.iteye.com/blog/840784 (转载请注明出处) 用途 ...
- ISP PIPLINE(零) 知识综述预热
本文为camera isp pipline概述 ISP,即image signal processing.为图像成型做的处理工作.适应不同光学环境下图像的还原. pipline流程如下: 光通过LEN ...
- Window通过zip安装并启动mariadb
下载解压后进入bin目录 使用mysql_install_db.exe工具:https://mariadb.com/kb/en/mariadb/mysql_install_dbexe/ 安装完成后,在 ...
- Spring使用笔记(三) 高级装配
高级装配 一.环境与Profile 一)配置profile bean 环境的改变导致配置改变(需求:通过环境决定使用哪个bean),可以通过Spring的Profile解决. Profile可以在程序 ...
- How to show color in CSS
转至:https://blog.csdn.net/CallMeQiuqiuqiu/article/details/54743459 http://www.w3school.com.cn/cssref/ ...