数组去重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 = 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<int[]>不行! --> 原理是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<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(0, 10);
        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[]{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 --> 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 = {1, 2, 3, 43, 2, 4, 1, 5765, 23, 12, 6, 1, 2, 56, 2, 3, 2, 3, 7, 8, 128843, 1};
        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来转。*/
    }
}

 

 
posted @ 2017-12-14 11:41  starskyhu  阅读(433)  评论(0编辑  收藏  举报