0
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).

5
  • 12
    Please don't call your class Object. You won't even know what hit you. Commented Sep 3, 2013 at 12:44
  • That's just an example. @SotiriosDelimanolis - But thanks to know. Commented Sep 3, 2013 at 12:45
  • Off topic: Using Object as class name is not a very good idea. It is better to not use name of classes already existing in the JDK Commented Sep 3, 2013 at 12:45
  • @JonyKale, use Foo instead Commented Sep 3, 2013 at 12:46
  • 1
    Are you sure you are using a HashMap and not a HashSet? Commented Sep 3, 2013 at 12:47

6 Answers 6

4

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

1

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..

Comments

0

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().

1 Comment

Let's say I want to have <String, ObjectClass> can I do map.remove(map.get(0).getC()) so it will print the key? 0 = the index
0

The solution that work for me is:

map.remove("key");

Comments

-1

Try this

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.