合并多个list并去重

问题描述:java 怎么把多个list 合并成一个去掉重复的 大家好,给大家分享一下合并两个list去除重复的元素,很多人还不知道这一点。下面详细解释一下。现在让我们来看看!

如何将两个List合并,且其中不允许出现重复的项

合并多个list并去重的相关图片

示例代码:

    public static void main(String[] args){。

        List<Integer> list1 = new ArrayList<Integer>();。

        list1.add(1);。

        list1.add(2);。

        list1.add(3);。

        list1.add(4);。

        List<Integer> list2 = new ArrayList<Integer>();。

        list2.add(1);。

        list2.add(4);。

        list2.add(7);。

        list2.add(10);。

        List<Integer> listAll = new ArrayList<Integer>();。

        listAll.addAll(list1);。

        listAll.addAll(list2);。

        listAll = new ArrayList<Integer>(new LinkedHashSet<>(listAll));。

        System.out.println(listAll);。

    }

输出:

[1, 2, 3, 4, 7, 10]。

代码要典:

合并 使用java.util.List.addAll(Collection<? extends Integer>)。

去重,借助LinkedHashSet 。

java 合并两个 list内容 剔除相同内容的相关图片

java 合并两个 list内容 剔除相同内容

public static void main(String args[]){。

    /**

      * 大体思路:

      * 1.把list2添加到list1中;添加时,先循环list2,用list2中的每个值,分别和。

      *  list1去比较,如果重复,跳出循环,则不添加,如果不重复,则list2的值添加到list1中。

      */

    List<Integer> list2=new ArrayList<Integer>();  。

     list2.add(1);  。

     list2.add(2);  。

     list2.add(4);  。

    

    List<Integer> list1=new ArrayList<Integer>();  。

     list1.add(1);  。

     list1.add(2);  。

     list1.add(5);  。

     

    temp = 0;//重复标识 0-该值在list1中唯一;1-该值在list1中有重复值。

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

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

            if (list2.get(i) == list1.get(j)){。

                temp = 1;。

            }

        }

        if(temp = 0){。

            list1.add(list2.get(i));。

        }

    }  

    

    System.out.println(list1.size());。

后记:list1本身也需要去重,方法一样,自己去一下吧。

请教两个ArrayList合并,并且去掉重复数据的算法的相关图片

请教两个ArrayList合并,并且去掉重复数据的算法

遍历这两个list ,使用 双层循环,在内循环判断,若外循环的list的value存在在内循环中,则同时删除两个list中的那个value,不存在的话,使用第三个list来动态添加,最后返回第三个list就是你所需要的resultList。

JAVA,用List做,两个数组中数的合并和去除相同元素的相关图片

JAVA,用List做,两个数组中数的合并和去除相同元素

import java.util.*;。

public class Test {。

public static void main(String[] args){ 。

List <Integer> l1=new ArrayList<Integer>();。

l1.add(1);l1.add(2);l1.add(3);。

l1.add(4);l1.add(5);l1.add(6);。

List <Integer> l2=new ArrayList<Integer>();。

l2.add(4);l2.add(5);l2.add(6);。

l2.add(7);l2.add(8);l2.add(9);。

Set<Integer> s=new TreeSet(l1);。

for(Integer i:l2){。

//当添加不成功的时候 说明s中已经存在该对象,直接remove掉该对象即可。

if(!s.add(i)) s.remove(i);。

}

System.out.println(s);。

//还可以用下面一种方法

List <Integer> temp=new ArrayList<Integer>(l1);//用来保存两者共同有的数据。

temp.retainAll(l2);。

l1.removeAll(temp);//l1中去掉两者共同有的数据。

l2.removeAll(temp);//l2中去掉两者共同有的数据。

List <Integer> l3=new ArrayList<Integer>();。

l3.addAll(l1);。

l3.addAll(l2);。

System.out.println(l3);。

}

如何去除LIST里的重复元素并合并相同的对象

package com.ajax.test;。

import java.io.File;。

import java.io.FileWriter;。

import java.util.Arrays;。

/**

* 把一个数组的元素复制到另个数组; 去除重复元素不能用SET集合; 每次复制的记录输到一个文件里。

*

* @author ajax_2003。

* @version 1.0, 2009-7-26。

*

*/

public class CopyArrayAndRemoveDuplicate {。

private static String FILE_PATH = "d:/abc.txt";。

private static File file;。

static {

file = new File(FILE_PATH);。

}

/**

* 取出冗余数据

*

* @param nums。

* 原数组。

*/

private int[] removeDuplicate(int[] nums) throws Exception {。

int[] tmpArray = new int[nums.length];。

int count = 0;。

loop: //

for (int i = 0; i < nums.length; i++) {。

int tmp = nums[i];。

for (int j = 0; j < count; j++) {。

if (tmp == tmpArray[j])。

continue loop;。

}

tmpArray[count++] = tmp;。

log("成功复制了元素" + tmp);// 写日志。

}

return copyArray(tmpArray, 0, count);。

}

/**

* 拷贝数组

*

* @param srcArray。

* 要拷贝的数组。

* @param startIndex。

* 拷贝起始索引。

* @param endIndex。

* 拷贝结束索引。

* @return 结果数组。

*/

private int[] copyArray(int[] srcArray, int startIndex, int endIndex)。

throws Exception {。

if (endIndex <= startIndex)。

throw new Exception("Argumens wrong!");。

int[] desArray = new int[endIndex - startIndex];。

System.arraycopy(srcArray, startIndex, desArray, 0, desArray.length);。

return desArray;。

}

/**

* 输出操作日志(即: 每次复制的记录输到一个文件里)。

*

* @param oprate。

*/

private void log(String oprate) {。

FileWriter out = null;。

try {

out = new FileWriter(file, true);。

out.write(oprate + "\r\n");。

} catch (Exception e) {。

e.printStackTrace();。

} finally {

try {

if (out != null)。

out.close();。

out = null;。

} catch (Exception ex) {。

ex.printStackTrace();。

}

}

}

public static void main(String[] args) {。

int[] nums = { 1, 223, 1, 2, 2, 2, 3, 2, 3, 34, 45, 5, 5, 3, 23, 2, 2,。

3, 4, 5, 5, 6, 7, 78, 8, 9, 34, 90, 45, 675, 4, };。

int[] finalArray;。

try {

finalArray = new CopyArrayAndRemoveDuplicate()。

.removeDuplicate(nums);。

System.out.println(Arrays.toString(finalArray));。

} catch (Exception e) {。

e.printStackTrace();。

}

}

有些地方可能考虑的不完全,见谅!

//合并数组并排序

public void arrCopy() {。

int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };。

int[] b = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 };。

System.arraycopy(a, 2, b, 5, 5);。

for(int n : b){。

System.out.print(n+",");。

}

System.out.println();。

//去掉重复元素

public static void main(String args[]) { 。

int[] arr = { 1, 2, 2, 3, 3, 4, 4, 5, 5, 5, 6, 6, 7, 8, 9, 9, 10, 11, 11, 。

11, 12, 12, 13, 14, 14, 15 }; // 预设数据数组 。

int index = 1; // 保存最后一个不重复的位置 。

int last = arr[0]; 。

for (int i = 1; i < arr.length; i++) { 。

if (arr[i] != last) { 。

arr[index] = arr[i]; 。

last = arr[index]; 。

index++; 。

}

}

int[] rtn = new int[index]; 。

System.arraycopy(arr, 0, rtn, 0, index); 。

System.out.println(Arrays.toString(rtn)); 。

}

原文地址:http://www.qianchusai.com/%E5%90%88%E5%B9%B6%E5%A4%9A%E4%B8%AAlist%E5%B9%B6%E5%8E%BB%E9%87%8D.html

cc/比划我猜题目大全成语,我比划你猜游戏成语题目

cc/比划我猜题目大全成语,我比划你猜游戏成语题目

gao第二声的字,二声hao字有哪些

gao第二声的字,二声hao字有哪些

emolument和salary的区别,emolument和remuneration区别

emolument和salary的区别,emolument和remuneration区别

片段作文200字四季,四季描写作文200多字

片段作文200字四季,四季描写作文200多字

小聚心情短语,十年姐妹小聚心情短语

小聚心情短语,十年姐妹小聚心情短语

新sat写作范文,新sat写作高分突破pdf

新sat写作范文,新sat写作高分突破pdf

有你真好友情事例,有你真好作文关于朋友事例

有你真好友情事例,有你真好作文关于朋友事例

草船借箭读后感50字以下,草船借箭读后感怎么写100字

草船借箭读后感50字以下,草船借箭读后感怎么写100字

basolateral,basolateral compartment

basolateral,basolateral compartment

双生火焰最后一世才会相遇,双生火焰最后一世合一后,下一世会怎样

双生火焰最后一世才会相遇,双生火焰最后一世合一后,下一世会怎样