We help IT Professionals succeed at work.

Compare Hashmap keys values

ellandrd
ellandrd asked
on
7,034 Views
Last Modified: 2008-01-09
i have a hashmap made up of pixel values (key) with the number of times that pixel values appeared.

the only compare method i know of is the String compare method so how would i compare keys in a hashmap?
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
What type (class) are the keys?
CERTIFIED EXPERT
Top Expert 2016
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
>>if (key.equals(otherKey))

equal wouldnt really work in my case as i need to like check if the values are similar or near enough the same...

e.g im comparing 2 pixels and there values and freqencies are:


234 123567
232 345673

Well then by looking at these two values, i can see that they are similar (in colour range) so then i carry on my processing of the algorithm...

i think

if (key.equals(anotherKey)) will just do exact match which isnt what i want...

Im gonna look into objects suggestions now...  
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
taking the absolute value of the difference between the two values would give you an indication how 'close' they are
CERTIFIED EXPERT
Top Expert 2016

Commented:
You can override equals to do what you want
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> You can override equals to do what you want

You should definitely *not* do that. equals() has a very strict contract.
CERTIFIED EXPERT
Top Expert 2016

Commented:
I meant override compareTo sorry
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> I meant override compareTo sorry

again, do *not* do that. compareTo() also has a strict definition of what it should return.
see the javadoc for details
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
you copuld encapsulate the code I posted above in your own *new* method if you wished. Such that the method returned whether two values were considered close. Up to how you want to use it, probably not necessary though.
CERTIFIED EXPERT
Top Expert 2016

Commented:
Using a custom Comparator would be better
CERTIFIED EXPERT
Top Expert 2016
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
ok i know this is off topic but im putting my pixel values and freqencies into a hashmap like:

public Map<Integer, Integer> getColourMap() {
        Map<Integer, Integer> colourMap = new HashMap<Integer, Integer>();
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                int pixelValue = srcImage.getRGB(x, y);
                Integer numPixels = colourMap.get(pixelValue);
                colourMap.put(pixelValue, numPixels == null ? 1 : numPixels + 1);
            }
        }
        return colourMap;
    }

this is fine and works but when i print the contents of colourMap i get:

{-9010293=2, -5327945=4, -10327172=1, -15000296=1, -9076339=2, -12565686=3, ......}

what does this mean or stand for?


now back to the actualy topic...

objects:

>>taking the absolute value of the difference between the two values would give you an indication how 'close' they are

will by reading how colourclustering works, you are on the right path.

the algorithm compares each pixel value and if they are similar, the similar pixels values are multiplied by there frequency and the two total sums are added and divided by 2 to make a new single pixel value.

CEHJ:

>>What type (class) are the keys?

the keys are of type integer so im comparing int's and that is why im asking what does this mean:

{-9010293=2, -5327945=4, -10327172=1, -15000296=1, -9076339=2, -12565686=3, ......}

when i print out the colourMap contents...

Author

Commented:
ooops sorry CEHJ didnt see your comment made at:

Date: 02/19/2006 10:22AM GMT

will check it out now...
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> what does this mean or stand for?

they are the key/value pairs in your map

Author

Commented:
Mmmm ok... i though getting getRGB() return pixel value in format of 0 - 255? i was wrong...
CERTIFIED EXPERT
Top Expert 2016

Commented:
The negative output you get is the reason you should use Long

Author

Commented:
like i know to get red green blue i use:

int pixelValue = srcImage.getRGB(x, y);
int red = (int) (pixelValue & 0xFF);
int green = (int) ((pixelValue >> 8) & 0xFF);
int blue = (int) ((pixelValue >> 16) & 0xFF);

but would i get a total value for these three separate values?  if it is as simple as adding them together please knock me out... ;-)
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> i though getting getRGB() return pixel value in format of 0 - 255?

each component is from 0-255.
when comparing how close you need to compare the individual elements, and not the combined value.

> The negative output you get is the reason you should use Long

you don't need (or want) to use a long. int is fine.
CERTIFIED EXPERT
Top Expert 2016
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
CEHJ,

Have you ever actually done any work with colour or imaging?  You comments don't seem to make any sense.
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>but would i get a total value for these three separate values?  if it is as simple as adding them together please knock me out... ;-)

By calling getRGB() ;-)

Returns 0-0xFF FF FF FF, which is why you *must* use long unless you have very flexible logic ;-)
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> but would i get a total value for these three separate values?  if it is as simple as adding them together please knock me out...

no its not :)
there are various alrorithms you can use, see the article I posted above for a discussion.


Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> By calling getRGB() ;-)

unnecessary

> Returns 0-0xFF FF FF FF, which is why you *must* use long unless you have very flexible logic ;-)

that doesn't even make sense.

Author

Commented:
yes just looked over article - the Euclidean distance is mentioned in my paper that im following...

http://www.myjavaserver.com/~seandelaney/spieEI05_cmtbg.pdf

under 'colour clustering'...
CERTIFIED EXPERT
Top Expert 2016

Commented:
You might want to ignore the alpha channel though, in which case

val = val & 0xFFFFFF;
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
is the euclidean distance sufficient for your needs?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
well what im trying to do is get the three dominant colors based on the color map of the picture.

i think we are heading off the topic of this thread - i will split the points for this one and start a new thread based on 'getting the three dominant colors based on the color map of the picture'.

i think ye have provided a solution to compare values which what this thread is about but ending up leading into a different problem/discussion...

all agree?

ellandrd

Author

Commented:
objects:

>>http://www.uk-dave.com/projects/java/colourtracker.php

very very helpful! esp for the euclidean distance stuff... i understand that formula but still there is bits im not clear on but we can discuss this in a new thread.. (if you want?)
CERTIFIED EXPERT
Top Expert 2016

Commented:
If you ignore alpha:

7766923=2,11449271=4,6450044=1,1776920=1,7700877=2,4211530=3

You need a frequency table, but of course 'dominance' could mean one thing mathematically and another visually

Author

Commented:
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>could mean one thing mathematically and another visually

Although if you have an indexed colour model then that distinction evaporates

Author

Commented:
>>distinction evaporates

eh? how / what do you mean?
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)

Not quite sure how the accepted answer comes about though with its contradictions:

>>You chould use a class that implements Comparable for your keys.

>>You should definitely *not* do that. equals() has a very strict contract.

============================

>>In your case Integer or Long would seem suitable

>>you don't need (or want) to use a long.

I could go on...

objects, have you ever actually done any work with colour or imaging?

Author

Commented:
i just choose comments what i though was helpful in comparing values and not actually towards my problem...

after all the topic was about comparing values so i accepted / assigned comments that are helpful to compare values.

as i said near the near of the thread, we where getting off topic so i opened a new question....

Author

Commented:
typo - as i said near the end of the thread,

Author

Commented:
so are ye guys willing to give some guidence in this thread:

https://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21742692.html

ellandrd
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
The comments by CEHJ regarding using a COmparator appear irrelevant.
How exactly are you using a Comparator?
CERTIFIED EXPERT
Top Expert 2016

Commented:
How could a Comparator be irrelevant where ranking a collection is involved?

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.