Skip to main content

Sorting Collections

Aims​

In this lab, you will experiment with more advanced features of collections.

Sorting a list​

  1. Using your preferred IDE, create a new runnable file called "SortingStrings".

  2. Create an arraylist of 10 Strings, containing names of different participants in the course.

  3. Sort the list.

  4. Print out the list.

Click here to view the sample solution

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SortingStrings {

public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("Charlie");
names.add("Alice");
names.add("Jack");
names.add("Grace");
names.add("Diana");
names.add("Eve");
names.add("Bob");
names.add("Hannah");
names.add("Frank");
names.add("Ivy");

Collections.sort(names);
System.out.println("Sorted names: " + names);


}
}

The Comparable interface​

  1. Create a class called "Account", containing fields called name and balance. If you have worked through the Java Primer labs, then you should re-create this account so that there is no inheritance / use of interfaces.

Click here to view the sample class

public class Account {

private Double balance;
private String name;

public Account() {
}

public Account(Double balance, String name) {
this.balance = balance;
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) throws DodgyNameException {
this.name = name;
}

public Double getBalance() {
return balance;
}
public void setBalance(Double balance) {
this.balance = balance;
}


@Override
public String toString() {
return "Account [balance=" + balance + ", name=" + name + "]";
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(balance);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Account other = (Account) obj;
if (Double.doubleToLongBits(balance) != Double.doubleToLongBits(other.balance))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}


}
  1. Create a runnable class called SortingAccounts. In the main method create a list of 5 accounts.

Click here to view the sample class

import java.util.ArrayList;
import java.util.List;

public class SortingAccounts {

public static void main(String[] args) {
List<Account> accounts = new ArrayList<>();
accounts.add(new Account(1000.0, "Alice"));
accounts.add(new Account(2000.0, "Eve"));
accounts.add(new Account(1500.0, "Charlie"));
accounts.add(new Account(750.0, "Diana"));
accounts.add(new Account(500.0, "Bob"));

}
}
  1. Implement the Comparable Interface for the Account class. The default sort order should use the balance field.

  2. Sort the list of accounts and print out the list.

Click here to view the sample solution

public class Account implements Comparable<Account> {

...

public int compareTo(Account other) {
return this.balance.compareTo(other.balance);
}

...

}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SortingAccounts {

public static void main(String[] args) {
List<Account> accounts = new ArrayList<>();
accounts.add(new Account(1000.0, "Alice"));
accounts.add(new Account(2000.0, "Eve"));
accounts.add(new Account(1500.0, "Charlie"));
accounts.add(new Account(750.0, "Diana"));
accounts.add(new Account(500.0, "Bob"));

Collections.sort(accounts);
System.out.println("Sorted accounts by balance:");
System.out.println(accounts);
}
}