Count items common to both the lists but with different prices | Java implementation to count items common to both the lists but with different prices ; details of an item ; function to count items common to both the lists but with different prices ; ' um ' implemented as hash table that contains item name as the key and price as the value associated with the key ; insert elements of ' list1' in ' um ' ; for each element of ' list2' check if it is present in ' um ' with a different price value ; required count of items ; Driver program to test above
import java . util . * ; class GFG { static class item { String name ; int price ; public item ( String name , int price ) { this . name = name ; this . price = price ; } } ; static int countItems ( item list1 [ ] , int m , item list2 [ ] , int n ) { HashMap < String , Integer > um = new HashMap < > ( ) ; int count = 0 ; for ( int i = 0 ; i < m ; i ++ ) um . put ( list1 [ i ] . name , list1 [ i ] . price ) ; for ( int i = 0 ; i < n ; i ++ ) if ( ( um . containsKey ( list2 [ i ] . name ) ) && ( um . get ( list2 [ i ] . name ) != list2 [ i ] . price ) ) count ++ ; return count ; } public static void main ( String [ ] args ) { item list1 [ ] = { new item ( " apple " , 60 ) , new item ( " bread " , 20 ) , new item ( " wheat " , 50 ) , new item ( " oil " , 30 ) } ; item list2 [ ] = { new item ( " milk " , 20 ) , new item ( " bread " , 15 ) , new item ( " wheat " , 40 ) , new item ( " apple " , 60 ) } ; int m = list1 . length ; int n = list2 . length ; System . out . print ( " Count ▁ = ▁ " + countItems ( list1 , m , clist2 , n ) ) ; } }