public static NumberList delta ( Map < String , Object > currentMap , Map < String , Object > previousMap ) { LinkedHashMap < String , Long > values = new LinkedHashMap < String , Long > ( currentMap . size ( ) ) ; if ( currentMap . size ( ) != previousMap . size ( ) ) { throw new IllegalArgumentException ( "Maps must have the same keys" ) ; } for ( Entry < String , Object > k : currentMap . entrySet ( ) ) { Object v = k . getValue ( ) ; Number current = getNumber ( v ) ; Object p = previousMap . get ( k . getKey ( ) ) ; Number previous = null ; if ( p == null ) { previous = 0 ; } else { previous = getNumber ( p ) ; } long d = ( current . longValue ( ) - previous . longValue ( ) ) ; values . put ( k . getKey ( ) , d ) ; } return new NumberList ( values ) ; }
This assumes both maps contain the same keys . If they don t then keys will be lost .