java对List去重并排序,如何快速地去掉两个 java中两个list集合如何排序
java\u4e2d\uff0c\u5982\u4f55\u6bd4\u8f832\u4e2alist,\u53bb\u6389\u76f8\u540c\u7684\uff0c\u53d6\u4e0d\u540c\u7684\uff0c\u518d\u7ec4\u5408\u6210\u4e00\u4e2alist\u597d\u50cflist\u6709\u4e2acontains\u65b9\u6cd5\uff0c\u8fed\u4ee3\u6bd4\u8f83\uff0c\u5177\u4f53\u600e\u4e48\u6bd4\u8f83\uff0c\u8fd8\u5f97\u770b\u4f60list\u91cc\u9762\u662f\u4ec0\u4e48\u6570\u636e\u7c7b\u578b,\u4f60\u7684\u4e24\u4e2alist\u5e94\u8be5\u4e0d\u662f\u5305\u542b\u548c\u88ab\u5305\u542b\u7684\u5173\u7cfb\u5427\u3002
\u9996\u5148\u8ba9\u4f60\u7684Article\u5b9e\u73b0Comparable\u8fd9\u4e2a\u63a5\u53e3..
\u7136\u540e\u6839\u636e\u4ed6\u7684gxrq\u8fdb\u884c\u6bd4\u8f83..
\u96c6\u5408\u5408\u6210\u7684\u8bdd
list1.addAll(list2)\u5c31\u884c\u4e86\u3002
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
public class Article implements Comparable {
public static void main(String[] args) {
List list1 = new ArrayList();
List list2 = new ArrayList();
Article a1 = new Article();
a1.setGxrq(new Date(100, 5, 5));
Article a2 = new Article();
a2.setGxrq(new Date(100, 3, 5));
list1.add(a1);
list2.add(a2);
list1.addAll(list2);
Collections.sort(list1);
for(Article a : list1) {
System.out.println(a.getGxrq());
}
}
private String title;
private String content;
private Date gxrq;
public int compareTo(Article arg0) {
return this.gxrq.compareTo(arg0.gxrq);
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getGxrq() {
return gxrq;
}
public void setGxrq(Date gxrq) {
this.gxrq = gxrq;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
Java8开始,对数值,集合等提供了Stream流操作,可以方便的对集合进行操作.
比如 筛选,过滤,去重, 映射, 排序,规约,收集 等操作
简单的参考代码如下
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
// 使用Java8 的Stream特性 和 Java8的Lambda 语句
public class Demo {
public static void main(String[] args) {
// 需求: 集合去重,排序,得到一个新集合里面的元素是之前元素的平方
List<Integer> list=Arrays.asList(5,2,2,1,3,4);
List<Integer> listResult = list
.stream() // 得到流
.distinct() // 去重 5,2,1,3,4
.sorted() // 自然排序 , 也可以自定义排序规则1,2,3,4,5
.map(x->x*x) // 每个元素求平方1,4,9,16,25
.collect(Collectors.toList()); //收集并返回
System.out.println(listResult);//1,4,9,16,25
}
}
绛旓細List<String> list = new ArrayList<>();/*list.add("abc");list.add("abc");list.add("def");list.add("bcd");*/Set<String> set = new LinkedHashSet<>();//杩欑鏂瑰紡淇濊瘉鍘婚噸鍚鐨勫厓绱犵浉瀵逛綅缃笉鍙//Set<String> set = new HashSet<>();//杩欑鏂瑰紡鍘婚噸鍚庡厓绱犵殑浣嶇疆寰堟湁鍙兘鍙樺寲...
绛旓細鍘绘帀閲嶅瀵硅薄鐨勬柟妗堝緢澶, 姣斿鍒╃敤set鍘婚噸绛.鍙傝冧唬鐮 import java.util.ArrayList;import java.util.HashSet;public class Test {public static void main(String[] args) {ArrayList<Object> list =new ArrayList<Object>();list.add(8);list.add(2);list.add(8);list.add(7);list.add(6);...
绛旓細List<Integer> listTemp= new ArrayList<Integer>();Iterator<Integer> it=list.iterator();while(it.hasNext()){ int a=it.next();if(listTemp.contains(a)){ it.remove();} else{ listTemp.add(a);} } for(Integer i:list){ System.out.println(i);} } lhkzyz java List 鍘婚噸锛堜袱绉...
绛旓細浣犳妸o.equals(list)閭e彞鎹㈡垚list.contains(o)璇曡瘯銆傜幇鍦ㄤ綘鐨勪唬鐮侀噷o鏄釜Object锛堝叾瀹炴槸String锛锛宭ist鏄釜List锛鏍规湰涓嶅彲鑳絜qual鐨 鍝︿笅闈㈤偅涓猯i.remove(o)涔熸崲鎴恖ist.remove(o)___鍝︿笉瀵广傘傘備綘涓嬮潰鐨刬terator寰幆閲岄潰鍏跺疄鎶婃瘡涓涓兘鍒犳帀浜嗐傘傘--- 鎴戝姞浜嗕竴浜涙垜鐨勬敼鍔ㄤ唬鐮侊紝涓嶈繃濡傚彟涓涓...
绛旓細鍦ㄤ綘鐨勭被閲岄潰閲嶅啓compareto鏂规硶锛岀敤浣犵殑閭d袱涓睘鎬ф潵鍒ゆ柇姣旇緝鏍囧噯銆傦紙浣犵殑绫婚渶瑕佸厛瀹炵幇comparable鎺ュ彛锛
绛旓細listAll.addAll(list2); listAll = new ArrayList<Integer>(new LinkedHashSet<>(listAll)); System.out.println(listAll); }杈撳嚭:[1, 2, 3, 4, 7, 10]浠g爜瑕佸吀:鍚堝苟 浣跨敤java.util.List.addAll(Collection<? extends Integer>)鍘婚噸,鍊熷姪LinkedHashSet ...
绛旓細list.add("娴嬭瘯5");System.out.println("娌℃湁鍘婚噸鍓嶇殑鏁版嵁涓>>>"+list.toString());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涓殑鎵鏈夊厓绱犵劧鍚庡垹闄ら噸澶嶃備袱灞傚惊鐜紝澶栧眰寰幆浠庣涓涓厓绱犲悜鏈鍚庝竴涓厓绱犲惊鐜紝鍐呭眰寰幆鏄粠鏈鍚庝竴涓厓绱犲悜澶栧眰寰幆鍏冪礌鐨勫綋鍓嶅厓绱犲惊鐜傛瘮杈冧袱涓厓绱犳槸鍚︾浉绛夛紝濡傛灉鐩哥瓑锛岀Щ闄ら潬鍚庣殑鍏冪礌鏉ヨ繘琛鍘婚噸銆傝繖绉嶆柟娉曟椂闂村鏉傚害澶т簬O(n)锛屽皬浜嶰(N_)姝ょ鏂瑰紡鏄埄鐢ㄤ簡Set鐨勭壒鎬э細鍏冪礌涓嶅彲閲嶅锛屽叾搴曞眰...
绛旓細checkNumExist){ nums.add(num); } } public boolean checkNumExist(Integer){ return nums.contains(Integer); }}杩欎釜SingleList灏辩畝鍗曞疄鐜颁簡鍘婚噸澧炲姞锛屽彲浠ョ敤瀹冩潵璇绘暟鎹 public class Test{ public static void main(String[] args){ SingleList nums = new SingleList()...
绛旓細鍦ㄤ綆鐗堟湰涓(java8-) ,鍙互鐢ㄥ惊鐜 ,涓涓竴涓亶鍘嗗苟鍒ゆ柇 ,鎵惧埌鍚堥傜殑鏉′欢 ,鎶婁綘鎯宠鐨勫璞℃斁鍏ヤ竴涓泦鍚堜腑