This note is for myself.
For some reason i keep forgot about this very useful method :(
To compare 2 floats value use method from Float class:
Float.compare(floatVariable1,floatVariable2);
Example:
float floatVariable1 = 8.88f;
float floatVariable2 = 1.14f;
int result = Float.compare(floatVariable1, floatVariable2);
if (result > 0) {
System.out.println(floatVariable1 + " is grater than " + floatVariable2);
} else if (result == 0) {
System.out.println(floatVariable1 + " and " + floatVariable2 + " are equal");
} else {
System.out.println(floatVariable2 + " is grater than " + floatVariable1);
}
DOMINIK SYMONOWICZ's blogs - FOREVER HUNGRY of knowledge, sarcasm, smiling, music, dancing, and food ...lots of food. This IT blog is a collection of notes with a simple solution with an explanation, added documentation, sarcastic comment and weird example. I also write about a lesson learnt from my mistakes. A non-it blog can be found here: Dominik Symonowicz's non-IT blog and website
Showing posts with label compare. Show all posts
Showing posts with label compare. Show all posts
8 December 2011
12 April 2011
How to compare enum with string in java?
Solution for java 5 and java 6
As we know to compare string to string we need use method
string1.compareTo(string2);
so what you need to do is convert Enum to String
EnumExample.ENUM1.toString();
So now,to compare enum with string you need
string1.compareTo(EnumExample.ENUM1.toString());
Simple :)
As we know to compare string to string we need use method
string1.compareTo(string2);
so what you need to do is convert Enum to String
EnumExample.ENUM1.toString();
So now,to compare enum with string you need
string1.compareTo(EnumExample.ENUM1.toString());
Simple :)
28 February 2011
How to use Comparator and Comparable in Java. Comparable Vs Comparator battle
So... I was told that using Comparable and Comparator in the same class is a bad .
I've done it,because ... Eclipse suggest me this silly idea. It obviously means that i didn't understand difference between them,so it was time for do little research and here are my notes:
Definitions:
Comparable:
A comparable is a interface and object that implement this interface is capable of comparing itself with another object. To implement this interfaced class must contain a method called compareTo.
Comparator:
A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances. This comparator class must implement the java.util.Comparator interface.
Look for below links for more complete information about Comparable and Comparator: (Where they will show example how to implementing sorting for List and other collections ):
java sorting - comparator vs comparable
javablogging - comparable vs comparator
I've done it,because ... Eclipse suggest me this silly idea. It obviously means that i didn't understand difference between them,so it was time for do little research and here are my notes:
Definitions:
Comparable:
A comparable is a interface and object that implement this interface is capable of comparing itself with another object. To implement this interfaced class must contain a method called compareTo.
Comparator:
A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances. This comparator class must implement the java.util.Comparator interface.
Look for below links for more complete information about Comparable and Comparator: (Where they will show example how to implementing sorting for List and other collections ):
java sorting - comparator vs comparable
javablogging - comparable vs comparator
Subscribe to:
Posts (Atom)