0

I am having a hashMap which stores my values like that:

{10997=Event [Numb=0007449001, date=07.02.2008], 10998=Event [Numb=0007449001, date=08.04.2008], ...

As you can see the key is a integer and the value is an Event Object which has a numeric value and a date, which is saved as a String.

I want to compare the numeric value of the map to another value with an if statment.

I tried:

if(numericVal==eventMap.get(i).toString()) {

However that does not really work. How to get the value easily out of the HashMap and of the Object?

I appreciate your answer!

UPDATE

By easily I mean performance orientated, because I have to process a lot of values.

user2051347
  • 1,609
  • 4
  • 23
  • 34
  • Do you have a way to get the numeric value from your Event object? Something like eventMap.get(i).getNumericValue() ? – gtgaxiola Jan 09 '14 at 15:45
  • you want to compare numerical value with what of map? Is it `key` values of map or `Numb` of `Event`? – Narendra Pathai Jan 09 '14 at 15:45
  • All `Map`s have a method called `keySet()` that returns a `Set` of keys. You could then iterate through them. However, if all you want to do is tell if a key is in your Map, then you can use the `contains()`. Being a `HashMap` it should be the most optimal way to see if your `HashMap` contains a particular key. – CodeChimp Jan 09 '14 at 15:46
  • 1
    `==` seems not right here `== eventMap.get(i).toString()`. Also what is your `Event` class? Does it have methods to get value of `Numb`? – Pshemo Jan 09 '14 at 15:46
  • 1
    Use method `equals` instead of `==`. – Nicolai Jan 09 '14 at 15:47

2 Answers2

1

first off; do not compare Strings with == use equalsIgnoreCase() instead (or just equals() if the casing matters)

second; consider giving your Event a compareTo() and while you are at it an equals() and a hashCode().

Once you got the components in place and why they should be there, you'll realize what you want to do is possible in a much more elegant way.

posdef
  • 6,498
  • 11
  • 46
  • 94
1

If I understand correctly you are trying to compare the actual numeric value within your EventObject

Therefore your EventObject has to have an accesor to it.

Lets say your EventObject looks something like:

public class EventObject {    
    private String numericValue;
    private String date;

    public EventObject(String numericValue, String date) {
        this.numericValue = numericValue;
        this.date = date;
    }

    public String getNumericValue() {
        return numericValue;
    }

    public void setNumericValue(String numericValue) {
        this.numericValue = numericValue;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

}

From there now accessing the numericValue from a HashMap should be as easy as:

HashMap<Integer,EventObject> map = new HashMap<>();
map.put(10997,new EventObject("0007449001", "07.02.2008"));

int keyMap = 10997;  //for sake of making obvious this is Key and not numericValue 
String numValue = hashMap.get(keyMap).getNumericValue();

A comparison then can be done by:

if("0007449001".equals(map.get(10997).getNumericValue())) {
    System.out.println("The Date I have is: " + map.get(10997).getDate());
}

Which will get you:

The Date I have is: 07.02.2008
gtgaxiola
  • 9,241
  • 5
  • 42
  • 64