合并两个list,不包含重复的对象
package com.compare.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ListTest {
public static List<Integer> createList1(){
List<Integer> list=new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
return list;
}
public static List<Integer> createList2(){
List<Integer> list=new ArrayList<Integer>();
list.add(7);
list.add(8);
list.add(9);
list.add(4);
list.add(5);
list.add(6);
return list;
}
public static List<Integer> combine(List<Integer> list1,List<Integer> list2){
List<Integer> list=new ArrayList<Integer>(list1);
System.out.println("list1:");
printList(list);
list.removeAll(list2);
System.out.println("list1-list2:");
printList(list);
list.addAll(list2);
System.out.println("list1+list2:");
printList(list);
Collections.sort(list);
printList(list);
return list;
}
public static void printList(List<Integer> list){
System.out.println("List输出如下:");
for (int i = 0; i < list.size(); i++) {
int j=list.get(i);
System.out.println(j);
}
}
public static void main(String[] args) {
List<Integer> list1=createList1();
List<Integer> list2=createList2();
List<Integer> list=combine(list1, list2);
//printList(list);
}
}
合并两个list,不包含重复的对象的更多相关文章
- 【转】合并两个List并去掉重复项
原文:https://my.oschina.net/jack90john/blog/1493170 工作中很多时候需要用到合并两个List并去除其中的重复内容.这是一个很简单的操作,这里主要是记录一下 ...
- leetcode350之实现求解两数组交集(包含重复元素)
给定两个数组,编写一个函数来计算它们的交集. 说明: 输出结果中每个元素出现的次数,应与元素在两个数组中出现的次数一致. 我们可以不考虑输出结果的顺序 def binarySearch(nums, t ...
- [LeetCode] Contains Duplicate III 包含重复值之三
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- 算法练习之x的平方根,爬楼梯,删除排序链表中的重复元素, 合并两个有序数组
1.x的平方根 java (1)直接使用函数 class Solution { public int mySqrt(int x) { int rs = 0; rs = (int)Math.sqrt(x ...
- 算法练习之合并两个有序链表, 删除排序数组中的重复项,移除元素,实现strStr(),搜索插入位置,无重复字符的最长子串
最近在学习java,但是对于数据操作那部分还是不熟悉 因此决定找几个简单的算法写,用php和java分别实现 1.合并两个有序链表 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两 ...
- (转载)按行合并两个sql的查询结果
(转载)http://blog.csdn.net/wxwstrue/article/details/6784774 Union all join 是平行合并 为水平连接 Union all 是垂直合并 ...
- [LeetCode] Contains Duplicate 包含重复值
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- 剑指Offer面试题:16.合并两个排序的链表
PS:这也是一道出镜率极高的面试题,我相信很多童鞋都会很眼熟,就像于千万人之中遇见不期而遇的人,没有别的话可说,唯有轻轻地问一声:“哦,原来你也在这里? ” 一.题目:合并两个排序的链表 题目:输入两 ...
- LeetCode 219. Contains Duplicate II (包含重复项之二)
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
随机推荐
- Jmeter-基于Ubuntu运行
这几天折腾了很久,整合了一套接口自动化的持续集成工具,先从最基础的运行Jmeter说起.由于我是用Docker部署的持续集成环境,所以接口运行必须在服务器上 一:在Linux服务器先安装jdk 1:先 ...
- ORACLE 11G R2 RAC classical install OGG12.1(LINUX) 经典抽取模式单项同步配置OGG12.1
博文结构图如下: 一.环境描述以及注意事项 1.1 环境简介 IP 系统 Oracle版本 OGG版本 源端 172.16.10.16/36 RHEL6.5 oracle11204 12.1 目标端 ...
- eclipse配置svn方法
一.在Eclipse里下载Subclipse插件 方法一:从Eclipse Marketplace里面下载 具体操作:打开Eclipse --> Help --> Eclipse Mark ...
- C语言函数2
一.PTA实验作业 6-3 使用函数判断完全平方数: 1. 本题PTA提交列表: 2. 设计思路: 3.本题调试过程碰到问题及PTA提交列表情况说明: 1.一开始考虑让输入值N去整除一个循环变量i,i ...
- 在Apache中运行Python WSGI应用
我们介绍如何使用Apache模块mod_wsgi来运行Python WSGI应用. 安装mod_wsgi 我们假设你已经有了Apache和Python环境,在Linux或者Mac上,那第一步自然是安装 ...
- 去掉xcode编译warning:ld: warning: directory not found for option '-L
选择工程, 编译的 (targets) 选择 Build Settings 菜单 查找 Library Search Paths 和 Framework Search Paths, 删掉编译报warn ...
- Flask 部署和分发
到目前为止,启动Flask应用都是通过"app.run()"方法,在开发环境中,这样固然可行,不过到了生产环境上,势必需要采用一个健壮的,功能强大的Web应用服务器来处理各种复杂情 ...
- 使用Github pages+jekyll搭建自己的博客(windows版)
最近突发奇想,想试试GitHub pages来搭建博客.网上一搜一大堆,嗯...看来还是挺简单的...于是自己撸起袖子干...... 结果对于我这种GitHub注册过,git 没用过,ruby.jek ...
- Raid 5数据恢复原理以及raid 5数据恢复实际操作案例
Raid 5数据恢复算法原理 要理解 raid 5数据恢复原理首先要先认识raid5,"分布式奇偶校验的独立磁盘结构"也就是我们称之为的raid 5数据恢复有一个概念需要理解,也就 ...
- 解决忽略VScode中Python插件pylint报错的问题
pylint是VScode中python自带的插件,可以帮助代码规范,美观. 但是有些报错是你不想看到的,你可以选择性的忽略. 例如,在re.compile()中,可以添加参数re.S使. 匹配任意字 ...