public final Comparator<String> ID_IGN_CASE_COMP = new Comparator<String>() {
public int compare(String s1, String s2) {
return s1.compareToIgnoreCase(s2);
}
};
private Map< String, Animal > _animals = new TreeMap< String, Animal >(ID_IGN_CASE_COMP);
My problem is, how to use method get(id) ignoring th...
List<Question> questions = new ArrayList<Question>();
questions.addAll(getAllQuestions()); //returns a set of Questions
Collections.sort(questions, new BeanComparator("questionId")); //org.apache.commons.beanutils.BeanComparator
Under Java 1.5, the above works fine except that the 'new BeanComparator("questionId")' generates an uncheck...
I'm developing a Java application and am new to using TreeMap. The program needs to keep track of the number of occurrences of each word in a text file. However, I'm having trouble putting my data into the TreeMap.
It works fine when I use the same exact code to put the data into a HashMap, but I need the data to be sorted by the value....
Java: How do I perform list operations with different definitions of equals?
I have two lists of generic POJOs. I need to perform some set operations on the lists based on different ways of comparing the POJOs within the lists.
For example, if my POJO had the following structure:
public class GenericPojo {
private String id;
p...
Hello,
Is it possible to serialize a TreeMap with a comparator??
I've tested and it serializes well a treemap without comparator, when you add the comparator, it throws an exception.
If I declare comparator as transient, it still doesn't work. It only works if I make every tree map transient but it doesnt serialize the trees in that c...
Hello,
Is it possible to use a Comparator without implementing the Comparable class? For instance, if I had the following:
MyClass {
Comparator comp;
OrderedListInheritance(Comparator c) {
this.comp = c;
}
}
Could I then use comp to compare two objects? If so, how would I go about doing that?
Thanks...
...
I have a bunch of objects of a class Puzzle. I have overridden equals() and hashCode(). When it comes time to present the solutions to the user, I'd like to filter out all the Puzzles that are "similar" (by the standard I have defined), so the user only sees one of each.
Similarity is transitive.
Example:
Result of computations:
A ...
Is there any way I can compare values like this?
if (ci.getNumber() == (6252001|5855797|6251999)) { ... }
...
We're looking at different methods to sort the objects/elements in an array, the thing that doesn't make sense to me is how the actual sorting is done. I guess the big point of confusion is how can the "sort" method be effective if it only compares one object against another?
If there are values a, g, b, d, z, s, h in the array im not ...
Hello all,
I've been looking around to see if I find something to help me with my problem, but no luck until now. I've got the following classese:
public interface ISort<T> {
public List<T> sort(List<T> initialList);
}
public abstract class Sort<T> implements ISort<T> {
private Comparator<? super T> comparator;
p...
I'm implementing Dijkstra's on a board of tiles. I want to store all the tiles in a Priority Queue, sorted by their distance from the starting location. In Java, this would be something like:
Queue<Point> pq = new PriorityQueue<Point>(new Comparator() { /* sort by distance from start */ });
What would the equivalent by in C# XNA? C# ha...
I understand the get and put principle for collections: if a method takes in a collection that it will write a type T to, the parameter has to be Collection<? super T>, whereas if it will read a type T from, the parameter has to be Collection<? extends T>.
But could someone please explain the Collections.max() signature:
public static ...
I'm trying to sort my dynamic columns in a cross tab according to some custom scheme.
In the docs I found mention of comparatorExpression: Crosstab group bucket comparator expression. The result of this expression is
used to sort the buckets, in ascending or descending order. If no comparator
expression is specified, the natural order...
Hi,
I am trying to sort a buffer full of variable-length records alphabetically in C++. I previously asked how to implement this, and was told to sort an array of pointers to the records. I set up an array of pointers, but realized that each pointer points to the beginning of a record, but there is no way of it knowing when the record s...
Hi guys i've never written a comparator b4 and im having a real problem. I've created a hashtable.
Hashtable <String, Objects> ht;
Could someone show how you'd write a comparator for a Hashtable? the examples i've seen overide equals and everything but i simply dont have a clue. The code below is not mine but an example i found, the ...
Hi,
We have some code wich sorts a list of addresses based on the distance between their coordinates. this is done through collections.sort with a custom comparator.
However from time to time an adress without coordinates is in the list causing a NullPointerException. My initial idea to fix this was to have the comparator return 0 as d...
I have a List where element is:
struct element {
double priority;
int value;
}
How can I implement my own comparer which allow me sort List by priority ? I try with SortredList... but it don't allow douplicated keys :(
Big thanks for help!
...
In C# I need to split a string (a log4j log file) into array elements based on a particular sequence of characters, namely "nnnn-nn-nn nn:nn:nn INFO". I'm currently splitting this log file up by newlines, which is fine except when the log statements themselves contain newlines.
I don't control the input (the log file) so escaping them ...
Hi,
I'm trying order pairs of integers ascendantly where a pair is considered less than another pair if both its entries are strictly less than those of the other pair, and larger than the other pair if both its entries are strictly larger than those of the other pair. All other cases are considered incomparable.
They way I want to sol...
Hi,
I have a list of Point objects, (each one with x,y properties) and would like to find the left-most and right-most points. I've been trying to do it with find_if, but i'm not sure its the way to go, because i can't seem to pass a comparator instance. Is find_if the way to go? Seems not. So, is there an algorithm in <algorithm> to ac...