I'm curious. What could be the reason that a Comparator shuffles entries on each
application start?
final static class ContactsListComparator implements Comparator
{
public int compare(Object o1, Object o2)
{
if((o1.toString().compareTo(o2.toString()))<0)
{
return -1;
}
if((o1.toString().compareTo(o2.toString()))>0)
{
return 1;
}
else
{
return 0;
}
}
}
First App Start:
Second App Start
As mentioned in one the answer
The Comparator actually compares an custom object Contact
public class Contact
{
// Members
private String _contactFirstName;
private String _contactLastName;
private long _contactLastModified;
// Constructor
public Contact()
{
set_contactLastModified();
}
public Contact(String contactFirstName)
{
_contactFirstName = contactFirstName;
set_contactLastModified();
}
// Accessable Getters
public String get_contactFirstName()
{
return _contactFirstName;
}
public String get_contactLastName()
{
return _contactLastName;
}
public long get_contactLastModified()
{
return _contactLastModified;
}
public void set_contactLastModified()
{
_contactLastModified = System.currentTimeMillis();
}
}
your toString method probably isn't overridden for your objects representing the contacts. It will return a hash string for those objects, which varies every time your app is run.
You can fix this either of two ways:
Override the toString() method in your Contact object to return the contact's name (1), or
Change the Comparator to Comparator<Contact> so it gets Contact objects as parameters (2)
for (1), add this to your Contact class:
#Override public String toString() {
return get_contactFirstName();
}
for (2) you would end up with this Comparator implementation:
final static class ContactsListComparator implements Comparator<Contact> {
public int compare(Contact o1, Contact o2) {
return contact1.get_contactFirstName().compareTo(contact2.get_contactFirstName());
}
}
you don't even need to check for the <0 or >0, but you can just return whatever the String comparison gives.
I would use:
final static class ContactsListComparator implements Comparator<Contact>
{
public int compare(Contact c1,Contact c2)
{
int i=c1.get_contactLastName().compareTo(c2.get_contactLastName());
if(i!=0) return i;
return c1.get_contactFirstName().compareTo(c2.get_contactFirstName());;
}
}
Your first example is basically the same as
final static class ContactsListComparator implements Comparator {
public int compare(Object o1, Object o2) {
return o1.toString().compareTo(o2.toString());
}
}
This would work if you override toString() like
public String toString() {
return _contactFirstName + ' ' + _contactLastName;
}
However, a comparator which compares the intended fields is better as has been suggested.
Related
I need to make a programm which is like a rally, theres 2 types of vehicles, motorcycle and cars, two types of motorcycle, with and without sidecar, the thing is that I need to verify if there is just a motorcycle in an array list, I mean, two wheels vehicle. That verification should be done in a method called esDe2Ruedas(), which is called by an abstract overrided method called check() that should be the one that verifies if a group of vehicles from an array are able to run in the rally, if its true all the elements of the array must be from the same type.
Here is the code
this is how the program arrays the vehicles
GrandPrix gp1 = new GrandPrix();
gp1.agregar(v1);
//gp1.mostrar(v1);
gp1.agregar(v2);
System.out.println(gp1.check());
GrandPrix gp2 = new GrandPrix();
gp2.agregar(vt1);
gp2.agregar(vt2);
gp2.agregar(m2);
System.out.println(gp2.check());
GrandPrix gp3 = new GrandPrix();
gp3.agregar(vt1);
gp3.agregar(vt2);
gp3.agregar(m1);
System.out.println(gp3.check());
GrandPrix gp4 = new GrandPrix();
gp4.agregar(m1);
gp4.agregar(m2);
System.out.println(gp4.check());
This is the class that is using
import java.util.ArrayList;
public class GrandPrix extends Rally{
ArrayList<Vehiculo> ve = new ArrayList<Vehiculo>();
public void agregar(Vehiculo v) {
ve.add(v);
}
public void agregar(Carro c) {
ve.add(c);
}
public void agregar(Moto m) {
ve.add(m);
}
#Override
boolean check() {// HERE I VERIFY IF THE VEHICLES ARE COMPATIBLE
return false;
}
}
This is the class where everything goes on
public class Vehiculo {
private String Nombre;
private double velocidad_max;
private int peso;
private int comb;
public Vehiculo() {
setNombre("Anónimo");
setVel(130);
setPeso(1000);
setComb(0);
}
public Vehiculo(String string, double d, int i, int j) {
setNombre(string);
setVel(d);
setPeso(i);
setComb(j);
}
double rendimiento() {
return velocidad_max/peso;
}
public boolean mejor(Vehiculo otroVehiculo) {
return rendimiento()>otroVehiculo.rendimiento();
}
public String toString() {
return getNombre()+"-> Velocidad máxima = "+getVel()+" km/h, Peso = "+getPeso()+" kg";
}
/**************************************
---------SET And GET Nombre------------
***************************************/
public String getNombre() {
return Nombre;
}
public void setNombre(String nuevoNombre) {
this.Nombre=nuevoNombre;
}
/**************************************
---------SET And GET velocidad_max------------
***************************************/
public double getVel() {
return velocidad_max;
}
public void setVel(double nuevaVel) {
this.velocidad_max=nuevaVel;
}
/**************************************
---------SET And GET peso------------
***************************************/
public double getPeso() {
return peso;
}
public void setPeso(int nuevoPeso) {
this.peso=nuevoPeso;
}
/**************************************
---------SET And GET comb------------
***************************************/
public int getComb() {
return comb;
}
public void setComb(int comb) {
this.comb = comb;
}
boolean esDe2Ruedas() {
return false;
}
}
This is the class of motorcycles, which is in theory the same as the car's class, without sidecar thing
public class Moto extends Vehiculo{
private boolean sidecar;
public Moto(String string, double d, int i, int j) {
setNombre(string);
setVel(d);
setPeso(i);
setComb(j);
setSidecar(false);
}
public Moto(String string, double d, int i, int j, boolean b) {
setNombre(string);
setVel(d);
setPeso(i);
setComb(j);
setSidecar(b);
esDe2Ruedas(false);
}
public String toString() {
String str = null;
if(isSidecar())
str =super.toString()+", Moto, con sidecar";
else
str =super.toString()+", Moto";
return str;
}
public boolean isSidecar() {
return sidecar;
}
public void setSidecar(boolean sidecar) {
this.sidecar = sidecar;
}
I guess what you presented is what is given. If you came up with the design it is ok, but I believe it could be improved. Anyway, I try to respond to what I believe was your question straight away.
Vehiculo is the super type of Moto (which can have a side car and becomes 3 wheeler).
Vehiculo has a method esDe2Ruedas, which returns false.
Moto inherits that method <-- this is wrong, it should override it and, depending on side car, return the expected boolean value.
In the check method you can now distinguish between Moto and "Moto with sidecar" by using that method.
Lets say I have a Product class in Java and 2 Comparators:
1st is price Comparator for asc order.
2nd is price Comparator for desc order.
It can be that if I changed the 1st to be product name Comparator, so, the 2nd will change automatic to name Comparator as well?
Thanks alot!
Exmaple:
class ProductComparatorByPriceDesc implements Comparator<Customer> {
#Override
public int compare(Product o1, Product o2) {
return o1.getPrice() - o2.getPrice();
}
}
Class ProductComparatorByPriceAsc implements Comparator<Customer> {
#Override
public int compare(Customer o1, Customer o2) {
return o2.getPrice() - o1.getPrice();
}
}
So if i changed the 1st comparator to sort by name, not price, the 2nd will changed as well, but not the opposite!
One way would be:
import java.util.*;
class SomeClass {
public int price;
public String name;
public SomeClass(String name, int price) {
this.name = name;
this.price = price;
}
}
class PriceOrNameComparator implements Comparator<SomeClass> {
boolean compareByPrice;
public PriceOrNameComparator byPrice() {
this.compareByPrice = true;
return this;
}
public PriceOrNameComparator byName() {
this.compareByPrice = false;
return this;
}
public int compare(SomeClass a, SomeClass b) {
if (compareByPrice) {
return a.price - b.price;
} else {
return a.name.compareTo(b.name);
}
}
public Comparator<SomeClass> reverseComparator() {
return new Comparator<SomeClass>() {
public int compare(SomeClass a, SomeClass b) {
int res = PriceOrNameComparator.this.compare(a, b);
if (res == 0) {
return 0;
} else {
return (res > 0) ? -1 : 1;
}
}
};
}
}
class Test {
public static void main(String[] args) {
SomeClass s1 = new SomeClass("a", 5);
SomeClass s2 = new SomeClass("b", 4);
PriceOrNameComparator c = new PriceOrNameComparator().byPrice();
Comparator<SomeClass> r = c.reverseComparator();
System.out.println(c.compare(s1, s2)); // 1
System.out.println(r.compare(s1, s2)); // -1
c.byName();
System.out.println(c.compare(s1, s2)); // -1
System.out.println(r.compare(s1, s2)); // 1
}
}
Basically, the outer comparator is configurable, and the inner, reverse order, comparator, being an anonymous inner class, has an implicit reference to the outer comparator and can observe changes in its state.
I would suggest only having a single comparator class for comparing by price, and a separate comparator class to compare by name (or no classes - see the end of my answer). Each class does one thing, and does it well.
Then you can reverse any comparator using the Comparator.reversed default method... and likewise you can chain them together using Comparator.thenComparing, should you wish to order by name and then price, for example:
Comparator<Product> nameThenPrice =
new NameComparator().thenComparing(new PriceComparator());
(If you're not using Java 8, it's easy enough to write a ReversingComparator which takes an existing one, and a CompoundComparator which takes two existing ones.)
You can also use Java 8's static methods in Comparator:
Comparator<Product> byName = Comparator.comparing(p -> p.getName());
Comparator<Product> byPrice = Comparator.comparing(p -> p.getPrice());
Comparator<Product> nameThenPrice = byName.thenComparing(byPrice);
That way you often don't need to implement Comparator at all manually.
I am using three classes in my program:
Term class with variables coefficient and exponent, toString() method etc.
Polynome class, using an ArrayList to store the different Term objects.
Main class that runs the program.
Can I use the toString method of ArrayList in my Polynome class? I'm trying to, but I can't.
I need my polynome to output like this: [3x^2, 3x^1, 1x^0]
I am really confused, I'm calling the toString method of Term, using a for-loop to access each term separately.
My code:
public class Term {
private int coëfficiënt;
private int exponent;
public Term(int coëfficiënt, int exponent) {
this.coëfficiënt = coëfficiënt;
this.exponent = exponent;
}
public int getCoef() {
return coëfficiënt;
}
public int getExp() {
return exponent;
}
public String toString() {
return coëfficiënt + "x^" + exponent;
}
}
Polynome class:
public class Polynoom {
private ArrayList<Term> polynoom;
public Polynoom() {
polynoom = new ArrayList<Term>();
}
public void add(Term term) {
polynoom.add(term);
}
public Term get(int i) {
return polynoom.get(i);
}
public int size() {
return polynoom.size();
}
public String toString() {
// what should I write here?
}
}
Main class:
public class opgave3 {
public static void main(String[] args) {
Polynoom polynoom1, polynoom2, sompolynoom;
polynoom1 = new Polynoom();
polynoom1.add(new Term(1, 2));
polynoom1.add(new Term(3, 1));
polynoom1.add(new Term(1, 0));
polynoom2 = new Polynoom();
polynoom2.add(new Term(-1, 3));
polynoom2.add(new Term(2, 2));
polynoom2.add(new Term(-5, 0));
System.out.println("Tests: ");
System.out.println(polynoom1.toString());
for (int i = 0; i < polynoom1.size(); i++) {
System.out.println(polynoom1.get(i).toString());
}
System.out.println(polynoom1.get(0).toString());
}
}
You just need to use your ArrayList's toString() method as the results of Polynome's toString() method.
public class Polynome {
public ArrayList<Term> terms;
#Override
public String toString() {
if (terms != null) {
return terms.toString();
} else {
return "";
}
}
}
EDIT: The quick answer, since you put your code up is to put
return polynoom.toString();
where you have indicated. Then in your Main class you can simply write
System.out.println(polynoom1);
to show the contents in the desired format.
As Tenner said, use the toString() method of your ArrayList to get the desired output. But also make sure your Term class has a useful toString method of its own:
public class Term {
private int co, ex;
public Term(int coeff, int exp) {
co = coeff;
ex = exp;
}
#Override
public String toString() {
return co + "x^" + ex;
}
}
Add #Override toString() to your Term & Polynome class. The Term class toString() should return a string in the format of coefficientx^exponent.
Then have the Polynome class toString() return yourArrayList.toString()
public static void main(String[] args) throws Exception {
Polynome polynome = new Polynome();
polynome.addTerm(3, 2);
polynome.addTerm(3, 1);
polynome.addTerm(1, 0);
System.out.println(polynome);
}
public static class Term {
private int coefficient;
private int exponent;
public Term(int c, int e) {
coefficient = c;
exponent = e;
}
#Override
public String toString() {
return coefficient + "x^" + exponent;
}
}
public static class Polynome {
private List<Term> terms = new ArrayList<>();
public void addTerm(int coefficient, int exponent) {
terms.add(new Term(coefficient, exponent));
}
#Override
public String toString() {
return terms.toString();
}
}
Results:
Long story short, you can ALWAYS use toString() on anything, even if it's a user defined class. When you call the method, it calls the closest parent class's toString() method, which is guaranteed to be there as Object has one. If you want to control the output of toString() called on your object, you must override it. As it is, if you have an object with a member of type ArrayList, calling your object's toString() will include a ton of extra information that you probably don't want. In order to get the output you want, you need to have the code given by #Tenner's answer, which is
public class Polynome {
public ArrayList<Term> terms;
#Override
public String toString() {
if (terms != null) {
return terms.toString();
} else {
return "";
}
}
}
But you also need to override toString() in the Term class, so that each term outputs in the form desired. The reason this is required is that when you call toString() on an ArrayList, or any other container for that matter, it iterates through the container, calling each object's toString() in turn, adding whatever formatting the container class defines. Ultimately, Term's toString() will be called, and you can control that output by overriding it in the Term class.
As for the last part of the question, you need not call Term's toString() directly, as calling the toString() method of the ArrayList will do this on its own.
I want to sort some objects which are in an ArrayList on the basis of the objects properties.
the object has:
public class Minterm
{
String minTerm;
char flagTick;
String minTermDerive;
int groupNo;
String adjGroup;
static int MaxLiterals;
then i have this in the main method:
ArrayList<Minterm> column =new ArrayList<Minterm>();
then i add some objects of type Minterm in the list. but at the end i want to organise them and sort them on the member variable groupNo(Ascending order).
i searched and came up with the comparable and comparator interfaces that i tried but didnt succeed. is there any other method to do this? or am i doing the comparator implemnting wrong.
EDIT :
Following is the code i wrote for Comparator. Please confirm if it will sort in ascending?
package backEnd;
import java.util.Comparator;
public class Comp implements Comparator<Minterm>
{
#Override
public int compare(Minterm a, Minterm b)
{
return a.getgroupOne().compareTo(b.getgroupOne());
}
}
i run it as:
Collections.sort(column , new Comp());
seems to be working fine. but i dont have a sound understanding of it.
Please confirm if it will sort in ascending?
You should let Minterm implement Comparable<MinTerm> or write a custom Comparator for MinTerm and then use Collections.sort.
Using a comparator it would look like this:
Collections.sort(column, new Comparator<Minterm>() {
#Override
public int compare(Minterm o1, Minterm o2) {
return Integer.valueOf(o1.groupNo).compareTo(o2.groupNo);
}
});
Regarding your edit:
Yes. that sorts Minterms based on the groups in ascending order.
Collections.sort() and the Comparator interface are precisely the right tool for this job.
Something along the following lines should do it (untested):
Collections.sort(column, new Comparator<Minterm>() {
public int compare(Minterm o1, Minterm o2) {
return Integer.valueOf(o1.groupNo).compareTo(o2.groupNo);
}
});
Two ways, using Collections.sort(..):
make your object implement Comparable. Involves changing the original class, which may not be possible
supply custom Comparator. It takes instances of your object and compares them. Doesn't require a change to the class.
Either way, make sure you conform to the interfaces' contracts.
Here is the sample code (for more examples refer http://java2novice.com/java-collections-and-util/arraylist/sort-comparator/ ):
public class MyArrayListSort {
public static void main(String a[]){
List<Empl> list = new ArrayList<Empl>();
list.add(new Empl("Ram",3000));
list.add(new Empl("John",6000));
list.add(new Empl("Crish",2000));
list.add(new Empl("Tom",2400));
Collections.sort(list,new MySalaryComp());
System.out.println("Sorted list entries: ");
for(Empl e:list){
System.out.println(e);
}
}
}
class MySalaryComp implements Comparator<Empl> {
#Override
public int compare(Empl e1, Empl e2) {
if(e1.getSalary() < e2.getSalary()){
return 1;
} else {
return -1;
}
}
}
class Empl{
private String name;
private int salary;
public Empl(String n, int s){
this.name = n;
this.salary = s;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String toString(){
return "Name: "+this.name+"-- Salary: "+this.salary;
}
}
I have this class:
public class Sample implements Comparable<Sample> {
public String a;
public String b;
public String c;
public int compareTo (Sample sampleToCompare) {
int compResult = this.a.compareTo(sampleToCompare.a);
return (compResult != 0 ? compResult :
this.b.compareTo(sampleToCompare.b));
}
}
I want compareTo() to behave or sort using different class properties depending if a flag is set.
So, if flag == 1 I'd like compareTo() to using property c, otherwise is flag == 0, whatever is currently in the method.
In other words, sort the same class in different ways.
I am not sure how to achieve this. Please help.
Also, please let me know if more information is needed from my side.
If you want to implement different kind of sorting, you should take a look at java.util.Comparator interface.
public class SampleComparatorA implement Comparator<Sample> {
public int compare(Sample a, Sample b) {
// Your sorting
}
}
And use java.util.Collections.sort() method with the Comparator as the secound parameter instead.
Collections.sort(aSampleList, new SampleComparatorA());
How about:
public int compareTo(Sample sampleToCompare) {
if (flag == 1) {
return this.c.compareTo(sampleToCompare.c);
}
if (flag == 0) {
// current stuff
}
...
}
That's not a very object-oriented way to do it, though. Probably you should have two different comparators and a way to select them based on your "flag" value. Something like:
class Sample {
private String a;
private String b;
private String c;
}
class ASampleComparator implements Comparator<Sample> {
public int compare(Sample o1, Sample o2) {
return o1.a.compareTo(o2.a);
}
}
class BSampleComparator implements Comparator<Sample> {
public int compare(Sample o1, Sample o2) {
return o1.b.compareTo(o2.b);
}
}
class CSampleComparator implements Comparator<Sample> {
public int compare(Sample o1, Sample o2) {
return o1.c.compareTo(o2.c);
}
}
public Comparator<Sample> pickComparator(int flag) {
switch (flag) {
case 0:
return new ASampleComparator();
case 1:
return new BSampleComparator();
case 2:
return new CSampleComparator();
default:
throw new IllegalArgumentException("Bad flag value: " + flag);
}
}
You should make your flag static so the comparison will be consistent (as described in Effective Java, item 12), otherwise, you might get that a.compareTo(b) returns that a > b, but b.compareTo(a) returns that b > a. So the simplest implementation I can think about is:
public class Sample implements Comparable<Sample> {
public String a;
public String b;
public String c;
public static boolean my_flag = false;
public int compareTo (Sample sampleToCompare) {
if (flag) {
return this.c.compareTo(sampleToCompare.c);
}
int compResult = this.a.compareTo(sampleToCompare.a);
return (compResult != 0 ? compResult :
this.b.compareTo(sampleToCompare.b));
}
}