public static void main(String[] args) {
List<Object> list=new ArrayList <Object>();
Map<String, Object> map = new HashMap <String, Object> ( );
list.add ( "Love" );
list.add ( 666 ); //整型重复
list.add ( 666 );
list.add ( "Love" );// 字符串对象重复
list.add ( new User () );// 对象重复
list.add ( new User () );
list.add ( new Integer ( "256" ) );
list.add ( new Integer ( "256" ) );
map.put ( "001","我是001号" );
map.put ( "002","我是002号" );
list.add ( map ); // map集合去重
list.add ( map );
for (Object o:list) {
System.out.println ( "========"+o );
}
System.out.println ( "+++++++++去重后的结果++++++++" );
//Collectors.toList() 将流中的所有元素导出到一个列表( List )中
List <Object> newlist = list.stream().distinct().collect( Collectors.toList());
for (Object o:newlist) {
System.out.println ( "========"+o );
}
System.out.println ( "+++++++++合并两个集合的结果++++++++" );
List<Object> list2=new ArrayList <Object>();
list2.add ( "新建列表" );
list2.addAll( newlist );
System.out.println ( list2.containsAll ( newlist ) );//list2集合中包括newlist集合中的所有元素
System.out.println ( newlist.containsAll ( list2 ) );
for (Object o:list2) {
System.out.println ( "========"+o );
}
}
list集合去重
于 2022-06-05 13:00:20 首次发布