Hi I'm trying to sort an object based on a value of that object. Originally I used TreeMap which did the sorting but there were some non-unique keys which were removed so that didn't work. I googled around and came across Guava MultiMap. But how do I actually use this to sort the values?
Basically I'm trying to sort budget information based on the diffYTD_percentage value. Thanks!
Multimap<Double, BudgetInformation> multiMap = ArrayListMultimap.create();
for (Budget budget : budgets) {
BudgetInformation budgetInfo = getBudget(budget.getId());
double actualToDate = budgetInfo.getActualToDate();
double budgetToDate = budgetInfo.getTotalBudgetToDate();
double diffYTD_value = budgetToDate - actualToDate;
double diffYTD_percentage_value = 0.0;
if (budgetToDate != 0.0) {
double fraction = actualToDate / budgetToDate;
fraction = fraction - 1;
diffYTD_percentage_value = fraction * 100;
}
multiMap.put(diffYTD_percentage_value, budgetInfo);
}
Iterator<BudgetInformation> budgetIterator = multiMap.values().iterator();
Using Tree in order to sort collection is wrong approach. In Java there are dedicated utility methods to sort list, Collections.sort(List<T extends Comparable<? super T>> list) and Collections.sort(List<T> list, Comparator<? super T> c). The first is used to sort list where elements have natural ordering (basically implement Comparable interface), the later is used in cases like your own, when you need to sort elements using custom ordering.
So, basically what you need to do is to create Comparator that will compare two BusinessInfo instances based on diffYTD_percentage_value and call Collections.sort with this comparator.
Comparator<BudgetInformation> budgetInfoCmp = new Comparator<BudgetInformation>() {
private double getDiffYTDPercentage(BudgetInformation budgetInfo) {
double actualToDate = budgetInfo.getActualToDate();
double budgetToDate = budgetInfo.getTotalBudgetToDate();
double diffYTDValue = budgetToDate - actualToDate;
double diffYTDPercentageValue = 0.0;
if (budgetToDate != 0.0) {
double fraction = actualToDate / budgetToDate;
fraction = fraction - 1;
diffYTDPercentageValue = fraction * 100;
}
return diffYTDPercentageValue;
}
#Override
public int compare(BudgetInformation o1, BudgetInformation o2) {
return Double.compare(getDiffYTDPercentage(o1), getDiffYTDPercentage(o2));
}
};
List<BudgetInformation> budgetInformationsToSort = getBudgetInformations();
Collections.sort(budgetInformationsToSort, budgetInfoCmp);
Also try to avoid using underscore in variable naming, as under Java naming convention variable names should be in camel case.
How about using TreeMultiMap? It doesnt support duplicate key-value pair. But duplicate keys are fine.
Related
I have a program that calculates the correlation value between a currency and a stock value. I'm adding the "pairs" (currency name + ": " + Correlation Value) in to an ArrayList and if i print out the arraylist this is my output:
SDG: 0.6672481089755959
RON: 0.7950474904606127
MKD: 0.788195252851783
MXN: 0.8429550156320716
CAD: 0.7777753208834005
ZAR: 0.8254509631193871
I'm trying to think of a smart way to sort them by correlation value, from biggest to smallest, but can't think of a good way of doing this. Any ideas?
You could have three approaches:
Use Collections.sort with a custom comparator, which splits the string by : (and trims it) and then returns the .compareTo value of the numeric part of your string.
Create a new object maybe call it CurrencyCorrelation which has 2 properties (currencyName and correlation maybe?). The class will implement the Comparable interface and override the toString() method to yield the correlation as you'd like (currencyName + ": " + String.valueOf(correlation)). You would then call Collections.sort(...) without the need of specifying the comparator, as per option 1.
As per #Sasha Salauyou's recommendation, you could also declare the class, as per option 2 and then use Java 8 lamba expressions to define the comaparator, without the need of having your class extend the Comparable interface. This would look something like so: list.sort((e1, e2) -> e1.getCorrelation().compareTo(e2.getCorrelation()))
The second option would probably be better, with the first option requiring less changes.
You could store them in a TreeMap which is already sorted. From Docs:
The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.
Assuming your elements are strings and that the correlation values are doubles, you can simply write your own comparator and sort:
Collections.sort(al, new Comparator<String>() {
#Override
public int compare(String s1, String s2) {
double d1 = Double.parseDouble(s1.substring(s1.indexOf(": ") + 2));
double d2 = Double.parseDouble(s2.substring(s2.indexOf(": ") + 2));
return Double.compare(d1, d2);
}
});
/** Immutable class holding currency-correlation pair */
public static class CurrencyCor {
public final String currency;
public final Double correlation;
private CurrencyCor(String cur, Double cor) {
if (cur == null || cor == null)
throw new NullPointerException("Null argument(s)");
currency = cur;
correlation = cor;
}
#Override
public String toString() {
return String.format("%s: %s", currency, correlation)
}
}
// ...
List<CurrencyCor> list = new ArrayList<>();
list.add(new CurrencyCor("SDG", 0.6672481089755959));
list.add(new CurrencyCor("ZAR", 0.8254509631193871));
// ... add remaining pairs
list.sort((e1, e2) -> e1.correlation.compareTo(e2.correlation)); // sort by correlation value
list.forEach(System.out::println); // print out sorted pairs
Hi am trying to draw polygons on the map depending on the shortest distance.
i have created the array list called distancearray.
but i want to create a new array list called shortdistance, this array list must have the same values in the distancearray but it must be shorted in assending order depending on the distance.
can any one help me to create this shortdistance array. Thank you.
String array[][]=dbhand.collecting(getIntent().getExtras().getString("temple_type"), Integer.parseInt(getIntent().getExtras().getString("notemples")));
for(int i=0;i<array.length;i++){
displaymarkers(Double.parseDouble(array[i][0]), Double.parseDouble(array[i][1]), array[i][2]);
}
for(int i=0;i<array.length;i++){
double Distance = calculate_distance(SEATTLE_LAT, SEATTLE_LNG, Double.parseDouble(array[i][0]), Double.parseDouble(array[i][1]));
double[][] distancearray = new double[array.length][3];
distancearray[i][0]=Double.parseDouble(array[i][0]);
distancearray[i][1]=Double.parseDouble(array[i][1]);
distancearray[i][2]=Distance;
}
double [][] shortdistance = new double [array.length][3];
Draw Polygon Function
private void drawpolygon(String array[][]) {
int lengh = array.length;
if(lengh==2){
mMap.addPolygon(new PolygonOptions()
//.add(new LatLng(9.662502, 80.010239), new LatLng(9.670931, 80.013201), new LatLng(9.663216, 80.01333))
.add(new LatLng(9.6632139, 80.0133258))
.add(new LatLng(Double.parseDouble(array[0][0]), Double.parseDouble(array[0][1])))
.add(new LatLng(Double.parseDouble(array[1][0]), Double.parseDouble(array[1][1])))
.fillColor(Color.GRAY));
}
Just call Arrays.sort() against your array and specify a comparator that looks at the distance.
You are going to have difficulty sorting your arrays as-is because you currently store your data in parallel arrays:
distancearray[i][0]=Double.parseDouble(array[i][0]);
distancearray[i][1]=Double.parseDouble(array[i][2]);
distancearray[i][3]=Distance;
You can sort distancearray[n] but that will give you nonsense results. Instead what you should do is create a small class that implements Comparable (comparing based on distance) that holds your three values, then work with an array of those classes:
class DistanceInfo implements Comparable<DistanceInfo> {
public double a;
public double b;
public double distance;
public DistanceInfo (double a, double b, double distance) {
this.a = a;
this.b = b;
this.distance = distance;
}
#Override public int compareTo (DistanceInfo d) {
return Double.compare(distance, d.distance);
}
}
// then:
DistanceInfo[] distancearray = new DistanceInfo[array.length];
// and you can load it using the constructor:
for (int i = 0; i < array.length) {
double a = Double.parseDouble(array[i][0]);
double b = Double.parseDouble(array[i][1]);
distancearray[i] = new DistanceInfo(a, b, Distance);
}
(The fields can be made final if you wish to specify that they cannot be modified after construction.)
Now, Arrays.sort() will sort distancearray based on distance:
Arrays.sort(distancearray, null);
You could also use an ArrayList<DistanceInfo> instead, the idea is the same except you sort with Collections.sort().
In general, for this reason (among others) it is usually better to use a class to store all information about an object as opposed to parallel arrays.
As an aside, you may want to consider using this class for array as well, it will simplify your code a bit (you can modify displaymarkers to take a DistanceInfo, and you can also avoid parsing the same double more than once).
I am attempting to sort a hashmap on type <Integer,Double> using a TreeMap and a SortedMap I want to sort on the absolute values of the Doubles but I also want to retain the sign value (hence not storing as an unsigned Double).
Below is the code I am using, however I am not getting the values I expect, presumably due to the use of hashcode() can anybody point out how to fix this?
Map<Integer,Double> termWeights = new HashMap<Integer,Double>();
SortedMap sortedData = new TreeMap(new ValueComparer(termWeights));
System.out.println(termWeights);
sortedData.putAll(termWeights);
System.out.println(sortedData);
class ValueComparer implements Comparator {
private Map _data = null;
public ValueComparer(Map data) {
super();
_data = data;
}
public int compare(Object o1, Object o2) {
Double e1 = Math.abs((Double) _data.get(o1));
Double e2 = Math.abs((Double) _data.get(o2));
int compare = e2.compareTo(e1);
if (compare == 0) {
Integer a = o1.hashCode();
Integer b = o2.hashCode();
return b.compareTo(a);
}
return compare;
}
}
Thanks
Can you give an example of expected and actual results?
Sorted map: {17=1.644955871228835, 0=-1.029545248153297, 10=-5.291765636407169E-4, 9=-3.331976978545177E-4, 1=-2.7105555587851366E-4, 2=-2.7105555587851366E-4, 7=-2.0897436261984377E-4, 8=-1.305197184270594E-5, 3=0.0, 4=0.0, 5=0.0, 6=0.0, 11=0.0, 12=0.0, 13=0.0, 14=0.0, 15=0.0, 16=0.0, 18=0.0, 19=0.0, 20=0.0, 21=0.0, 22=0.0}
So what is the problem?
That looks correctly sorted from biggest to smallest.
But I would avoid using hashCode in the tie-break secondary comparator, because you need it to never return the same value for different inputs. In this case, it works, because you are calling it on an Integer, where hashCode just returns the same int. But if you used Long or String keys in your map, it would have collisions. Compare the two keys directly instead.
And finally, you must not change the weights after starting to use the comparator. That will lead to an inconsistent TreeMap.
Hey, I need to sort an array list of class house by a float field in a certain range. This is my code for the selection sort:
Basically sortPrice copies the stuff in this list into a new one selecting only the values in that range, then it does a selection sort in that array list. The arraylist (x) in sortPrice is unsorted an needs to be copied because what x references cannot be altered.
public ArrayList<House> sortPrice(ArrayList<House> x,float y, float z){
ArrayList<House> xcopy = new ArrayList<House>();
for(int i = 0; i<x.size(); i++){
if(x.get(i).myPriceIsLessThanOrEqualTo(z) && x.get(i).myPriceIsGreaterThanOrEqualTo(y)){
xcopy.add(x.get(i));
}
}
ArrayList<House> price= new ArrayList<House>();
while(xcopy.size()>0){
House min = xcopy.get(0);
for(int i = 1; i < xcopy.size();i++){
House current = xcopy.get(i);
if (current.myPriceIsGreaterThanOrEqualTo(min.getPrice())){
min = current;
}
}
price.add(min);
xcopy.remove(min);
}
return price;
}
Here is what the house class looks like:
public class House {
private int numBedRs;
private int sqft;
private float numBathRs;
private float price;
private static int idNumOfMostRecentHouse = 0;
private int id;
public House(int bed,int ft, float bath, float price){
sqft = ft;
numBathRs = bath;
numBedRs = bed;
this.price = price;
idNumOfMostRecentHouse++;
id = idNumOfMostRecentHouse;
}
public boolean myPriceIsLessThanOrEqualTo(float y){
if(Math.abs(price - y)<0.000001){
return true;
}
return false;
}
public boolean myPriceIsGreaterThanOrEqualTo(float b){
if(Math.abs(b-price)>0.0000001){
return true;
}
return false;
}
When i call looking for houses in range 260000.50 to 300000 I only get houses that are at the top of the range even though I have a lower value at 270000. Can someone help?
You should use rounded double type.
DecimalFormat p= new DecimalFormat( "#0.00" );
double price = 123.00001;
price = new Double(p.format(price)).doubleValue();
Your method name indicates it sorts the data, the method also filters. You need to change the name. Actually there are a number of variables in this code which need their names change to better reflect what they contain.
You need to replace the for loops with for-each loops. That will simply and make the code clearer. You would also no longer need the while() statement.
I would also refactor the mypriceis... methods into a single ispriceinrange method.
Alternatively I'd look at the comparator interface and look at introducing it as a way to sort you data without having to hand code a loop.
Just took a second look. You do actually sort in the second loop, but that some ugly code and probably won't perform well to boot. Definitely go look at comparator and Collections.sort() methods. Save yourself a world of pain.
The best data structure for this problem would be a NavigableMap<Float,List<House>> (such as a TreeMap), which supports a subMap operation:
SortedMap<K,V> subMap(K fromKey, K toKey) : Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive.
There is also an overload that allows you to set inclusive bounds.
Here's a demonstration:
NavigableMap<Integer,String> nmap = new TreeMap<Integer,String>();
nmap.put(5, "Five");
nmap.put(1, "One");
nmap.put(7, "Seven");
nmap.put(3, "Three");
System.out.println(nmap.subMap(2, 6));
// prints {3=Three, 5=Five}
For your case, you'd want to either make House implementsComparable<House>, or define your own custom Comparator<House>. You'd then let TreeMap do the sorting for you, and don't have to deal with selection sort (which, at O(N^2), is far from optimal).
If you're stuck with the specification that you're given, then I'd suggest breaking apart the logic into helper methods like these:
List<House> filterInRange(List<House> houses, float low, float high) {
List<House> ret = new ArrayList<House>();
for (House h : houses) {
if (isInRange(h.getPrice(), low, high)) {
ret.add(h);
}
}
return ret;
}
static boolean isInRange(float v, float low, float high) { ...DIY... }
void selectionSort(List<House> houses) { ...Wikipedia... }
Then to get a sorted List<House> in a specified price range, invoke filterInRange and then selectionSort the returned list.
See also
Java language guide/for-each
I need a sorted set of objects and am currently using the TreeSet. My problem is that the compareTo of the objects will often return 0, meaning the order of those two objects is to be left unchanged. TreeMap (used by TreeSet by default) will then regard them as the same object, which is not true.
What alternative to TreeMap can I use?
Use case: I have a set of displayable objects. I want to sort them by Y coordinate, so that they are rendered in the correct order. Of course, two objects may well have the same Y coordinate.
You're defining one criteria to compare, but you need to add extra criteria.
You say:
I have a set of displayable objects. I want to sort them by Y coordinate, so that they are rendered in the correct order. Of course, two objects may well have the same Y coordinate.
So, If two elements have the same Y coordinate, what you you put first? What would be the other criteria?
It may be the creation time, it may be the x coordinate, you just have to define it:
Map<String,Thing> map = new TreeMap<String,Thing>(new Comparator<Thing>(){
public int compare( Thing one, Thing two ) {
int result = one.y - two.y;
if( result == 0 ) { // same y coordinate use another criteria
result = one.x - two.x;
if( result == 0 ) { //still the same? Try another criteria ( maybe creation time
return one.creationTime - two.creationTime
}
}
return result;
}
});
You have to define when one Thing is higher / lower / equal / than other Thing . If one of the attributes is the same as other, probably you should not move them. If is there other attribute to compare the use it.
The issue you're running into is that compareTo returning 0 means that the objects are equal. At the same time, you're putting them into a set, which does not allow multiple copies of equal elements.
Either re-write your compareTo so that unequal elements return different values, or use something like a java.util.PriorityQueue which allows multiple copies of equal elements.
I've done this before. It's an ordered multi-map and it is just a TreeMap of List objects. Like this..
Map<KeyType, List<ValueType>> mmap = new TreeMap<KeyType, List<ValueType>>();
You need to construct a new LinkedList every time a new key is introduced, so it might be helpful to wrap it in a custom container class. I'll try to find something.
So, I threw this custom container together quickly (completely untested), but it might be what you are looking for. Keep in mind that you should only use this type of container if you are truly looking for an ordered map of value lists. If there is some natural order to your values, you should use a TreeSet as others have suggested.
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class MTreeMap<K, V> {
private final Map<K, List<V>> mmap = new TreeMap<K, List<V>>();
private int size = 0;
public MTreeMap() {
}
public void clear() {
mmap.clear();
size=0;
}
public boolean containsKey(K key) {
return mmap.containsKey(key);
}
public List<V> get(K key) {
return mmap.get(key);
}
public boolean isEmpty() {
return mmap.isEmpty();
}
public Set<K> keySet() {
return mmap.keySet();
}
public Collection<List<V>> valueLists() {
return mmap.values();
}
public void put(K key, V value) {
List<V> vlist = mmap.get(key);
if (null==vlist) {
vlist = new LinkedList<V>();
mmap.put(key, vlist);
}
vlist.add(value);
++size;
}
public List<V> remove(Object key) {
List<V> vlist = mmap.remove(key);
if (null!=vlist) {
size = size - vlist.size() ;
}
return vlist;
}
public int size() {
return size;
}
public String toString() {
return mmap.toString();
}
}
Here's a rudimentary test:
public class TestAnything {
public static void main(String[] args) {
MTreeMap<Integer, String> mmap = new MTreeMap<Integer, String>();
mmap.put(1, "Value1");
mmap.put(2, "Value2");
mmap.put(3, "Value3");
mmap.put(1, "Value4");
mmap.put(3, "Value5");
mmap.put(2, "Value6");
mmap.put(2, "Value7");
System.out.println("size (1) = " + mmap.get(1).size());
System.out.println("size (2) = " + mmap.get(2).size());
System.out.println("size (3) = " + mmap.get(3).size());
System.out.println("Total size = " + mmap.size());
System.out.println(mmap);
}
}
The output is this:
size (1) = 2
size (2) = 3
size (3) = 2
Total size = 7
{1=[Value1, Value4], 2=[Value2, Value6, Value7], 3=[Value3, Value5]}
I have one idea of my own, but it's more of a workaround
int compare(Object a, Object b) {
an = a.seq + (a.sortkey << 16); // allowing for 65k items in the set
bn = b.seq + (a.sortKey << 16);
return an - bn; // can never remember whether it's supposed to be this or b - a.
}
sortKey = what really matters for the sorting, for example an Y coordinate
seq = a sequence number assigned to objects when added to the set
There are 2 important things to remember when using sorted sets (e.g. TreeSet) :
1) They are sets; two equal elements are not allowed in the same collection
2) Equality must be consistent with the comparison mechanism (either comparator or comparable)
Therefore, in your case you should "break ties" by adding some secondary ordering criteria. For example: first use Y axis, then X, and then some unique object identifier.
See also http://eyalsch.wordpress.com/2009/11/23/comparators/