结论:
覆盖发生条件:多个Map中的key—value必须完全一样,Set集合中后面的map会覆盖前面的Map。 如果不想被覆盖,必须保证Map中的value与其它map的value不一样。
会覆盖测试
HashSet < Map < String , Object > > value12 = new HashSet < > ( ) ;
Map < String , Object > map5 = new HashMap < > ( ) ;
Map < String , Object > map6 = new HashMap < > ( ) ;
Map < String , Object > map7 = new HashMap < > ( ) ;
Map < String , Object > map8 = new HashMap < > ( ) ;
map5. put ( "111" , 111 ) ;
map5. put ( "222" , 222 ) ;
map5. put ( "333" , 333 ) ;
map5. put ( "444" , 444 ) ;
map6. put ( "111" , 111 ) ;
map6. put ( "222" , 222 ) ;
map6. put ( "333" , 333 ) ;
map6. put ( "444" , 444 ) ;
map7. put ( "111" , 111 ) ;
map7. put ( "222" , 222 ) ;
map7. put ( "333" , 333 ) ;
map7. put ( "444" , 444 ) ;
map8. put ( "111" , 111 ) ;
map8. put ( "222" , 222 ) ;
map8. put ( "333" , 333 ) ;
map8. put ( "444" , 444 ) ;
value12. add ( map5) ;
value12. add ( map6) ;
value12. add ( map7) ;
value12. add ( map8) ;
Iterator < Map < String , Object > > iterator = value12. iterator ( ) ;
System . out. println ( value12. size ( ) ) ;
while ( iterator. hasNext ( ) ) {
Map < String , Object > next = iterator. next ( ) ;
System . out. println ( next) ;
}
输出
{ 111 = 111 , 222 = 222 , 333 = 333 , 444 = 444 }
不会覆盖测试
HashSet < Map < String , Object > > value12 = new HashSet < > ( ) ;
Map < String , Object > map5 = new HashMap < > ( ) ;
Map < String , Object > map6 = new HashMap < > ( ) ;
Map < String , Object > map7 = new HashMap < > ( ) ;
Map < String , Object > map8 = new HashMap < > ( ) ;
map5. put ( "111" , 111 ) ;
map5. put ( "222" , 222 ) ;
map5. put ( "333" , 333 ) ;
map5. put ( "444" , 444 ) ;
map6. put ( "111" , 111 ) ;
map6. put ( "222" , 222 ) ;
map6. put ( "333" , 777 ) ;
map6. put ( "444" , 555 ) ;
map7. put ( "111" , 111 ) ;
map7. put ( "222" , 222 ) ;
map7. put ( "333" , 999 ) ;
map7. put ( "444" , 444 ) ;
map8. put ( "111" , 111 ) ;
map8. put ( "222" , 222 ) ;
map8. put ( "333" , 333 ) ;
map8. put ( "444" , 444 ) ;
value12. add ( map5) ;
value12. add ( map6) ;
value12. add ( map7) ;
value12. add ( map8) ;
Iterator < Map < String , Object > > iterator = value12. iterator ( ) ;
System . out. println ( value12. size ( ) ) ;
while ( iterator. hasNext ( ) ) {
Map < String , Object > next = iterator. next ( ) ;
System . out. println ( next) ;
}
输出
3
{ 111 = 111 , 222 = 222 , 333 = 777 , 444 = 555 }
{ 111 = 111 , 222 = 222 , 333 = 333 , 444 = 444 }
{ 111 = 111 , 222 = 222 , 333 = 999 , 444 = 444 }