由指定对象集合分出重复对象集合和不重复对象集合

package com.example.demo0810.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import com.example.demo0810.entity.Person;

public class ObjectSort {
	
	
	//由指定对象集合ObjectList分出重复对象集合ListRepeat 和不重复对象集合ListNoRepeat
	public List ObjectListSortRepeat(List personList,String repeat){
		List listId=new ArrayList();
		List listIdRepeat=new ArrayList();
		List listIdNoRepeat=new ArrayList();
		List listRepeat=new ArrayList();
		List listNoRepeat=new ArrayList();
		personList.forEach(p->listId.add(p.getId()));
		Collections.sort(listId);
		//获取重复ID
		for (int i = 0; i < listId.size(); i++) {
			int idNow=(int) listId.get(i);
			for (int j = i+1; j < listId.size(); j++) {
				if(idNow==(int)listId.get(j)) {
					listIdRepeat.add(listId.get(j));
//					System.out.print("listId:"+listId.get(j)+" ");
					break;
				}
			}
		}
		//获取重复id对于的对象集合ListRepeat
		for(int i=0;i objectSet= new HashSet<>();
		for(Iterator it=personList.iterator();it.hasNext();) {
			objectSet.add(it.next());
        }*/
		for (int i = 0; i < personList.size(); i++) {
			int idNow=(int) personList.get(i).getId();
			boolean flag=true;
			for (int j = 0; j < listIdRepeat.size(); j++) {
				if(idNow==(int)listIdRepeat.get(j)) {
					flag=false;
					break;
				}
			}
			if(flag==true) {
				listNoRepeat.add(personList.get(i));
			}
		}
		if("repeat".equals(repeat)) {
			return listRepeat;
		}else if("NoRepeat".equals(repeat)) {
			return listNoRepeat;
		}else {
			return null;
		}
	}
	
	public static void main(String[] args) {
		List personList=new ArrayList() {
			{
				add(new Person(	1,"yoko01",20,"ssss"));
				add(new Person(	3,"yoko02",20,"ssss"));
				add(new Person(	2,"yoko03",20,"ssss"));
				add(new Person(	1,"yoko01",20,"ssss"));
				add(new Person(	4,"yoko05",20,"ssss"));
			}
		};
		ObjectSort os=new ObjectSort();
		System.out.println("==============初始列表================");
		personList.forEach(ps->System.out.println(ps));
		System.out.println("==============重复列表================");
		os.ObjectListSortRepeat(personList, "repeat").forEach(ps->System.out.println(ps.toString()));
		System.out.println("==============非重复列表================");
		os.ObjectListSortRepeat(personList, "NoRepeat").forEach(ps->System.out.println(ps.toString()));
		
	}
}

输出结果:

由指定对象集合分出重复对象集合和不重复对象集合_第1张图片

 

简化版:

 public static void main(String[] args){
         List list  =   new  ArrayList(); 
         list.add("aaa");
         list.add("bbb");
         list.add("aaa");
         list.add("aba");
         list.add("aaa");

         Set set = new  HashSet(); 
         List newList = new  ArrayList(); 
         List newList1 = new  ArrayList(); 
         for (String cd:list) {
            if(set.add(cd)){
                newList.add(cd);
            }else{
                newList1.add(cd);
            }
        }
         System.out.println( "去重后的集合: " + newList); 
         System.out.println( "重复的集合: " + newList1); 
      }

 

你可能感兴趣的:(java开发基础入门,技术累积总结)