集合-将list的新旧数据做对比
一、String集合
List < String > newList = new ArrayList < String > ( ) ;
newList. add ( "1" ) ;
newList. add ( "2" ) ;
newList. add ( "3" ) ;
newList. add ( "5" ) ;
newList. add ( "7" ) ;
newList. add ( "9" ) ;
List < String > oldList = new ArrayList < String > ( ) ;
oldList. add ( "2" ) ;
oldList. add ( "3" ) ;
oldList. add ( "4" ) ;
oldList. add ( "5" ) ;
oldList. add ( "6" ) ;
oldList. add ( "8" ) ;
oldList. add ( "10" ) ;
List < String > newListMore = new ArrayList < String > ( ) ;
for ( int i = 0 ; i < newList. size ( ) ; i++ ) {
if ( oldList. indexOf ( newList. get ( i) ) < 0 ) {
newListMore. add ( newList. get ( i) ) ;
}
}
System . out. println ( "新的列表里多的东西:" + newListMore) ;
System . out. println ( "-------------------------------" ) ;
List < String > oldListLess = new ArrayList < String > ( ) ;
for ( int i = 0 ; i < oldList. size ( ) ; i++ ) {
if ( newList. indexOf ( oldList. get ( i) ) < 0 ) {
oldListLess. add ( oldList. get ( i) ) ;
}
}
System . out. println ( "以前列表里少的东西:" + oldListLess) ;
System . out. println ( "-------------------------------" ) ;
List < String > list = new ArrayList < String > ( ) ;
for ( int i = 0 ; i < oldList. size ( ) ; i++ ) {
if ( newList. indexOf ( oldList. get ( i) ) >= 0 ) {
list. add ( oldList. get ( i) ) ;
}
}
System . out. println ( "没变化的数据:" + list) ;
System . out. println ( "-------------------------------" ) ;
二、Map集合
Map < String , String > map1 = new HashMap < > ( ) ;
map1. put ( "id" , "A001" ) ; map1. put ( "name" , "张三" ) ;
Map < String , String > map2 = new HashMap < > ( ) ;
map2. put ( "id" , "A002" ) ; map2. put ( "name" , "李四" ) ;
Map < String , String > map3 = new HashMap < > ( ) ;
map3. put ( "id" , "A003" ) ; map3. put ( "name" , "王五" ) ;
List < Map < String , String > > newMapList = new ArrayList < > ( ) ;
newMapList. add ( map1) ; newMapList. add ( map2) ; newMapList. add ( map3) ;
Map < String , String > map4 = new HashMap < > ( ) ;
map4. put ( "id" , "A001" ) ; map4. put ( "name" , "张三" ) ;
Map < String , String > map5 = new HashMap < > ( ) ;
map5. put ( "id" , "A003" ) ; map5. put ( "name" , "王五" ) ;
Map < String , String > map6 = new HashMap < > ( ) ;
map6. put ( "id" , "A004" ) ; map6. put ( "name" , "熊大" ) ;
Map < String , String > map7 = new HashMap < > ( ) ;
map7. put ( "id" , "A005" ) ; map7. put ( "name" , "熊二" ) ;
List < Map < String , String > > oldMapList = new ArrayList < > ( ) ;
oldMapList. add ( map4) ; oldMapList. add ( map5) ; oldMapList. add ( map6) ; oldMapList. add ( map7) ;
List < Map < String , String > > insertList = new ArrayList < > ( ) ;
for ( int i = 0 ; i < newMapList. size ( ) ; i++ ) {
if ( oldMapList. indexOf ( newMapList. get ( i) ) < 0 ) {
insertList. add ( newMapList. get ( i) ) ;
}
}
System . out. println ( "新增:" + insertList) ;
System . out. println ( "-------------------------------" ) ;
List < Map < String , String > > deleteList = new ArrayList < > ( ) ;
for ( int i = 0 ; i < oldMapList. size ( ) ; i++ ) {
if ( newMapList. indexOf ( oldMapList. get ( i) ) < 0 ) {
deleteList. add ( oldMapList. get ( i) ) ;
}
}
System . out. println ( "删除:" + deleteList) ;
System . out. println ( "-------------------------------" ) ;
List < Map < String , String > > noChangeList = new ArrayList < > ( ) ;
for ( int i = 0 ; i < oldMapList. size ( ) ; i++ ) {
if ( newMapList. indexOf ( oldMapList. get ( i) ) >= 0 ) {
noChangeList. add ( oldMapList. get ( i) ) ;
}
}
System . out. println ( "没变化:" + noChangeList) ;
System . out. println ( "-------------------------------" ) ;