{"id":23765,"date":"2015-07-26T11:37:22","date_gmt":"2015-07-26T06:07:22","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=23765"},"modified":"2015-07-26T12:15:39","modified_gmt":"2015-07-26T06:45:39","slug":"lets-listen-any-changes-made-in-collection-list-map-or-set","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/lets-listen-any-changes-made-in-collection-list-map-or-set\/","title":{"rendered":"Let&#8217;s listen any changes made in Collection (List, Map or Set)"},"content":{"rendered":"<p>Have you ever wondered how come your Collection is being updated and ended up spending time on debugging? We are going to see how can we get to know immediately if our Collection is altered. In this blog, we will do this using <em>ObservableList<\/em> for &#8216;List&#8217; and same can be applied for Map and Set as well.<\/p>\n<p>Here goes code to implement this :<\/p>\n<p>[java]<\/p>\n<p>import java.beans.*<br \/>\ndef event<br \/>\ndef listener = {<br \/>\nif (it instanceof ObservableList.ElementEvent) {<br \/>\nevent = it<br \/>\nlogEvent(event)<br \/>\n}<br \/>\nevent = null<br \/>\n} as PropertyChangeListener<\/p>\n<p>def logEvent (ObservableList.ElementEvent event) {<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0println &quot;New element ${event.newValue}, ${event.changeType} at Index ${event.index} and replaced ${event.oldValue}&quot;\u00a0<br \/>\n}<\/p>\n<p>\/* Declare our list and add listener to listen any change*\/<br \/>\ndef observableList = [1, 2, 3] as ObservableList<br \/>\nobservableList.addPropertyChangeListener(listener)[\/java]<\/p>\n<p>If we don&#8217;t need an explanatory log then above block of code can be rewritten as :<\/p>\n<p>[java]<\/p>\n<p>def observableList = [1, 2, 3] as ObservableList<br \/>\nobservableList.addPropertyChangeListener({<br \/>\n\/*We can use newValue(), oldValue()and few more methods with &#8216;it&#8217; but not changeType() and index() as these are in child class of PropertyChan geListener*\/<br \/>\n})[\/java]<\/p>\n<p>Now it&#8217;s time to alter our list and see the change:<\/p>\n<p>[java]observableList.add 42<br \/>\nobservableList.remove(2)<br \/>\nobservableList.putAt(2, 10)<br \/>\nobservableList.addAll([1, 2, 3, 4])<br \/>\nobservableList.clear()[\/java]<\/p>\n<p>Result (For first snippet of code):<\/p>\n<p>[java]New element 42, ADDED at Index 3 and replaced null<br \/>\nNew element null, REMOVED at Index 2 and replaced 3<br \/>\nNew element 10, UPDATED at Index 2 and replaced 42<br \/>\nNew element java.lang.Object@19d9744, MULTI_ADD at Index 2 and replaced java.lang.Object@1361c3f<br \/>\nNew element java.lang.Object@19d9744, CLEARED at Index 0 and replaced java.lang.Object@1361c3f<br \/>\n[\/java]<\/p>\n<p>Hope it helps \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever wondered how come your Collection is being updated and ended up spending time on debugging? We are going to see how can we get to know immediately if our Collection is altered. In this blog, we will do this using ObservableList for &#8216;List&#8217; and same can be applied for Map and Set [&hellip;]<\/p>\n","protected":false},"author":230,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0},"categories":[1],"tags":[9],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/23765"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/230"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=23765"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/23765\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=23765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=23765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=23765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}