Jony Kale
Jony Kale

Reputation: 979

Java removing object from hashmap

public Map<Object> map = new HashMap<Object>();


map.add(new Object ("a", "b"));


public class Object {

    private String a;
    private String b;
    private String c;

    public Object(String a, String b) {
        this.a = a;
        this.a = b;
        this.c = "" + a + b + "";
    }

    public String getA() {
        return a;
    }

    public String getB() {
        return b;
    }

    public String getC() {
        return c;
    }   
}   

I have a hashmap, basically Object.getC() should be a + b in a string. and in some other class, I need to get the c value and if that hashmap collection index has the exact same c value, it will delete the index:

public static void deleteChest() {
    for (int i = 0; i < data.size(); i++) {
        if (data.get(i).getItems().length == 0) {
            Object c = new Object(data.get(i).getA(), data.get(i).getB());
            map.remove(c);
        }
    }
}

data hashmap should have the same index number as the map hashmap, so if you're wondering what is it.

Basically, I loop through all data hashmap items (to get the A, B and then match if C is inside the other hashmap (map)).

Now the question time

How can I check if an object is contained (exists) or delete it (not by index, but by key)? How can I do this (Example above).

Upvotes: 0

Views: 11855

Answers (6)

Jorge Santos
Jorge Santos

Reputation: 414

The solution that work for me is:

map.remove("key");

Upvotes: 0

Ashish Jagtap
Ashish Jagtap

Reputation: 2819

You should have to change the public Map<Object> map = new HashMap<Object>(); field to public Map<String,Foo> map = new HashMap<String,Foo>(); and you should change the name of your class from Object to Foo as 'mre' suggestion.

Foo Class

public class Foo {

    private String a;
    private String b;
    private String c;

    public Foo(String a, String b) {
        this.a = a;
        this.a = b;
        this.c = getKey();
    }

    public String getA() {
        return a;
    }

    public String getB() {
        return b;
    }

    public String getC() {
        return c;
    }   

    public String getKey() {
        return "" + a + b + "";
    }

    @Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((a == null) ? 0 : a.hashCode());
    result = prime * result + ((b == null) ? 0 : b.hashCode());
    result = prime * result + ((c == null) ? 0 : c.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Foo other = (Foo) obj;
    if (a == null) {
        if (other.a != null)
            return false;
    } else if (!a.equals(other.a))
        return false;
    if (b == null) {
        if (other.b != null)
            return false;
    } else if (!b.equals(other.b))
        return false;
    if (c == null) {
        if (other.c != null)
            return false;
    } else if (!c.equals(other.c))
        return false;
    return true;
}
}  

and you can add objects in map as follows

map.put(new Foo("a", "b").getKey(),new Foo("a", "b"));

now you deleteChest() method will be like

public static void deleteChest() {
        Foo foo = null;  
        for (int i = 0; i < data.size(); i++) {
            foo =  map.get(new Foo(data.get(i).getA(), data.get(i).getB()).getKey())   
            if( foo != null) {
                map.remove(foo.getKey());
            }
        }
    }

hope this will solve your problem..

Upvotes: 1

AlexR
AlexR

Reputation: 115338

First, you code cannot be compiled for several reasons.

  1. You called your class Object. FYI java.lang.Object is a base class for all classes. Since real class Object belongs to package java.lang you do not have to import it and therefore it is always available. I do not understand how compiler can solve this naming collision.

  2. Interface Map has 2 parameters: K for key and V for value. Therefre line

public Map<Object> map = new HashMap<Object>();

cannot be compiled. Change it to

public Map<Object, Object> map = new HashMap<Object, Object>();

Or, better use real parameters instead of Object.

  1. Map interface has method put that accepts 2 parameters. Therefore the following line cannot be compiled too.

map.add(new Object ("a", "b"));

Bottom line if you want to use map that associates groups of string use code like following:

Map<String, String> map = new HashMap<>();
map.put("a", "b");

To remove value by key use:

map.remove("a");

To retrieve value use String v = map.get("a")

To retrieve all values use either map.keySet() or map.entrySet().

Upvotes: 0

Indra Yadav
Indra Yadav

Reputation: 600

Try this

if(map.containsKey(key)){
            map.remove(map.get(key));
        }

Upvotes: -1

The first problem you have is that Map use key to retrieve value.

Set<Object> set = new HashSet();

Then when you are using Set or Map, your class must override hashcode and equals methods. If the do not do that the Set will not know how to properly compare them, to perform any operation.

This is why when you create a new Object(a,b,c) it set can not find them as it use the default value of hashcode with is not related to type members.

Upvotes: 1

mariosangiorgio
mariosangiorgio

Reputation: 5543

You should define the equals and hashCode method for your class.

Otherwise you fall back to the default implementation which checks for object identity, i.e. if the two references points to the same memory location instead of pointing to two objects containing the same data.

Upvotes: 4

Related Questions