Thursday, October 8, 2015

java - How to remove a key from HashMap while iterating

MainActivity.java

package com.cfsuman.me.javaexamples;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_ACTION_BAR);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Set background color for action bar
        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.RED));

        // Get widget reference from XML layout
        RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
        TextView tv = (TextView) findViewById(R.id.tv);

        // Empty the TextView
        tv.setText("");

        // Initialize a new HashMap
        Map<Integer,String> colors = new HashMap<>();
        // Put some entry to the map
        colors.put(1,"Red");
        colors.put(2,"Green");
        colors.put(3,"Blue");
        colors.put(4,"Black");
        colors.put(5,"Yellow");

        // Iterate over HashMap and remove an entry which value is 'Blue'
        tv.setText("Colors : ");
        Iterator it = colors.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry pair = (Map.Entry) it.next();
            tv.setText(tv.getText() + "\n" + pair.getKey() +  " : " + pair.getValue());
            if(pair.getValue().equals("Blue")){
                it.remove();
            }
        }

        // Iterate over HashMap after removing a pair
        tv.setText(tv.getText() + "\n\nColors (after removing 'Blue'): ");
        Iterator itr = colors.entrySet().iterator();
        while(itr.hasNext()){
            Map.Entry pair = (Map.Entry) itr.next();
            tv.setText(tv.getText() + "\n" + pair.getKey() + " : " + pair.getValue());
        }

        // Another way to remove a pair from HashMap while iterating
        // For loop to remove an entry from HashMap
        for(Iterator<Map.Entry<Integer,String>> ite = colors.entrySet().iterator(); ite.hasNext();)
        {
            Map.Entry<Integer,String> pair = ite.next();
            if(pair.getKey()==2){
                // If key == 2 the remove the pair from HashMap
                ite.remove();
            }
        }

        // Iterate over HashMap after removing an entry
        tv.setText(tv.getText() + "\n\nColors (after removing key '2'): ");
        Iterator iterator = colors.entrySet().iterator();
        while(iterator.hasNext()){
            Map.Entry pair = (Map.Entry) iterator.next();
            tv.setText(tv.getText() + "\n" + pair.getKey() + " : " + pair.getValue());
        }
   }
}
More android examples