Remove duplicate from a java collection - java

public class Employee implements Comparable<Employee> {
private int id;
private String name;
private String salary;
private String recordStatus;
private int key;
public Employee(int id, String name, String salary, int key) {
super();
this.id = id;
this.name = name;
this.salary = salary;
this.key = key;
}
}
Now I have a list of type Employee.
List<Employee> list = new ArrayList<Employee>();
list.add(new Employee(123, "zMadhu", "1000$",1));
list.add(new Employee(332, "bSudhan", "2000$",2));
list.add(new Employee(54, "cKongarass", "3000$",3));
list.add(new Employee(54, "xKongarass", "3000$",4));
list.add(new Employee(54, "aKongarass", "3000$",5));
Now I want to remove data from this list and have only unique IDS. I.E. I am expecting 54,123,332 in another list of type Employee.
Want to see how I can do it. Much appreciate your help here.

First override equals(..) and hashCode() where you use just the id :
...
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Employee)) return false;
Employee employee = (Employee) o;
return id == employee.id;
}
#Override
public int hashCode() {
return id;
}
...
Second Just create a Set<Employee> which will not accept duplicates Objects like so :
Set<Employee> result = new HashSet<>(list);// [54, 123, 332]
Take a look at a simple Ideone demo

First of all, this code won't compile because you don't have the comparable interface implemented. So, I just took that out for now assuming you left it out for brevity :).
Assuming you had that... the most sensible thing would be to use a map in the first place.
Assuming you want to start with this list though, you can convert this to a map and log/remove duplicates with a stream:
Map<Integer, Employee> employees = list.stream()
.collect(Collectors.toMap(k -> k.id, v -> v, (a, b) -> {
System.out.println("Duplicate found! " + a.id + " taking first one.");
return a;
}));
System.out.println(employees);
Results:
Duplicate found! 54 taking first one.
Duplicate found! 54 taking first one.
{54=Employee{id=54, name='cKongarass', salary='3000$',
recordStatus='null', key=3}, 123=Employee{id=123, name='zMadhu',
salary='1000$', recordStatus='null', key=1}, 332=Employee{id=332,
name='bSudhan', salary='2000$', recordStatus='null', key=2}}
Note for employees to print properly you need to add a toString() method to the class.
Person Class toString() Function:
#Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", salary='" + salary + '\'' +
", recordStatus='" + recordStatus + '\'' +
", key=" + key +
'}';
}

If you override the equals method accordingly you can do it this way in java 8+:
import java.util.stream.Collectors;
list.stream().distinct().collect(Collectors.toList())
It's also achievable without overriding the equals method, but more verbose though:
Set<Employee> uniqueSet = new TreeSet<>((e1, e2) -> e1.getId() == e2.getId() ? 0 : 1);
set.addAll(list);
List<Employee> result = new ArrayList<>(uniqueSet);
The lambda passed to the TreeSet constructor gets expanded to an implementation of Comparator<Employee>. Similar to the solution provided by #bsb but using java 8 features.

The easiest way to remove duplicate is by passing the List to a Set and Use Comparator to remove duplicate elements.
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class RemoveDuplicate {
public static void main(String[] args) {
List<Employee> list = new ArrayList<Employee>();
list.add(new Employee(123, "zMadhu", "1000$",1));
list.add(new Employee(332, "bSudhan", "2000$",2));
list.add(new Employee(54, "cKongarass", "3000$",3));
list.add(new Employee(54, "xKongarass", "3000$",4));
list.add(new Employee(54, "aKongarass", "3000$",5));
//Printing original list
for (Employee emp : list) {
System.out.println(emp.getId());
}
Set<Employee> set = new TreeSet<Employee>(new Comparator<Employee>() {
#Override
public int compare(Employee e1, Employee e2) {
return e1.getId() == e2.getId() ? 0 : 1;
}
});
set.addAll(list);
final ArrayList<Employee> newList = new ArrayList<Employee>(set);
System.out.println("\n***** After removing duplicates *******\n");
for (Employee emp : newList) {
System.out.println(emp.getId());
}
}
}

Related

Sorting a list with stream.sorted() in Java

I'm interested in sorting a list from a stream. This is the code I'm using:
list.stream()
.sorted((o1, o2)->o1.getItem().getValue().compareTo(o2.getItem().getValue()))
.collect(Collectors.toList());
Am I missing something? The list is not sorted afterward.
It should sort the lists according to the item with the lowest value.
for (int i = 0; i < list.size(); i++)
{
System.out.println("list " + (i+1));
print(list, i);
}
And the print method:
public static void print(List<List> list, int i)
{
System.out.println(list.get(i).getItem().getValue());
}
This is not like Collections.sort() where the parameter reference gets sorted. In this case you just get a sorted stream that you need to collect and assign to another variable eventually:
List result = list.stream().sorted((o1, o2)->o1.getItem().getValue().
compareTo(o2.getItem().getValue())).
collect(Collectors.toList());
You've just missed to assign the result
Use list.sort instead:
list.sort((o1, o2) -> o1.getItem().getValue().compareTo(o2.getItem().getValue()));
and make it more succinct using Comparator.comparing:
list.sort(Comparator.comparing(o -> o.getItem().getValue()));
After either of these, list itself will be sorted.
Your issue is that
list.stream.sorted returns the sorted data, it doesn't sort in place as you're expecting.
Java 8 provides different utility api methods to help us sort the streams better.
If your list is a list of Integers(or Double, Long, String etc.,) then you can simply sort the list with default comparators provided by java.
List<Integer> integerList = Arrays.asList(1, 4, 3, 4, 5);
Creating comparator on fly:
integerList.stream().sorted((i1, i2) -> i1.compareTo(i2)).forEach(System.out::println);
With default comparator provided by java 8 when no argument passed to sorted():
integerList.stream().sorted().forEach(System.out::println); //Natural order
If you want to sort the same list in reverse order:
integerList.stream().sorted(Comparator.reverseOrder()).forEach(System.out::println); // Reverse Order
If your list is a list of user defined objects, then:
List<Person> personList = Arrays.asList(new Person(1000, "First", 25, 30000),
new Person(2000, "Second", 30, 45000),
new Person(3000, "Third", 35, 25000));
Creating comparator on fly:
personList.stream().sorted((p1, p2) -> ((Long)p1.getPersonId()).compareTo(p2.getPersonId()))
.forEach(person -> System.out.println(person.getName()));
Using Comparator.comparingLong() method(We have comparingDouble(), comparingInt() methods too):
personList.stream().sorted(Comparator.comparingLong(Person::getPersonId)).forEach(person -> System.out.println(person.getName()));
Using Comparator.comparing() method(Generic method which compares based on the getter method provided):
personList.stream().sorted(Comparator.comparing(Person::getPersonId)).forEach(person -> System.out.println(person.getName()));
We can do chaining too using thenComparing() method:
personList.stream().sorted(Comparator.comparing(Person::getPersonId).thenComparing(Person::getAge)).forEach(person -> System.out.println(person.getName())); //Sorting by person id and then by age.
Person class
public class Person {
private long personId;
private String name;
private int age;
private double salary;
public long getPersonId() {
return personId;
}
public void setPersonId(long personId) {
this.personId = personId;
}
public Person(long personId, String name, int age, double salary) {
this.personId = personId;
this.name = name;
this.age = age;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
It seems to be working fine:
List<BigDecimal> list = Arrays.asList(new BigDecimal("24.455"), new BigDecimal("23.455"), new BigDecimal("28.455"), new BigDecimal("20.455"));
System.out.println("Unsorted list: " + list);
final List<BigDecimal> sortedList = list.stream().sorted((o1, o2) -> o1.compareTo(o2)).collect(Collectors.toList());
System.out.println("Sorted list: " + sortedList);
Example Input/Output
Unsorted list: [24.455, 23.455, 28.455, 20.455]
Sorted list: [20.455, 23.455, 24.455, 28.455]
Are you sure you are not verifying list instead of sortedList [in above example] i.e. you are storing the result of stream() in a new List object and verifying that object?
sorting Integer using streamAPI
arr.stream()
.sorted((item1,item2)-> Integer.compare(item1.price, item2.price))
.forEach(item-> item.show());
//asc
System.out.println("--------------------");
//desc
arr.stream()
.sorted((item1,item2)-> item1.price<item2.price?1:-1)
.forEach(item->item.show());
This is a simple example :
List<String> citiesName = Arrays.asList( "Delhi","Mumbai","Chennai","Banglore","Kolkata");
System.out.println("Cities : "+citiesName);
List<String> sortedByName = citiesName.stream()
.sorted((s1,s2)->s2.compareTo(s1))
.collect(Collectors.toList());
System.out.println("Sorted by Name : "+ sortedByName);
It may be possible that your IDE is not getting the jdk 1.8 or upper version to compile the code.
Set the Java version 1.8 for Your_Project > properties > Project Facets > Java version 1.8
This might help for people ending up here searching how to sort list alphabetically.
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class SortService {
public static void main(String[] args) {
List<TestData> test = new ArrayList<>();
test.add(prepareTestData("Asmin",1));
test.add(prepareTestData("saurav",4));
test.add(prepareTestData("asmin",2));
test.add(prepareTestData("Saurav",3));
test.forEach(data-> System.out.println(data));
/** Output
* TestData(name=Asmin, id=1)
* TestData(name=saurav, id=4)
* TestData(name=asmin, id=2)
* TestData(name=Saurav, id=3)
*/
test.sort(Comparator.comparing(TestData::getName,String::compareToIgnoreCase));
test.forEach(data-> System.out.println(data));
/**Sorted Output
* TestData(name=Asmin, id=1)
* TestData(name=asmin, id=2)
* TestData(name=saurav, id=4)
* TestData(name=Saurav, id=3)
*/
}
private static TestData prepareTestData(String name, int id){
TestData testData= new TestData();
testData.setId(id);
testData.setName(name);
return testData;
}
}
#Getter
#Setter
#ToString
class TestData{
private String name;
private int id;
}
Collection<Map<Item, Integer>> itemCollection = basket.values();
Iterator<Map<Item, Integer>> itemIterator = itemCollection.stream().sorted(new TestComparator()).collect(Collectors.toList()).iterator();
package com.ie.util;
import com.ie.item.Item;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class TestComparator implements Comparator<Map<Item, Integer>> {
// comparator is used to sort the Items based on the price
#Override
public int compare(Map<Item, Integer> o1, Map<Item, Integer> o2) {
// System.out.println("*** compare method will be called *****");
Item item1 = null;
Item item2 = null;
Set<Item> itemSet1 = o1.keySet();
Iterator<Item> itemIterator1 = itemSet1.iterator();
if(itemIterator1.hasNext()){
item1 = itemIterator1.next();
}
Set<Item> itemSet2 = o2.keySet();
Iterator<Item> itemIterator2 = itemSet2.iterator();
if(itemIterator2.hasNext()){
item2 = itemIterator2.next();
}
return -item1.getPrice().compareTo(item2.getPrice());
}
}
**** this is helpful to sort the nested map objects like Map> here i sorted based on the Item object price .
Using Comparator:
List<Type> result = list
.stream()
.sorted(Comparator.comparing(Type::getValue))
.collect(Collectors.toList());

Cannot add element to ObservableList (UnsupportedOperationException) in JavaFX [duplicate]

This question already has answers here:
Why do I get an UnsupportedOperationException when trying to remove an element from a List?
(17 answers)
Closed 6 years ago.
Initial Starting Point
I have an existing List of 1000 Person objects which I would like to insert an Extractor to listen for property changes within any Person objects (this ObservableList will be later attached to a TableView).
So my code would be like:
ObservableList<Person> observablePersons = FXCollections.observableList(personlist,
personextractor);
Error Message
But when I try to add a new person objects to this ObservableList observablePersons, I run into this error:
run:
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:148)
at com.sun.javafx.collections.ObservableListWrapper.doAdd(ObservableListWrapper.java:101)
at javafx.collections.ModifiableObservableListBase.add(ModifiableObservableListBase.java:151)
at java.util.AbstractList.add(AbstractList.java:108)
at test.listchangelistener.listChangeDemo.main(listChangeDemo.java:72)
Java Result: 1
Could you please tell me why would I come across this error message? My java version is jdk1.8.0_91 (32-Bit)
Person Class
package test.listchangelistener;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
class Person {
private final IntegerProperty age = new SimpleIntegerProperty();
private final StringProperty name = new SimpleStringProperty();
public Person(String name, Integer age) {
setName(name);
setAge(age);
}
public int getAge() {
return age.get();
}
public final void setAge(int value) {
age.set(value);
}
public IntegerProperty ageProperty() {
return age;
}
public String getName() {
return name.get();
}
public final void setName(String value) {
name.set(value);
}
public StringProperty nameProperty() {
return name;
}
#Override
public String toString() {
return "Person{" + "age=" + age.get() + ", name=" + name.get() + '}';
}
}
Test Code
package test.listchangelistener;
import java.util.Arrays;
import java.util.List;
import javafx.beans.Observable;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.util.Callback;
public class listChangeDemo {
public static void main(String[] args) {
Person p1 = new Person("Ted", 26);
Person p2 = new Person("Anne", 19);
// just a simple list
List<Person> persons = Arrays.asList(p1, p2);
// extractor to observe change of person properties
Callback<Person, Observable[]> extractor = (Person p) -> {
return new Observable[]{
p.ageProperty(),
p.nameProperty()
};
};
// make list observable and attach extractor
ObservableList<Person> observablePersons = FXCollections.observableList(persons, extractor);
// create listchangeListener for observableList
ListChangeListener listener = (ListChangeListener) (ListChangeListener.Change c) -> {
while (c.next()) {
if (c.wasAdded()) {
System.out.println("these were added: ");
List addedSubList = c.getAddedSubList();
addedSubList.forEach((Object t) -> {
System.out.println("added Person: " + t);
});
} else if (c.wasRemoved()) {
System.out.println("these were removed");
List removedSubList = c.getRemoved();
removedSubList.forEach((Object t) -> {
System.out.println("removed Person: " + t);
});
} else if (c.wasUpdated()) {
System.out.println("these were updated");
System.out.println("Updated elements are: "
+ c.getList().subList(c.getFrom(), c.getTo()));
}
}
};
// attach listchangeListener to observableList
observablePersons.addListener(listener);
// testing changes
observablePersons.add(new Person("Siegfried", 10));
}
}
The problem is coming from the creation of your backing List:
List<Person> persons = Arrays.asList(p1, p2);
If you take a look on the javadoc of Arrays.asList:
Returns a fixed-size list backed by the specified array.
On a closer look you got an UnsupportedOperationException because List.add() is an optional operation:
Throws:
UnsupportedOperationException - if the add operation is not supported by this list.
You can update the creation of the backing list as:
List<Person> persons = new ArrayList<Person>(Arrays.asList(p1, p2));
The difference is that the returned ArrayList is an exact, independent copy of the passed one, therefore the restriction described above is not valid anymore in this case.
You can also take a look on this question which explaines this topic in depths:
Difference between Arrays.asList(array) vs new ArrayList<Integer>(Arrays.asList(ia)) in java

How to sort an ArrayList of type Student? [duplicate]

I have simple class
public class ActiveAlarm {
public long timeStarted;
public long timeEnded;
private String name = "";
private String description = "";
private String event;
private boolean live = false;
}
and List<ActiveAlarm> con. How to sort in ascending order by timeStarted, then by timeEnded? Can anybody help? I know in C++ with generic algorithm and overload operator <, but I am new to Java.
Using Comparator
For Example:
class Score {
private String name;
private List<Integer> scores;
// +accessor methods
}
Collections.sort(scores, new Comparator<Score>() {
public int compare(Score o1, Score o2) {
// compare two instance of `Score` and return `int` as result.
return o2.getScores().get(0).compareTo(o1.getScores().get(0));
}
});
With Java 8 onwards, you can simply use lambda expression to represent Comparator instance.
Collections.sort(scores, (s1, s2) -> { /* compute and return int */ });
Either make ActiveAlarm implement Comparable<ActiveAlarm> or implement Comparator<ActiveAlarm> in a separate class. Then call:
Collections.sort(list);
or
Collections.sort(list, comparator);
In general, it's a good idea to implement Comparable<T> if there's a single "natural" sort order... otherwise (if you happen to want to sort in a particular order, but might equally easily want a different one) it's better to implement Comparator<T>. This particular situation could go either way, to be honest... but I'd probably stick with the more flexible Comparator<T> option.
EDIT: Sample implementation:
public class AlarmByTimesComparer implements Comparator<ActiveAlarm> {
#Override
public int compare(ActiveAlarm x, ActiveAlarm y) {
// TODO: Handle null x or y values
int startComparison = compare(x.timeStarted, y.timeStarted);
return startComparison != 0 ? startComparison
: compare(x.timeEnded, y.timeEnded);
}
// I don't know why this isn't in Long...
private static int compare(long a, long b) {
return a < b ? -1
: a > b ? 1
: 0;
}
}
JAVA 8 and Above Answer (Using Lambda Expressions)
In Java 8, Lambda expressions were introduced to make this even easier! Instead of creating a Comparator() object with all of it's scaffolding, you can simplify it as follows: (Using your object as an example)
Collections.sort(list, (ActiveAlarm a1, ActiveAlarm a2) -> a1.timeStarted-a2.timeStarted);
or even shorter:
Collections.sort(list, Comparator.comparingInt(ActiveAlarm ::getterMethod));
That one statement is equivalent to the following:
Collections.sort(list, new Comparator<ActiveAlarm>() {
#Override
public int compare(ActiveAlarm a1, ActiveAlarm a2) {
return a1.timeStarted - a2.timeStarted;
}
});
Think of Lambda expressions as only requiring you to put in the relevant parts of the code: the method signature and what gets returned.
Another part of your question was how to compare against multiple fields. To do that with Lambda expressions, you can use the .thenComparing() function to effectively combine two comparisons into one:
Collections.sort(list, (ActiveAlarm a1, ActiveAlarm a2) -> a1.timeStarted-a2.timeStarted
.thenComparing ((ActiveAlarm a1, ActiveAlarm a2) -> a1.timeEnded-a2.timeEnded)
);
The above code will sort the list first by timeStarted, and then by timeEnded (for those records that have the same timeStarted).
One last note: It is easy to compare 'long' or 'int' primitives, you can just subtract one from the other. If you are comparing objects ('Long' or 'String'), I suggest you use their built-in comparison. Example:
Collections.sort(list, (ActiveAlarm a1, ActiveAlarm a2) -> a1.name.compareTo(a2.name) );
EDIT: Thanks to Lukas Eder for pointing me to .thenComparing() function.
We can sort the list in one of two ways:
1. Using Comparator : When required to use the sort logic in multiple places
If you want to use the sorting logic in a single place, then you can write an anonymous inner class as follows, or else extract the comparator and use it in multiple places
Collections.sort(arrayList, new Comparator<ActiveAlarm>() {
public int compare(ActiveAlarm o1, ActiveAlarm o2) {
//Sorts by 'TimeStarted' property
return o1.getTimeStarted()<o2.getTimeStarted()?-1:o1.getTimeStarted()>o2.getTimeStarted()?1:doSecodaryOrderSort(o1,o2);
}
//If 'TimeStarted' property is equal sorts by 'TimeEnded' property
public int doSecodaryOrderSort(ActiveAlarm o1,ActiveAlarm o2) {
return o1.getTimeEnded()<o2.getTimeEnded()?-1:o1.getTimeEnded()>o2.getTimeEnded()?1:0;
}
});
We can have null check for the properties, if we could have used 'Long' instead of 'long'.
2. Using Comparable(natural ordering): If sort algorithm always stick to one property:
write a class that implements 'Comparable' and override 'compareTo' method as defined below
class ActiveAlarm implements Comparable<ActiveAlarm>{
public long timeStarted;
public long timeEnded;
private String name = "";
private String description = "";
private String event;
private boolean live = false;
public ActiveAlarm(long timeStarted,long timeEnded) {
this.timeStarted=timeStarted;
this.timeEnded=timeEnded;
}
public long getTimeStarted() {
return timeStarted;
}
public long getTimeEnded() {
return timeEnded;
}
public int compareTo(ActiveAlarm o) {
return timeStarted<o.getTimeStarted()?-1:timeStarted>o.getTimeStarted()?1:doSecodaryOrderSort(o);
}
public int doSecodaryOrderSort(ActiveAlarm o) {
return timeEnded<o.getTimeEnded()?-1:timeEnded>o.getTimeEnded()?1:0;
}
}
call sort method to sort based on natural ordering
Collections.sort(list);
In java8+ this can be written in single line as follows:
collectionObjec.sort(comparator_lamda) or comparator.comparing(CollectionType::getterOfProperty)
code:
ListOfActiveAlarmObj.sort((a,b->a.getTimeStarted().compareTo(b.getTimeStarted())))
or
ListOfActiveAlarmObj.sort(Comparator.comparing(ActiveAlarm::getTimeStarted))
public class ActiveAlarm implements Comparable<ActiveAlarm> {
public long timeStarted;
public long timeEnded;
private String name = "";
private String description = "";
private String event;
private boolean live = false;
public int compareTo(ActiveAlarm a) {
if ( this.timeStarted > a.timeStarted )
return 1;
else if ( this.timeStarted < a.timeStarted )
return -1;
else {
if ( this.timeEnded > a.timeEnded )
return 1;
else
return -1;
}
}
That should give you a rough idea. Once that's done, you can call Collections.sort() on the list.
Since Java8 this can be done even cleaner using a combination of Comparator and Lambda expressions
For Example:
class Student{
private String name;
private List<Score> scores;
// +accessor methods
}
class Score {
private int grade;
// +accessor methods
}
Collections.sort(student.getScores(), Comparator.comparing(Score::getGrade);
Java-8 solution using Stream API:
A. When timeStarted and timeEnded are public (as mentioned in the requirement) and therefore do not (need to) have public getter methods:
List<ActiveAlarm> sorted =
list.stream()
.sorted(Comparator.comparingLong((ActiveAlarm alarm) -> alarm.timeStarted)
.thenComparingLong((ActiveAlarm alarm) -> alarm.timeEnded))
.collect(Collectors.toList());
B. When timeStarted and timeEnded have public getter methods:
List<ActiveAlarm> sorted =
list.stream()
.sorted(Comparator.comparingLong(ActiveAlarm::getTimeStarted)
.thenComparingLong(ActiveAlarm::getTimeEnded))
.collect(Collectors.toList());
If you want to sort the original list itself:
A. When timeStarted and timeEnded are public (as mentioned in the requirement) and therefore do not (need to) have public getter methods:
list.sort(Comparator.comparingLong((ActiveAlarm alarm) -> alarm.timeStarted)
.thenComparingLong((ActiveAlarm alarm) -> alarm.timeEnded));
B. When timeStarted and timeEnded have public getter methods:
list.sort(Comparator.comparingLong(ActiveAlarm::getTimeStarted)
.thenComparingLong(ActiveAlarm::getTimeEnded));
Guava's ComparisonChain:
Collections.sort(list, new Comparator<ActiveAlarm>(){
#Override
public int compare(ActiveAlarm a1, ActiveAlarm a2) {
return ComparisonChain.start()
.compare(a1.timestarted, a2.timestarted)
//...
.compare(a1.timeEnded, a1.timeEnded).result();
}});
We can use the Comparator.comparing() method to sort a list based on an object's property.
class SortTest{
public static void main(String[] args) {
ArrayList<ActiveAlarm> activeAlarms = new ArrayList<>(){{
add(new ActiveAlarm("Alarm 1", 5, 10));
add(new ActiveAlarm("Alarm 2", 2, 12));
add(new ActiveAlarm("Alarm 3", 0, 8));
}};
/* I sort the arraylist here using the getter methods */
activeAlarms.sort(Comparator.comparing(ActiveAlarm::getTimeStarted)
.thenComparing(ActiveAlarm::getTimeEnded));
System.out.println(activeAlarms);
}
}
Note that before doing it, you'll have to define at least the getter methods of the properties you want to base your sort on.
public class ActiveAlarm {
public long timeStarted;
public long timeEnded;
private String name = "";
private String description = "";
private String event;
private boolean live = false;
public ActiveAlarm(String name, long timeStarted, long timeEnded) {
this.name = name;
this.timeStarted = timeStarted;
this.timeEnded = timeEnded;
}
public long getTimeStarted() {
return timeStarted;
}
public long getTimeEnded() {
return timeEnded;
}
#Override
public String toString() {
return name;
}
}
Output:
[Alarm 3, Alarm 2, Alarm 1]
Employee POJO Class
package in.ac.adit.oop.sort;
public class Employee {
private int id;
private String name;
private String department;
public int getId() {
return id;
}
public Employee() {
super();
}
public Employee(int id, String name, String department) {
super();
this.id = id;
this.name = name;
this.department = department;
}
#Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", department=" + department + "]";
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
}
Employee Class To Manage Employee
package in.ac.adit.oop.sort;
import java.util.ArrayList;
import java.util.List;
public class Example {
public static void main(String[] args) {
/*
* Create 10 Employee Object
*/
Employee emp1 = new Employee(1, "Nayan", "IT");
Employee emp2 = new Employee(2, "Siddarth", "CP");
Employee emp3 = new Employee(3, "Samarth", "AE");
Employee emp4 = new Employee(4, "Bhavesh", "CV");
Employee emp5 = new Employee(5, "Sam", "FT");
Employee emp6 = new Employee(6, "Keyur", "IT");
Employee emp7 = new Employee(7, "Bala", "ME");
Employee emp8 = new Employee(8, "Mitul", "ME");
Employee emp9 = new Employee(9, "Kamlesh", "EE");
Employee emp10 = new Employee(10, "Piyush", "EE");
/*
* List of Employee Object
*/
List<Employee> employeeList = new ArrayList<Employee>();
employeeList.add(emp1);
employeeList.add(emp2);
employeeList.add(emp3);
employeeList.add(emp4);
employeeList.add(emp5);
employeeList.add(emp6);
employeeList.add(emp7);
employeeList.add(emp8);
employeeList.add(emp9);
employeeList.add(emp10);
CustomObjectSort customObjectSort = new CustomObjectSort();
List<Employee> sortByDepartment = customObjectSort.sortByDepartment(employeeList);
/*
* Sorted By Department
*/
for (Employee employee : sortByDepartment) {
System.out.println(employee);
}
/*
* Sorted By Name
*/
List<Employee> sortByName = customObjectSort.sortByName(employeeList);
for (Employee employee : sortByName) {
System.out.println(employee);
}
/*
* Sorted By Id
*/
List<Employee> sortById = customObjectSort.sortById(employeeList);
for (Employee employee : sortById) {
System.out.println(employee);
}
}
}
Custom Sorting
package in.ac.adit.oop.sort;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CustomObjectSort {
public List<Employee> sortByName(List<Employee> employeeList) {
Collections.sort(employeeList, new Comparator<Employee>() {
#Override
public int compare(Employee employee1, Employee employee2) {
return employee1.getName().compareTo(employee2.getName());
}
});
return employeeList;
}
public List<Employee> sortByDepartment(List<Employee> employeeList) {
Collections.sort(employeeList, new Comparator<Employee>() {
#Override
public int compare(Employee employee1, Employee employee2) {
return employee1.getDepartment().compareTo(employee2.getDepartment());
}
});
return employeeList;
}
public List<Employee> sortById(List<Employee> employeeList) {
Collections.sort(employeeList, new Comparator<Employee>() {
#Override
public int compare(Employee employee1, Employee employee2) {
return employee1.getId() - employee2.getId();
}
});
return employeeList;
}
}
You can use Collections.sort and pass your own Comparator<ActiveAlarm>
In java you need to use the static Collections.sort method. Here is an example for a list of CompanyRole objects, sorted first by begin and then by end. You can easily adapt for your own object.
private static void order(List<TextComponent> roles) {
Collections.sort(roles, new Comparator() {
#Override
public int compare(Object o1, Object o2) {
int x1 = ((CompanyRole) o1).getBegin();
int x2 = ((CompanyRole) o2).getBegin();
if (x1 != x2) {
return x1 - x2;
} else {
int y1 = ((CompanyRole) o1).getEnd();
int y2 = ((CompanyRole) o2).getEnd();
return y2 - y1;
}
}
});
}
You can call Collections.sort() and pass in a Comparator which you need to write to compare different properties of the object.
As mentioned you can sort by:
Making your object implement Comparable
Or pass a Comparator to Collections.sort
If you do both, the Comparable will be ignored and Comparator will be used. This helps that the value objects has their own logical Comparable which is most reasonable sort for your value object, while each individual use case has its own implementation.
public class ActiveAlarm {
public long timeStarted;
public long timeEnded;
private String name = "";
private String description = "";
private String event;
private boolean live = false;
}
List<ActiveAlarm> con = new ArrayList<ActiveAlarm>();
Collections.sort(con , (a1, a2) -> a1.timeStarted.compareTo(a2.timeStarted));
Collections.sort(con , (a1, a2) -> a1.timeEnded.compareTo(a2.timeEnded));
Here's what did the trick for me.
Was much shorter and easier than everything else I found:
Collections.sort(listName, Comparator.comparing(Object::getProperty).reversed());
The ".reversed()" part at the end was a requirement for my specific project but I'm sharing it too, as it took a while to find it
The best and the easiest way to sort any list of objects in Java (Java 8 and above).
Lets sort a basket of fruits based on the property "fruitName"
Fruit POJO:
class Fruit
{
int price;
String fruitName;
public Fruit(int price, String fruitName) {
super();
this.price = price;
this.fruitName = fruitName;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getFruitName() {
return fruitName;
}
public void setFruitName(String fruitName) {
this.fruitName = fruitName;
}
#Override
public String toString() {
return "Fruits [price=" + price + ", fruitName=" + fruitName + "]";
}
}
Now lets add fruits into a list and then sort it
List<Fruit> basketOfFruits = new ArrayList<>();
basketOfFruits.add(new Fruit(123, "oranges"));
basketOfFruits.add(new Fruit(45, "nectarine"));
basketOfFruits.add(new Fruit(369, "blueberries"));
basketOfFruits.add(new Fruit(248, "apple"));
basketOfFruits.add(new Fruit(968, "peaches"));
basketOfFruits.add(new Fruit(436, "grapes"));
basketOfFruits.add(new Fruit(596, "figs"));
//sorting by the property fruitName
Collections.sort(basketOfFruits, (f1, f2)->{return f1.getFruitName().compareTo(f2.getFruitName());});
You can now print the list (i.e basketOfFruits) and the fruits in the list would be sorted in ASCENDING order (lexicographically).
The output would look like this:
[Fruits [price=248, fruitName=apple], Fruits [price=369, fruitName=blueberries], Fruits [price=596, fruitName=figs], Fruits [price=436, fruitName=grapes], Fruits [price=45, fruitName=nectarine], Fruits [price=123, fruitName=oranges], Fruits [price=968, fruitName=peaches]]
Instead of Collections.sort(), Java streams can also be used (Java 8 and above). The following is the code using Java streams
List<Fruit> sortedFruits = basketOfFruits.stream().sorted( (f1, f2)->{return f1.getFruitName().compareTo(f2.getFruitName());}).collect(Collectors.toList());
here the list is sorted in the same manner as Collections.sort(), but the sorted items would be stored/collected in another list "sortedFruits". So, if we want to print the sorted items of the list, we need to print "sortedFruits" instead of "basketOfFruits" in this case

How to compare two array lists for similar objects which differ in at least one property in java?

I have two array list. Each has list of Objects of type User.
The User class looks like below
public class User {
private long id;
private String empCode;
private String firstname;
private String lastname;
private String email;
public User( String firstname, String lastname, String empCode, String email) {
super();
this.empCode = empCode;
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
}
// getters and setters
}
import java.util.ArrayList;
import java.util.List;
public class FindSimilarUsersWithAtLeastOneDifferentProperty {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List<User> list1 = new ArrayList<User>();
list1.add(new User("F11", "L1", "EMP01", "u1#test.com"));
list1.add(new User("F2", "L2", "EMP02", "u222#test.com"));
list1.add(new User("F3", "L3", "EMP03", "u3#test.com"));
list1.add(new User("F4", "L4", "EMP04", "u4#test.com"));
list1.add(new User("F5", "L5", "EMP05", "u5#test.com"));
list1.add(new User("F9", "L9", "EMP09", "u9#test.com"));
list1.add(new User("F10", "L10", "EMP10", "u10#test.com"));
List<User> list2 = new ArrayList<User>();
list2.add(new User("F1", "L1", "EMP01", "u1#test.com"));
list2.add(new User("F2", "L2", "EMP02", "u2#test.com"));
list2.add(new User("F6", "L6", "EMP06", "u6#test.com"));
list2.add(new User("F7", "L7", "EMP07", "u7#test.com"));
list2.add(new User("F8", "L8", "EMP08", "u8#test.com"));
list2.add(new User("F9", "L9", "EMP09", "u9#test.com"));
list2.add(new User("F100", "L100", "EMP10", "u100#test.com"));
List<User> resultList = new ArrayList<User>();
// this list should contain following users
// EMP01 (common in both list but differs in firstname)
// EMP02 (common in both list but differs in email)
// EMP10 (common in both list but differs in firstname, lastname and email)
}
}
If you see the sample code, the two lists have four users with emp code EMP01, EMP02, EMP09 and EMP10 common.
So, we only need to compare the properties of these four users.
If any of the users have at least one different property it should be added in the result list.
Please advise on how do I go about this?
Implement equals, hashcode in User
#Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (!(obj instanceof User))
return false;
User u = (User) obj;
return this.empCode == null ? false : this.empCode
.equals(u.empCode);
}
#Override
public int hashCode() {
return this.empCode == null ? 0 : this.empCode.hashCode();
}
#Override
public String toString() {
return "Emp Code: " + this.empCode;
}
Then use retainAll
list2.retainAll(list1);-->EMP01, EMP02, EMP09, EMP10
I think this what you should do -
for(User user1 : list1) {
for(User user2 : list2) {
if(user1.getEmpCode().equals(user2.getEmpCode())) {
if(!user1.getFirstName().equals(user2.getFirstName()) ||
!user1.getLastName().equals(user2.getLastName()) ||
!user1.getEmail().equals(user2.getEmail())) {
resultList.add(user1);
}
}
}
}
It might not make sense for the User to override equal and hashCode only to serve this purpose. They should be overriden in the way in which it makes more sense domain-wise.
This is simple. Override equal method in your User class. One very simple implementation(you can enhance it by using null checks etc) can be like below:
#override
public boolean equals(Object obj) {
User other = (User) obj;
if(this.id==other.id
&& this.empCode.equals(other.empCode)
&& this.firstname.equals(other.firstname)
&& this.lastname.equals(other.lastname)
&& this.email.equals(other.email)){
return true;
}else{
return false;
}
}
Once done, you can use:
for(user user: list1){
if(!resultList.contains(user)){
resultList.add(user);
}
}
for(user user: list2){
if(!resultList.contains(user)){
resultList.add(user);
}
}
The canonical approach is as follows:
Write a method countDifferences that counts the number of differences between users
For each object in one list, find the minimum when compared to the other lists objects
Report all objects where the minimum is not 0.
If you put weights on the different properties you can also control that e.g. a match in the ID attribute is stronger than a match in the name.
Update: sorry, misread your comment that the ID attribute must match.
Replace 2) with "find object which has the same ID". Other than that, I still recommend counting the number of differences. It is more flexible, as you can define thresholds for good or bad matches etc.
Add this method to your User class:
public boolean isSimilarButNotEqual(User other) {
if(!this.empCode.equals(other.empCode))
return false;
return !(this.firstname + this.lastname + this.email).equals(other.firstname + other.lastname + other.email);
}
Then, in the main() do:
for(User user1: list1){
for(User user2: list2){
if(user1.isSimilarButNotEqual(user2)){
resultList.add(user1);
resultList.add(user2);
}
}
}
I have used the following way to compare two custom ArrayList.
List<SinglePostData> allPosts = new ArrayList<>();
List<SinglePostData> noRepeatAllPosts = new ArrayList<>();
for (int i = 0; i < allPosts.size(); i++) {
boolean isFound = false;
for (int j = i+1; j < allPosts.size(); j++) {
if (allPosts.get(i).getTitle().equals(allPosts.get(j).getTitle())) {
isFound = true;
break;
}
}
if (!isFound) noRepeatAllPosts.add(allPosts.get(i));
}

How can I order TreeMaps or ArrayLists holding Persons based on their ID, name, or birthdate?

I have tried almost everything and I can't seem to get my lists to order themselves.
Here's some code:
private List<Person> names = new ArrayList<Person>();
private Map<Integer, Person> peopleMap = new TreeMap <Integer, Person>();
for(int i = 0; i<20; i++)
{
Person personOne = new Person();
peopleMap.put(personOne.id,personOne);
names.add(personOne);
}
Collections.sort(names);
run();
}
My Person class:
public class Person implements Comparable {
public String name;
public int id;
public Date birthdate;
static int idRecord = 0;
The values are filled with randoms. My date has a date format.
I also have a toString method inside my person class, but for some reason when I try to print my maps it gives me the hashcode (this is the hashcode right?) Person#a62fc3.
Here is my toString inside the person clasS:
public String toString()
{
char tab = '\t';
return ("ID Number: "+id+tab+" Name: "+tab+name+tab+" Birthdate: "+(birthdate.toString()));
}
I should add that I am not able to call my toString method inside my person class. Because it is printing Person#a62fc3.
public void sortByID()
{
char tab = '\t';
for (int i = 1; i<20; i++)
System.out.println((peopleMap.get(i)).toString());
//System.out.println("ID Number: "+(peopleMap.get(i).id)+tab+" Name: "+tab+peopleMap.get(i).name+tab+" Birthdate: "+peopleMap.get(i).birthdate);
run();
}
The commented code will work but the code calling the toString does not print what it should
Compare to method inside of my Person class:
public int compareTo(Object obj) {
Person o = (Person) obj;
if (this.id == o.id) { return 0; }
if (this.id > o.id) { return 1; }
if (this.id < o.id) { return -1; }
return 0;
I can provide more code if it's needed.
Compare by name method and it's output. Should I make an arrayList to store my values in and then sort it in that?
public void sortByName()
{
// char tab = '\t';
for(int j = 1; j<20; j++)
{
// System.out.println("ID Number: "+(names.get(j).id)+tab+" Name: "+tab+peopleMap.get(j).name+tab+" Birthdate: "+peopleMap.get(i).birthdate);
//Person p = names.get(j);
System.out.println(names.get(j).toString());
}
}
Output:
Person#10b30a7
Person#1a758cb
Person#1b67f74
Person#69b332
Person#173a10f
Person#530daa
Person#a62fc3
Person#89ae9e
Person#1270b73
Person#60aeb0
Person#16caf43
Person#66848c
Person#8813f2
Person#1d58aae
Person#83cc67
Person#e09713
Person#de6f34
Person#156ee8e
Person#47b480
Thanks
Well, I can't pinpoint the exact problem, I have a few suggestions.
Maps aren't sorted.
In general, an Map is not sorted, so you will not be able to sort the keys of the map. If you want to sort the Map use the SortedMap interface.
Use Generics when possible
The Comparable interface is generic. You should probably be implementing Comparable<Person>
Then your compareTo() method should look like this:
public int compareTo(Person p) {
if (this.id > p.id) return 1;
else if (this.id < p.id) return -1;
else return 0;
}
The difference between Comparator<Person> and Comparable<Person>
You need to take a look at the Comparator interface as well as the Comparable interface.
Your Person should implement comparable in that way that you usually want a person to be sorted. Then you should write some implementations of Comparator.
public classPersonNameComparator implements Comparator<Person> {
public int compare(Person p1, Person p2) {
return p1.name.compareTo(p2.name);
}
}
The importance of using the #Override annotation
It is important to always use the #Override annotation whenever you are trying to override a method of a super class or implement an interface method. The following are a few links regarding why this is a good idea:
Overriding the java equals() method quirk
When do you use Java's #Override annotation and why?
One issue that I see is that TreeMap sorts by key not by value. Your compareTo will not be used in the sorting of the tree since it is the value in the map. Since the key in the map is the id the the items in the tree should be sorted by the id of the person.
How do you know that the map isn't sorted? Can you show us some output that shows that it is not? Are you by any chance changing the ID of the Person after it gets put into the map?
Oh, and what is names compared to personMap? Also, are the ids really contiguous starting from 1? What does this code spit out:
for (Person person : peopleMap.values()) {
System.out.println(person);
}
did you use the #Override method to make sure that you are actually overriding the toString method? It looks like it is still printing out the default toString() (ie the value of the pointer to the object).
see : comparator API.
"The ordering imposed by a Comparator c on a set of elements S is said to be consistent with equals if and only if (compare((Object)e1, (Object)e2)==0) has the same boolean value as e1.equals((Object)e2) for every e1 and e2 in S."
I don't see an equals method in your Person class. The default implementation of equals compares identity. And if you override equals, you must define hashCode two.
And this question : Consistent Equals() results, but inconsistent TreeMap.containsKey() result
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
public class Person implements Comparable<Person> {
public final String name;
public final int id;
public final Date birthdate;
public Person(int id, String name, Date birthdate) {
this.id = id;
this.name = name;
this.birthdate = birthdate;
}
public static void main(String[] args) {
List<Person> list = new ArrayList<Person>();
for (int i = 10; i > 0; i--) {
list.add(new Person(i, "name" + String.valueOf(i), new Date()));
}
System.out.println(list);
Collections.sort(list);
System.out.println(list);
}
#Override
public boolean equals(Object other) {
if (!(other instanceof Person)) {
return false;
}
return this.id == ((Person)other).id;
}
#Override
public int hashCode() {
return 41 * id;
}
#Override
public String toString() {
return "Person<" + id + ">";
}
#Override
public int compareTo(Person other) {
if (!(other instanceof Person)) {
throw new IllegalArgumentException();
}
return this.id - ((Person)other).id;
}
}
Outputs :
[Person<10>, Person<9>, Person<8>, Person<7>, Person<6>, Person<5>, Person<4>, Person<3>, Person<2>, Person<1>]
[Person<1>, Person<2>, Person<3>, Person<4>, Person<5>, Person<6>, Person<7>, Person<8>, Person<9>, Person<10>]

Categories

Resources