java中怎样去除list中重复的数据,并且显示重复数据的条数 Java中如何去除List中的重复的值
Java\u4e2d\u5982\u4f55\u53bb\u9664List\u4e2d\u7684\u91cd\u590d\u7684\u503c1.\u7528\u53cc\u91cd\u5faa\u73af\u9010\u4e00\u68c0\u67e5\u5143\u7d20\u662f\u5426\u91cd\u590d\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a
public static void removeDuplicate(List list) { for (int i = 0; i i; j--) { if (list.get(j).equals(list.get(i))) { list.remove(j); } } } }2.\u901a\u8fc7HashSet\u53bb\u91cd\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a
public static void removeDuplicate1(List list) { HashSet h = new HashSet(list); list.clear(); list.addAll(h); }
\u9996\u5148\u5c06\u89e3\u51b3\u65b9\u6848\uff1a
\u5efa\u7acb\u4e00\u4e2a\u4e34\u65f6\u53d8\u91cf\uff0c\u7136\u540e\u53bb\u904d\u5386\u6570\u636e\uff0c\u5c06\u5f53\u524d\u5143\u7d20\u8d4b\u503c\u7ed9\u4e34\u65f6\u53d8\u91cf\uff0c\u62ff\u4e34\u65f6\u53d8\u91cf\u53bb\u548c\u6240\u6709\u5143\u7d20\u8fdb\u884c\u6bd4\u8f83\uff0c\u8bb0\u5f55\u91cd\u590d\u7684\u6b21\u6570\uff0c\u5982\u679c\u5927\u4e8e\u4e00\u5c31\u5220\u9664\u5f53\u524d\u8fd9\u4e2a\u6570\u3002
\u5176\u6b21\uff0c\u5982\u679c\u60f3\u5b58\u50a8\u4e0d\u91cd\u590d\u7684\u503c\uff0c\u5efa\u8bae\u4f7f\u7528\u96c6\u5408Set\uff0c\u5229\u7528Set\u4e0d\u5141\u8bb8\u91cd\u590d\u503c\u7684\u7279\u6027\uff0c\u5b58\u50a8\u5bf9\u8c61\u65f6\u5982\u679c\u60f3\u81ea\u5b9a\u4e49\u6bd4\u8f83\u6761\u4ef6\uff0c\u5c31\u91cd\u5199hashcode() \u548c equals() \u65b9\u6cd5
很多集合处理,我都推荐用Java8的流来处理,是非常简单易懂的
其实你这个问题,总来说就是按照集合中的字符串进行分类,并统计数量,Stream只用简单的代码就完成复杂的操作,代码如下
List<String> list = Arrays.asList("day", "walipo", "day", "walipo", "legeand", "day", "douwbo", "day", "day");
Map<String, Long> collect = list.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(collect);
最后返回的Map就是以字符串为key,value为其个数
删除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)。
List<String> list = new ArrayList<String>();
list.add("a");
list.add("a");
list.add("a");
list.add("b");
list.add("b");
list.add("c");
list.add("c");
list.add("c");
list.add("c");
//默认每个元素至少出现了一次
Map<String, Integer> map = new HashMap<String, Integer>();
for (String string : list) {
map.put(string, 1);
}
for (int i = 0; i < list.size(); i++) //外循环是循环的次数
{
for (int j = list.size() - 1 ; j > i; j--) //内循环是 外循环一次比较的次数
{
if (list.get(i).equals(list.get(j)))
{
list.remove(j);
if(map.containsKey(list.get(i))){
map.put(list.get(i), map.get(list.get(i))+1);
}
}
}
}
for (String string : list) {
System.out.println("去除重复后的元素:"+string+"--重复的次数:"+map.get(string));
}
/********************************************************************************
* Project Name [java-projects]
* File Name [PACKAGE_NAME.CountDuplication.java]
* Creation Date [2016-12-07]
* <p/>
* Copyright© 2016 [email protected] All Rights Reserved
* <p/>
* Work hard, play harder, think big and keep fit
********************************************************************************/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author <a href="mailto:[email protected]">杨舸</a>
* @version 1.0.0
* @since jdk 1.8
*/
public class CountDuplication {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("day");
list.add("walipo");
list.add("day");
list.add("walipo");
list.add("legeand");
list.add("day");
list.add("douwbo");
list.add("day");
list.add("day");
Map<String, Integer> map = new HashMap<>();
for (String str : list) {
Integer count = map.get(str);
if (count == null) {
count = 1;
map.put(str, count);
} else {
map.put(str, ++count);
}
}
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();
if (value == 1) {
System.out.println(key + "有1条");
} else {
System.out.println(key + "有" + (value - 1) + "条重复记录");
}
}
System.out.println(map.keySet()); // 不重复的元素
}
}
package com.Frame;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Main {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<String, String>();
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("d");
List<String> tempList = new ArrayList<String>();
for (String i : list) { // 删除重复元素
if (!tempList.contains(i)) {
tempList.add(i);
}
}
for (String a : list) // 统计元素重复次数
{
String count = map.get(a);
if (count == null) {
map.put(a, "1");
} else {
map.put(a, (Integer.parseInt(count) + 1) + "");
}
}
System.out.println("list中去重后的元素有:");
for (String i : tempList) {
System.out.println(i);
}
System.out.println("其中:");
for (String key : map.keySet()) {
System.out.println(key + "出现" + map.get(key) + "次");
}
}
}
绛旓細鐩存帴鏀惧叆set涓銆傚鏋淟ist鐨勬硾鍨嬫槸鍩烘湰绫诲瀷锛堝皝瑁呯被锛夋垨String锛屽彲浠ョ洿鎺ヨ繖鏍峰仛銆備絾鏄硾鍨嬫槸浣犺嚜宸卞啓鐨勭被锛屽氨闇瑕佷綘鎶婅繖涓被閲嶅啓equals鍜宧ashCode鏂规硶銆俰mport java.util.ArrayList;import java.util.LinkedHashSet;import java.util.List;import java.util.Set; public class Test { public static void ...
绛旓細濡傛灉鏄鎶奓ist涓殑閲嶅鍏冪礌鍒犻櫎鐨勮瘽鍙互鍏堝惂List杞垚Set鍘婚櫎閲嶅鍏冪礌 姣斿鐜板湪鏈変釜鏁扮粍涓 myArray 锛岄噷闈㈡湁閮ㄥ垎鐨勯噸澶嶅厓绱 Set mySet = new HashSet();for(Object obj : Array){ mySet.add(obj);} mySet涓墍淇濆瓨鐨勫厓绱犲氨鏄敮涓鐨勪簡.鍐嶅惂mySet淇濆瓨鍒版暟缁勪腑 瀹屾暣渚嬪瓙锛// 鍒涘缓涓涓暟缁勶紝閲岄潰...
绛旓細鍏堢粰浣犱竴涓湅鐪嬫槸涓嶆槸浣犻渶瑕佺殑濂戒簡锛// 鍒╃敤 Set 鐨勭壒鎬э紝灏嗘墍鏈夐」鐩斁鍏 Set //涓嵆鍙Щ闄ら噸澶嶇殑椤圭洰Set<String> stringSet = new HashSet<String>();for (String element : duplicateArray) {stringSet.add(element);}// Set.size() 涓轰笉閲嶅椤圭洰鐨勪釜鏁癝tring nonDuplicateArray[] = new ...
绛旓細鏈濂界殑鍔炴硶鏄敤Set锛屽洜涓篠et閲岄潰瀛樻斁鐨勬暟鎹槸涓嶉噸澶嶇殑銆傚鏋滀綘涓嶆兂鐢⊿et锛岄偅杩樺彲浠ュ悜涓嬮潰杩欐牱澶勭悊銆侺ist list_tmp = new ArrayList(); //寤虹珛涓涓敤浜庝复鏃跺瓨鏀句笉閲嶅list鍏冪礌鐨凩ist闆嗗悎銆俧or(Object obj:list){ //鍙互鏍规嵁闇瑕侊紝鍦ㄨ繖閲屽姞涓婂己鍒惰浆鍨嬨//濡俵ist閲岄潰瀛樻斁鐨勬槸涓涓猆ser瀵硅薄锛岄偅涔堝姞涓奤s...
绛旓細寰堝闆嗗悎澶勭悊锛鎴戦兘鎺ㄨ崘鐢↗ava8鐨勬祦鏉ュ鐞锛屾槸闈炲父绠鍗曟槗鎳傜殑 鍏跺疄浣犺繖涓棶棰橈紝鎬绘潵璇村氨鏄寜鐓ч泦鍚堜腑鐨勫瓧绗︿覆杩涜鍒嗙被锛屽苟缁熻鏁伴噺锛孲tream鍙敤绠鍗曠殑浠g爜灏卞畬鎴愬鏉傜殑鎿嶄綔锛屼唬鐮佸涓 List<String> list = Arrays.asList("day", "walipo", "day", "walipo", "legeand", "day", "douwbo", "...
绛旓細閬嶅巻List 鎶List閲岀殑绗竴涓厓绱犲拰鍏朵粬鍏冪礌姣旇緝锛屽鏋滄湁閲嶅鐨勫氨remove鎺 闇瑕佸弻灞侳or寰幆瀹炵幇
绛旓細arr); //鏂规硶鍘婚噸System.out.println(arr);}}package com.test;import java.util.*;public class Test { /**AAAA AAAA BBBB BBBB CCCC CCCC CCCC CCCC */public static void main(String[] args) {List<String> list = new ArrayList<String>();list.add("AAAA");list.add("AAAA"...
绛旓細鍋囪鍘熸暟鎹細List userlist ...鏂规硶1(涔熸槸鎴戞瘮杈冨父鐢ㄧ殑):鏂板缓涓涓狶ist锛岄亶鍘嗗師list锛屽姞鍏ユ柊鐨刲istList newlist = new ArrayList();for ( v : userlist) { if (!newlist.contains(v)) newlist.add(v);}...鏂规硶2:鍒╃敤set闆嗗悎Set set = new HashSet(); List newlist = new ArrayL...
绛旓細List<String> stuList3 = new ArrayList<String>();stuList3.addAll(stuList);stuList3.addAll(stuList2);//鎶婁袱涓泦鍚堝悎鎴愪竴涓 stuList.retainAll(stuList2);//鎵惧嚭涓や釜闆嗗悎涓噸澶鐨鏁版嵁 stuList3.removeAll(stuList);//鐒跺悗绉婚櫎 for(String s: stuList3){ System.out.println(s);//...
绛旓細java椤圭洰涓幏鍙杔ist閲嶅鏁版嵁鍙互浣跨敤list鐨reduplicateIndex鏂规硶,瀹炰緥濡備笅:public static void main(String[] args) throws Exception { List<String> list = new ArrayList<String>(); list.add("123"); list.add("456"); list.add("555"); list.add("123"); list.add("444"); list.add("123"); ...