Trying to use BeanComparator in my gradle project

156 Views Asked by At

I'm having trouble using BeanComparator in my gradle project. I can import in my build.gradle with implementation 'commons-beanutils:commons-beanutils:1.9.4'

but when I try to import import commons-beanutils.commons.beanutils.BeanComparator I always see cannot resolve symbol commons

Does anyone have a working example of using BeanComparator with the import statement? Is using BeanComparator not a good idea because it uses reflection? It seems to do exactly what I want it to do.

I have tried multiple variations import commons-beanutils.commons-beanutils.BeanComparator import commons.beanutils.commons-beanutils.BeanComparator etc.

I have also tried implementation 'org.apache.commons.beanutils:1.9.4' but that doesn't exist

2

There are 2 best solutions below

0
M. Wismer On

I did get it to work but I had to include

implementation 'commons-collections:commons-collections:3.2.1'

implementation 'commons-logging:commons-logging:1.2'

in my build.gradle

0
pfurbacher On

In build.gradle

dependencies{
    // https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils
    implementation 'commons-beanutils:commons-beanutils:1.9.4'
    . . . 
}

In your Java class file:

import java.util.Comparator;
import org.apache.commons.beanutils.BeanComparator;
...
    // Assume you want to compare Person objects based on their age properties.
    private Comparator<Person> bc = new BeanComparator<>("age");

Google "apache BeanComparator example" for lots of usage examples.

But I would use

people.sort((Person p1, Person p2)->p1.getAge()-p2.getAge());

as in

import java.util.Arrays;
import java.util.List;
import java.util.Comparator;
import org.apache.commons.beanutils.BeanComparator;

public class BeanComparatorDemo {

    public static void main(String[] args) {
        List<Person> people = Arrays.asList(new Person("Bart", 28), new Person("Iris", 23), new Person("Dumsani", 71));

        people.sort((Person p1, Person p2) -> p1.getAge() - p2.getAge());
        System.out.println("Sort using comparator lambda: " + people);

        // I wouldn't use BeanComparator because you can do this in regular Java
        // without additional dependencies (and possible vulnerabilities),
        // and BeanComparator seems to be hostile to Java records.
        Comparator<Person> bc = new BeanComparator<>("name");
        people.sort(bc);
        System.out.println("Sort on name using BeanComparator: " + people);
    }

    public static class Person {
        private String name;
        private int age;

        Person(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return this.name;
        }
        public int getAge() { 
            return this.age;
        }
        public String toString() {
            return "Person [" + name + ", " + age + "]";
        }
    }

}

Also, BeanComparator doesn't seem to like Java record.