title: Send Data to Server only if there is a change in HashMap Data in Android
body: <p>I've been researching this logic from the past week but no luck. Can anyone help me on with it? So the problem where I got stuck is I'm using the beacon concept (Moko Beacon) using bindService where it Scans the beacons till the app has been destroyed. So I get the beacon data all the time.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity implements 
                MokoScanDeviceCallback {
  
  private MokoService mMokoService;
  private HashMap&lt;String, BeaconXInfo&gt; beaconXInfoHashMap;
  public ArrayList&lt;BeaconXInfo&gt; beaconXInfos;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
  
  bindService(intent, mServiceConnection, BIND_AUTO_CREATE);
      beaconXInfoHashMap = new HashMap&lt;&gt;();
      beaconXInfos = new ArrayList&lt;&gt;();
  
  }
  
  public void onScanDevice(DeviceInfo device) {
      final BeaconXInfo beaconXInfo = beaconXInfoParseable.parseDeviceInfo(device);
      if (beaconXInfo == null) {
          return;
      }
      beaconXInfoHashMap.put(beaconXInfo.mac, beaconXInfo);
      updateDevices();
  }
  
  private void updateDevices() {
  Collections.sort(beaconXInfos, new Comparator&lt;BeaconXInfo&gt;() {
          @Override
          public int compare(BeaconXInfo lhs, BeaconXInfo rhs) {
  
             Log.e("beaconXinfo", String.valueOf(beaconXInfos.toString())); 
  
              if (lhs.rssi &lt; rhs.rssi){
  
                  for (int i = 0 ; i &lt; beaconXInfos.size();i++){
  
                      if ((beaconXInfos.get(i).rssi &gt; -40)){
                          NotificationHelper notificationHelper = new NotificationHelper(MainActivity.this);
                          notificationHelper.CreateNotification(beaconXInfos.get(i).mac,"enter");
                      }else if ((beaconXInfos.get(i).rssi &lt; -40)){
                          NotificationHelper notificationHelper = new NotificationHelper(MainActivity.this);
                          notificationHelper.CreateNotification(beaconXInfos.get(i).mac,"exit");
                      }
                  }
              }
  
              return 0;
          }
      });
      }
  </code></pre>
  
  <p>The problem lies in the updateDevices() method where I get values continuously till I destroy the app. The log values are as below :</p>
  
  <pre><code>E/beaconXinfo: [BeaconXInfo{name='BeaconX', mac='F5:5E:B1:65:94:4B'}]
  E/beaconXinfo: [BeaconXInfo{name='BeaconX', mac='F5:5E:B1:65:94:4B'}, BeaconXInfo{name='UFO', mac='55:46:4F:D2:72:5A'}]
  E/beaconXinfo: [BeaconXInfo{name='BeaconX', mac='F5:5E:B1:65:94:4B'}, BeaconXInfo{name='UFO', mac='55:46:4F:D2:72:5A'}, BeaconXInfo{name='null', mac='C8:DE:FE:45:50:02'}] //change in the data
  </code></pre>
  
  <p>As you can see above there is a change in data in the last log I want to send the data to the server only when there is a change in data in the hashmap.</p>
  
  <p>So first I send the data "F5:5E:B1:65:94:4B" because there is only one value. Next, I need to compare this hashmap with the next hashmap where there's a change in the data called "55:46:4F:D2:72:5A". I need to send this data ignoring the "F5:5E:B1:65:94:4B".</p>
  
  <p>Like same, How to send only this "C8:DE:FE:45:50:02" value comparing with the previous hashmap to the server.</p>
  
  <p>So How to compare the first log of hashmap data with the next series of hashmap data where I can take only the latest value and send it to the server.</p>
  
  <p>I've researched a bunch of question before posting this like below:</p>
  
  <p><a href="https://stackoverflow.com/q/1879700/7352857">How to remove Duplicate value from arraylist in Android</a></p>
  
  <p><a href="https://stackoverflow.com/q/203984/7352857">How do I remove repeated elements from ArrayList?</a></p>
  
  <p><a href="https://stackoverflow.com/q/17812454/7352857">Remove duplicate values from HashMap in Java</a></p>
  
  <p>Maybe I might be confused with the above which I use in my code.</p>
  
  <p>If anything needs to be added into the code please comment below. Any suggestion and answers would be highly appreciated. Thanks in advance.</p>