comparator

How come generic type parameter says "extends" Comparable not "implements" ?

I tried to write generic function that remove the duplicate elements from array. public static <E extends Comparable<E>> ArrayList<E> removeDuplicate(E[] arr) { //do quicksort Arrays.sort(arr); ArrayList<E> list = new ArrayList<E>(); int i; for(i=0; i<arr.length-1; i++) { if(arr[i].co...

Search using two keys in an arrayList

Hi all, I would like to implement a fast search on an ArrayList of objects. These objects consist of an int oldId, int newId and int inList, among with other things. Now, I tried implementing a binary search using the Collections.binarySearch on my list, but the problem is I need to search using the oldId and inList, to get the newId f...

How to search in a Set (using a Comparator)

Hi, I want to search in a Set without iterating manually over the elments but there does not seem to be a method to do Collections.search(myset, target, new ComparatorThing()). Am I not seeing something? Thanks. Edit: I am searching for another field than the natural order of the elements. As a manual workaround I used the following...

writing "Comparator" in SQL

Hello, I have the following problem: find the highest row in a table A according to the following rules: Table A Columns: V_Date Date, Type int, H_Date Date 1) find the highest V_Date 2) if V_Dates are the same find the row with the highest Priority, where Priority is defined in Table B with columns Type int, Priority int 3) if V_Dat...

Using binarySearch with Comparator and regex

Hi. I am trying to write a quick search that searches a List<String> Instead of looping through the list and manually checking, I want to do this using binarySearch, but I am not sure how to do it. Old way: for(String s : list) { if(s.startsWith("contact.") return true; } Instead I would like something like this: Collection...

using comparators as embedded classes, the mother class (initialized in clone ) fields are not visible in the compare method

I have developed a server-to-server protocol called CCMN and different drop policies for the messages cached by each server. The PEERSIM simulator creates a template node with the CCMN protocol and then clones this template node. The CCMN class includes different data structures maintaining the states required to implement the drop po...

TreeSet and equals function

Hi, There is a Java bean object which has implemented equals function based on certain criteria (Criteria A). I have a requirement to identify unique objects based on another criteria (Criteria B). Since the equals function uses criteria A, I can not use HashSet. So I thought of using TreeSet with my custom Comparator which is based on ...

Comparator problem - Java

Hey guys, Quite new to Java so this is probably pretty straight forward. I want to sort an ArrayList in the class MediaLib based on the natural order of a specified key. I cant work out how to use my comparator (compareTo(MediaInterface, key)) which is in the Media class. Whats the best way to go about this? package assign1; import ja...

Help comparing float member variables using Comparators

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...

How to compare percentages that are strings?

I have a string "+1.29%" or "-1.29%" How can I convert this into a float or double so I can compare the values? This is how my comparator class looks like: package org.stocktwits.helper; import java.util.Comparator; import org.stocktwits.model.Quote; public class PercentChangeComparator implements Comparator<Quote> { ...

Sorting ArrayList Not Working Properly

I am trying to sort my ArrayList, but when I check the first item in my supposedly sorted array, it is incorrect. I am not sure why? Am I missing anything? Here is my Comparator: package org.stocktwits.helper; import java.util.Comparator; import org.stocktwits.model.Quote; public class NameComparator implements Comparator<Quote> { ...

Java: Reference from a non-static comparator to it's outer class field

I need to sort lists of objects with a non-static comparator that uses a value from it's outer object field. class A { public int x; public int y; public int z; public Comparator<A> scoreComparator = new Comparator<A>() { public compare(A o1, A o2) { // System.out.println("this: " + this); ...

java comparator for simple custom logic

How I can find line 4,1 and 6 in example below? And is the use of Collection.sort() with Comparator reasonable in this case? a - b - c - d 1.) 6 8 16 18 2.) 38 40 55 57 3.) 6 8 25 27 4.) 1 5 11 15 5.) 6 8 3 5 6.) 9 12 19 22 7.) 18 20 1 3 8.) 23 25...

Comparator as static field - interface or implementation?

I have a class that already has a 'natural' order, and wish to define a different Comparator that can be used similar to String.CASE_INSENSITIVE_ORDER - i.e., define it as an instantiated static field to be referred to when needed. With an interface Foo which is the actual comparison type (it will be Comparator<Foo>), I'm in favor of pu...

How do I initialize a std::set comparator?

I need to initialize some comparator of the new data type TType based on std::set with some object o of another class Object: typedef std::set <unsigned int, sortSet(o)> TType This declaration is otside the class (in header file). At the time of the declaration this object does not have to exist, it will be created later. class sortS...

Is this a correct way of sorting by title, position and then order by using a Comparator?

Consider this class. public class DynamicField implements Comparable<DynamicField> { String title; int position; int order; @Override public int compareTo(DynamicField o) { if(position < o.position) return -1; if(position > o.position) return 1; if(order < o.order) ...

comparing two TreeNode (or DefaultMutableTreeNode) objects in Java Comparator

Hello everyone! My goal is very simple today, I am trying to work out the proper way to implement compareTo (or the Comparable) interface for my class which extends DefaultMutableTreeNode. The problem is this: Say I have a perfectly good class that represents times. I've already written a perfectly good compareTo method (which works as...

How get List from Set and Comparator

What is the "good" (and why ?) solution to get a List from a Set and sorted against a given Comparator ? ...

[Java/Android] Problem with String equals() returning false (custom Comparator involved)

Hello, I built a very simple custom Comparator, that I use with a TreeSet in order to sort Strings by length in that TreeSet. I'm having trouble finding the reason why (s1.equals(s2)) returns false even when the two strings s1 and s2 contain the same value... Eclipse "variables view" shows the letters are the same in both strings, but...

Minimal code, shorten expression

Following parameters are given: boolean a = true ; boolean b = false ; boolean c = true ; I want to have minimal code of this version: if ( ( a && ! b) || ( ! a && b) ) { z1 += 99 ; } if (a ^ b) { z1 += 19 ; } if ( ( a && b) || ( ! a && ! b) ) { z1 += 118; } What needs to be modified? ...