大部分是採用兩種方法,一種是用遍歷list集合判斷後賦給另外一個list集合,一種是用賦給set集合再返回給list集合。php
//set集合去重,不打亂順序 public static void main(String[] args){ List<String> list = new ArrayList<String>(); list.add("aaa"); list.add("bbb"); list.add("aaa"); list.add("aba"); list.add("aaa"); Set set = new HashSet(); List newList = new ArrayList(); for (String cd:list) { if(set.add(cd)){ newList.add(cd); } } System.out.println( "去重後的集合: " + newList); }
//遍歷後判斷賦給另外一個list集合 public static void main(String[] args){ List<String> list = new ArrayList<String>(); list.add("aaa"); list.add("bbb"); list.add("aaa"); list.add("aba"); list.add("aaa"); List<String> newList = new ArrayList<String>(); for (String cd:list) { if(!newList.contains(cd)){ newList.add(cd); } } System.out.println( "去重後的集合: " + newList); }
//set去重 public static void main(String[] args){ List<String> list = new ArrayList<String>(); list.add("aaa"); list.add("bbb"); list.add("aaa"); list.add("aba"); list.add("aaa"); Set set = new HashSet(); List newList = new ArrayList(); set.addAll(list); newList.addAll(set); System.out.println( "去重後的集合: " + newList); }
//set去重(縮減爲一行) public static void main(String[] args){ List<String> list = new ArrayList<String>(); list.add("aaa"); list.add("bbb"); list.add("aaa"); list.add("aba"); list.add("aaa"); List newList = new ArrayList(new HashSet(list)); System.out.println( "去重後的集合: " + newList); }
hashset不進行排序,還有一種方法是用treeset,去重而且按照天然順序排列,將hashset改成treeset就能夠了。(本來的順序是改變的,只是按照字母表順序排列而已)測試
//去重而且按照天然順序排列 List newList = new ArrayList(new TreeSet(list));
==============================================================================================================================================================================
由於用到list,要去除重複數據,嘗試了幾種方法。記錄於此。。。ui
測試數據:spa
List<string> li1 = new List<string> { "8", "8", "9", "9" ,"0","9"}; List<string> li2 = new List<string> { "張三", "張三", "李四", "張三", "王五", "李四" }; List<string> li3 = new List<string> { "A", "A", "C", "A", "C", "D" }; List<string> li4 = new List<string> { "12", "18", "19", "19", "10", "19" };
方法一:code
HashSet<string> hs = new HashSet<string>(li1); //此時已經去掉重複的數據保存在hashset中
方法二:對象
for (int i = 0; i < li2.Count; i++) //外循環是循環的次數 { for (int j = li2.Count - 1 ; j > i; j--) //內循環是 外循環一次比較的次數 { if (li2[i] == li2[j]) { li2.RemoveAt(j); } } }
方法三:blog
//把相同的用null代替。 for (int i = 0; i < li3.Count; i++) { for (int j = 0; j < li3.Count; j++) { if (i == j) continue; if (li3[i] == li3[j]) { li3[j] = "null"; } } }
方法四:排序
//這方法跟上面的同樣,只是變了邏輯 for (int i = 0; i < li4.Count - 1; i++) { for (int j = 0; j < li4.Count ; j++) { if (i != j) { if (li4[i] == li4[j]) { li4[j] = "null"; } } } }
最後輸出看結果繼承
Console.WriteLine("li1去除重複後的值爲"); hs.ToList().ForEach(item => Console.WriteLine(item)); Console.WriteLine("li2去除重複後的值爲"); li2.ForEach(item => Console.WriteLine(item)); Console.WriteLine("li3去除重複後的值爲"); li3.ForEach(item => Console.WriteLine(item)); Console.WriteLine("li4去除重複後的值爲"); li4.ForEach(item => Console.WriteLine(item));
null我沒去掉。用的時候去掉便可。接口
固然。還有許多辦法。好比linq Distinct 等等均可以,看看網上的這個例子:去掉modelList中title重複的內容,不區分大小寫
class Program { static void Main(string[] args) { List<Model> modelList = new List<Model>() { new Model() { ID = 1, Title = "abcde" }, new Model() { ID = 2, Title = "ABCDE" }, new Model(){ ID = 3, Title = "AbcdE" }, new Model() { ID = 4, Title = "A" }, new Model() { ID = 5, Title = "a" } }; Console.Read(); } } public class Model { public int ID { get; set; } public string Title { get; set; } }
解決方案一:這裏比較的前提是對象的哈希代碼相等。不然不會比較,由於哈希代碼不相等。兩個對象顯然不相等
//定義一個類繼承IEqualityComparer接口 public class ModelComparer : IEqualityComparer<Model> { public bool Equals(Model x, Model y) { return x.Title.ToUpper() == y.Title.ToUpper(); } public int GetHashCode(Model obj) { return obj.Title.ToUpper().GetHashCode(); } }
調用:
modelList = modelList.Distinct(new ModelComparer()).ToList();
解決方案二:
var title = modelList.GroupBy(m => m.Title.ToLower().Trim()).Select(m => new { ID = m.FirstOrDefault().ID }); modelList = modelList.Where(m => title.Select(mo => mo.ID).Contains(m.ID)).ToList(); foreach (var item in modelList) { Console.WriteLine(item.ID + "\t" + item.Title); }
固然。若是你僅僅比較兩個值是否相等。
List<string> li1 = new List<string> { "8", "8", "9", "8", "0", "9" }; li1 = li1.Distinct().ToList();