去除List列表中反复值(稍作调整,也适合于List<T> 和 List<?>)
方法一 循环元素删除
- public static void removeDuplicate(List list) {
- for ( int i = 0 ; i < list.size() - 1 ; i ++ ) {
- for ( int j = list.size() - 1 ; j > i; j -- ) {
- if (list.get(j).equals(list.get(i))) {
- list.remove(j);
- }
- }
- }
- System.out.println(list);
方法二 // 删除ArrayList中反复元素
- public static void removeDuplicate(List list) {
- for ( int i = 0 ; i < list.size() - 1 ; i ++ ) {
- for ( int j = list.size() - 1 ; j > i; j -- ) {
- if (list.get(j).equals(list.get(i))) {
- list.remove(j);
- }
- }
- }
- System.out.println(list);
方法三 删除ArrayList中反复元素,保持顺序
- // 删除ArrayList中反复元素,保持顺序
- public static void removeDuplicateWithOrder(List list) {
- Set set = new HashSet();
- List newList = new ArrayList();
- for (Iterator iter = list.iterator(); iter.hasNext();) {
- Object element = iter.next();
- if (set.add(element))
- newList.add(element);
- }
- list.clear();
- list.addAll(newList);
- System.out.println( " remove duplicate " + list);
- }
方法四 linq
- IList<string> U_list= new List<string>();
- IList<string> Update_list= new List<string>();
- var ulist = (from li in U_list
- select li).Distinct();
- foreach (var d in ulist)
- {
- Update_list.Add(d);
- }
其它情况:
去除List列表中反复值(3种解决方法)
public static void main(String[] args) {
String[] ar = { "dd", "c", "dd", "ff", "b", "e", "e" };
ArrayList list = new ArrayList();
for (int i = 0; i < ar.length; i++) {
list.add(ar[i]);
}
System.out.println("执行前:");
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
}
System.out.println();
/* 第一种方法 */
/**
* set方法去除list中反复的数据 set中插入反复的值仅仅保留一个
*/
HashSet h = new HashSet(list);
list.clear();
list.addAll(h);
/* 第一种方法 */ /* 另外一种方法 */
/**
* 第二中方法去除list中反复的数据
*/
Set set = new HashSet();
List newList = new ArrayList();
for (Iterator iter = list.iterator(); iter.hasNext();) {
Object element = iter.next();
if (set.add(element))
newList.add(element);
}
list.clear();
list.addAll(newList);
/* 另外一种方法 */ /* 第三种方法 */
/**
* 普通的方法去除list中反复的数据
*/
for (int i = 0; i < list.size() - 1; i++) {
for (int j = list.size() - 1; j > i; j--) {
if (list.get(j).equals(list.get(i))) {
list.remove(j);
}
}
}
/* 第三种方法 */
System.out.println("执行后:");
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
}
}
去除List列表中反复值(稍作调整,也适合于List<T> 和 List<?>)的更多相关文章
- selenium+java:获取列表中的值
selenium+java:获取列表中的值 (2011-08-23 17:14:48) 标签: 杂谈 分类: selenium 初步研究利用java+testNg框架下写selenium测试用例,今天 ...
- Python访问列表中的值
Python访问列表中的值: 列表中可以包含所有数据类型: # 列表中可以存放 数字数据类型数据 # int 型数据 lst = [1,2,3] print(lst) # [1, 2, 3] # fl ...
- c# json转换成dynamic对象,然后在dynamic对象中动态获取指定字符串列表中的值
using Newtonsoft.Json;using System;using System.Collections.Generic;using System.Linq;using System.T ...
- erlang取列表中某个值的位置
有个需求,比如在一个列表中,取出一个元素的位置,如果出现重复都取出.例如:List = [2,3,10,324,88,29,12],可以求大于某个值的位置,也可以取某个值的位置. 废话少说,直接上代码 ...
- Python3基础 sort 将一个列表中的值升序排列
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- python列表中的值转换为字符串,及列表里的所有值拼接成一个字符串 的方法
后记: ls3='%'.join(ls2) 会把%加入拼接成的字符里面,同理,加入其它字符也一样,''空就是什么都不加,如上图 最后输出 1%我%22
- 使用easyui实现双击列表中某个值直接对其进行修改
var editCell = undefined; $('#dg').datagrid({ url:'DwzServlet', iconCls:'icon icon-list' , queryPara ...
- Java 去除List列表中的重复项
/** * Remove list duplicate item * * @param srcList * @return */ private static ArrayList<Resolve ...
- python 访问列表中的值
#!/usr/bin/python list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print ...
随机推荐
- leetcode Contains Duplicate python
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- IOS 特定于设备的开发:获取额外的设备信息
sysctl()和sysctlbyname()允许获取系统信息.这些标准的UNIX函数用于询问操作系统有关硬件和OS的详细信息. 这些常量使你能够检查核心信息,比如系统的CPU频率,可用的内存量等.它 ...
- file_get_contents post数据
//默认模拟的header头 private function _defaultHeader() { $header = "User-Agent:Mozilla/5.0 (Windows; ...
- iOS6与iOS7屏幕适配技巧
一.没有包装任何 导航控制器 或者 UITabBarController 1.控制器的view是UIScrollView\UITableView\UICollectionView时(控制器是UITab ...
- swith 语句详解
switch 语句的格式: switch ( 整型或字符型变量 ) { case 变量可能值1 : 分支一; break; case 变量可能值2 : 分支二; break; case 变量可 ...
- Ordering Tasks(拓扑排序+dfs)
Ordering Tasks John has n tasks to do. Unfortunately, the tasks are not independent and the executio ...
- 第八届河南省赛G.Interference Signal(dp)
G.Interference Signal Time Limit: 2 Sec Memory Limit: 128 MB Submit: 35 Solved: 17 [Submit][Status ...
- android 视频播放器的INTENT-FILTER属性
<intent-filter> <action android:name="android.intent.action.VIEW&quo ...
- tomcat 7 无法打开管理页面
在配置文件tomcat-users.xml中添加如下内容即可. <role rolename="admin"/> <role rolename="man ...
- sql 分割字符串 存储过程
默认是用 , 分割,如有需要可以自己更改 我写的是 循环插入数据 存储过程,也可以自己改成 方法使用 存储过程: ),@jrid int) as begin declare @location int ...