I'm want a IDictionary<float, foo> that returns the larges values of the key first.
private IDictionary<float, foo> layers = new SortedDictionary<float, foo>(new DescendingComparer<float>());
class DescendingComparer<T> : IComparer<T> where T : IComparable<T>
{
public int Compare(T x, T y)
{
return -y.CompareTo(x);
...
For a class assignment, we can't use any of the languages bultin types, so I'm stuck with my own list. Anyway, here's the situation:
public class CrazyStructure <T extends Comparable<? super T>> {
MyLinkedList<MyTree<T>> trees; //error: type parameter MyTree is not within its bound
}
However:
public class CrazyStructure <T extend...
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 c...
If I have a list of elements I would like to sort, Java offers two ways to go about this.
For example, lets say I have a list of Movie objects and I’d like to sort them by title.
One way I could do this is by calling the one-argument version of the static java.util.Collections.sort( ) method with my movie list as the single argument....
I have a method which needs a Comparator for one of its parameters. I would like to pass a Comparator which does a normal comparison and a reverse comparator which does in reverse.
java.util.Collections provides a reverseOrder() this is good for the reverse comparison, but I could not find any normal Comparator.
The only solution what ...
Hi, all.
I've got a class, "Accumulator", that implements the Comparable compareTo method, and I'm trying to put these objects into a HashSet.
When I add() to the HashSet, I don't see any activity in my compareTo method in the debugger, regardless of where I set my breakpoints. Additionally, when I'm done with the add()s, I see severa...
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!
...
Hello, everyone!
As far as I know, things such as SortedMap or SortedSet, use compareTo (rather than equals) on Comparable<?> types for checking equality (contains, containsKey).
But what if certain types are equatable by concept, but not comparable?
(Hash codes, memory addresses, ...)
I have to declare a Comparator<?> and override th...
I need a comparator as part of a strategy pattern that can either use the natural ordering of the objects or some custom ordering. For the natural ordering case, I wrote a simple comparator:
private static class NaturalComparator<T extends Comparable<? super T>> implements Comparator<T> {
@Override
public int compare(T o1, T o2)...
In Java, how would I use generics to create a max function that takes as parameters two Comparable objects of the same type and returns the larger one?
I tried:
public static <T extends Comparable> T max(T obj1, T obj2)
{
return ( ((obj1).compareTo(obj2) >= 0) ? obj1 : obj2);
}
(It returns obj1 if they are both equal.)
The metho...
need an example on how to use a comparable class on a hashset to get an ascending order?
Let say we have a hashset like:
HashSet hs=new HashSet();
How can i get the hs to be in a ascending order??
...
this piece of code is supposed to consider flows in both direction as one flow.
for example:
srcAddr,dstAddr,srcPort,dstPort
192.168.1.65, 217.174.16.1, 123456,80
should be the same as
217.174.16.1, 192.168.1.65,80,123456
Another Example:
192.168.1.65, 217.174.16.1, 12345, 80, TCP
217.174.16.1, 192.168.1.65, 80, 12345, TCP
192.168...
I'm making a TreeSet of Point2D.Float type, creating it with a custom Comparable class that implements compare() of Point2D type. However, on calling "contains" on the TreeSet, I get a classcast error: java.lang.ClassCastException: java.awt.geom.Point2D$Float cannot be cast to java.lang.Comparable
The set is created as so:
private Coor...
I am able to compare Strings fine, but would like to know how I can rank floating point numbers?
getChange() returns a String. I want to be able to sort descending. How can I do this?
UPDATE:
package org.stocktwits.helper;
import java.util.Comparator;
import org.stocktwits.model.Quote;
public class ChangeComparator implements Compa...
I can use it to sort by emp id but I'm not sure if it is possible to compare strings. I get an error the operator is undefined for strings.
public int compareTo(Emp i) {
if (this.getName() == ((Emp ) i).getName())
return 0;
else if ((this.getName()) > ((Emp ) i).getName())
return 1...
I am calling a specific class using only its interface. The problem is, the class itself implements Comparable, but because I am referring to the class via a different interface, the compiler does not know it implements Comparable. I'm sure there is an easy solution to this... but I just can't think of it right now.
...
Is there any Compareable<Collection<T extends Compareable<T>>> implementation in Java (which behaves as C++'s std::list<T>::operator<() or std::set<T>::operator<())?
Edit: Comparator would make more sense...
...