I'm trying to work with fractions in Java.
I want to implement arithmetic functions. For this, I will first require a way to normalize the functions. I know I can't add 1/6 and 1/2 until I have a common denominator. I will have to add 1/6 and 3/6. A naive approach would have me add 2/12 and 6/12 and then reduce. How can I achieve a common denominator with the least performance penalty? W...
The contract of equals with regards to null, is as follows:
For any non-null reference value x, x.equals(null) should return false.
This is rather peculiar, because if o1 != null and o2 == null, then we have:
o1.equals(o2) // returns false
o2.equals(o1) // throws NullPointerException
The fact that o2.equals(o1) throws NullPointerException is a good thing, because it alerts us of progra...
I had a question which is pretty easy if you know the answer I guess. I read about sorting ArrayLists using a Comparator but somehow in all of the examples people used compareTo which according to some research is a method working on Strings...
I wanted to sort an ArrayList of custom objects by one of their properties: a Date object
(getStartDay()). Normally I compare them by item1.getStartDat...
What is the difference between Java's compare() and compareTo() methods? Do those methods give same answer?
Is it possible to match on a comparison using the pattern matching system in Scala?
For example:
a match {
case 10 => println("ten")
case _ > 10 => println("greater than ten")
case _ => println("less than ten")
}
The second case statement is illegal, but I would like to be able to specify "when a is greater than".
I have a list of objects I need to sort on a field, say Score. Without giving much thought I wrote a new class that implements Comparator, that does the task and it works.
Now looking back at this, I am wondering if I should have instead have the my class implement Comparable instead of creating a new class that implements Comparator. The score is the only field that the objects will be order...
Comparable contract specifies that e.compareTo(null) must throw NullPointerException.
From the API:
Note that null is not an instance of any class, and e.compareTo(null) should throw a NullPointerException even though e.equals(null) returns false.
On the other hand, Comparator API mentions nothing about what needs to happen when comparing null. Consider the following attempt of a generic...
I have a program I ported from C to Java. Both apps use quicksort to order some partitioned data (genomic coordinates).
The Java version runs fast, but I'd like to get it closer to the C version. I am using the Sun JDK v6u14.
Obviously I can't get parity with the C application, but I'd like to learn what I can do to eke out as much performance as reasonably possible (within the limits of the...
The API for the Java Set interface states:
For example, some implementations prohibit null elements, and some have restrictions on the types of their elements
I am looking for a basic Set implementation that does not require ordering (as ArrayList provides for the List interface) and that does not permit null. TreeSet, HashSet, and LinkedHashSet all allow null elements. Additionally, Tree...
Apparently, Alexander Stepanov has stated the following in an interview:
“I find OOP technically unsound. It attempts to decompose the world in terms of interfaces that vary on a single type. To deal with the real problems you need multisorted algebras - families of interfaces that span multiple types.” [Emphasis added.]
Ignoring his statement regarding OOP for a moment, what are "multisorted...
BigInteger bigInteger = ...;
if(bigInteger.longValue() > 0) { //original code
//bigger than 0
}
//should I change to this?
if(bigInteger.compareTo(BigInteger.valueOf(0)) == 1) {
//bigger than 0
}
I need to compare some arbitary BigInteger values. I wonder which approach is correct. Given the above code which one should be used? The original code is on the top.. I am thinking to...
When implementing compareTo(), does the degree of "difference" need to be taken into account?
For instance, if I have 3 objects, C1, C2, and C3, such that C1 < C2 < C3.
Should C1.compareTo(C2) return an integer that is less than C2.compareTo(C3)?
The documentation for the Comparable interface doesn't seem to specify one way or another, so I'm guessing the degree doesn't matter, but it...
Is quaternion comparison possible? I'm writing a Java class of Quaternions and I want to implement the Comparable interface to use the Collections.sort(List<Quaternion>) facility. I'm not expert at math, I really don't understand the things I read about Quaternions. So, can anyone tell me can I override the compareTo method for Quaternions and how? Thanks
My class declarition:
public cl...
Does any one know of some kind of Comparator factory in Java, with a
public Comparator getComparatorForClass(Class clazz) {}
It would return Comparators for stuff like String, Double, Integer but would have a
public void addComparatorForClass(Class clazz, Comparator comparator) {}
For arbitrary types.
OK, so it's easy to name an interface (or class for that matter) if you can easily think of a noun: User, Window, Database, Stream, etc.
What about an adjective or adjective concept? e.g. something that has a timestamp (HasTimestamp, Timestamped, Timestampable...?) or something that is tracked or watched (Trackable, IsTracked, Watchable, IsWatched...?)
If an object holds a unique primary key, what interfaces does it need to implement in order to be collection friendly especially in terms of being efficiently sortable, hashable, etc...?
If the primary key is a string, how are these interfaces best implemented?
Thanks!
How to compare and sort different type of objects using java Collections .Below is the use case:
For example DOG,MAN,TREE, COMPUTER,MACHINE - all these different objects has a common property say "int lifeTime". Now I want to order these obects based on the lifeTime property
Thx
I am using a findbugs in an ANT script and I can't figure out how to fix two of my errors. I have read the documentation, but don't understand. Here are my errors and the code that goes with them:
Error 1: Test for floating point equality. (FE_FLOATING_POINT_EQUALITY)
private boolean equals(final Quantity other) {
return this.mAmount == convertedAmount(other);
}
Error 2: EQ_COMPARETO...
I cant find any sorting function in the java API for vectors.
Collections.sort is only for List and not for Vector
I dont want to write my own sorting algo because I think java should implement this
Im looking for something like, class ClassName implements Comparator ...
ClassName cn; sort(cn);
Suppose you have an interface defined in C#. What is the easiest method to find all classes that provide an implementation of the interface?
The brute force method would be to use "Find References" in Visual Studio and manually look through the results to separate out the usages from the implementations, but for an interface in a large codebase that is heavily referenced with relatively few i...
Suppose I need TreeSet with elements sorted with some domain logic. By this logic it doesn't meter order of some elements that doesn't equal so compare method can return 0, but in this case I couldn't put they in TreeSet.
So, question:
what disadvantages I'll have from code like this:
class Foo implements Comparable<Foo>{}
new TreeSet<Foo>(new Comparator<Foo>(){
@Overrid...