An example of how to use LinkedList in Java, how to sort the items and how to display items
The following example , written in Java , shows how to use the class LinkedList. Using two classes, one to create objects of type Employee, another to create objects of type Product. To view the list of items in the list, using two different approaches: the first uses for( ), the second uses the while( ).
public class Employee implements Comparable<Object>
{
private String surname;
private String name;
private int age;
public Employee(String s, String n, int a)
{
surname = s;
name = n;
setAge(a);
}
public int getAge() {
return age;
}
private void setAge(int age) {
this.age = age;
}
public String ToString()
{
return surname + "\t" + name + "\t (" + getAge() + ")";
}
@Override
public int compareTo(Object o) {
return this.getAge() - (((Employee)o).getAge());
}
}
public class Product implements Comparable<Object>
{
private String name;
private String brand;
private double price;
public Product(String n, String b, double p)
{
name = n;
setBrand(b);
price = p;
}
public String getBrand() {
return brand;
}
private void setBrand(String brand) {
this.brand = brand;
}
public String ToString()
{
return name + "("+getBrand()+")\t € " + price;
}
@Override
public int compareTo(Object o) {
return (int)(this.price - (((Product)o).price));
}
}
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class GenericsTest
{
public static void main (String [] args)
{
// Create a LinkedList
List<Employee> employeesList = new LinkedList<Employee>();
// Fill LinkedList with some employees
fillEmployeesList(employeesList);
// Print the contents of the list
printEmployeesList(employeesList);
System.out.println();
// Sort employeesList
// See compareTo(Object o) of Employees class
Collections.sort(employeesList);
// Print the contents of the list
printEmployeesList(employeesList);
System.out.println();
// Create a LinkedList
List<Product> productsList = new LinkedList<Product>();
// Fill LinkedList with some products
fillProductsList(productsList);
// Print the contents of the list
printProductsList(productsList);
System.out.println();
// Sort productsList
// See compareTo(Object o) of Products class
Collections.sort(productsList);
// Print the contents of the list
printProductsList(productsList);
System.out.println();
// Display the names of employees who have more than 30 years
System.out.println("Enter the age");
Scanner scanner = new Scanner(System.in);
int limitAge = scanner.nextInt();
printEmployeesList(employeesList, limitAge);
System.out.println();
// Display the products of selected brand
System.out.println("Enter a brand");
String brand = scanner.next();
printProductsList(productsList, brand);
// Close the scanner
scanner.close();
}
/**
* Fills the list with the values of employees
* @param employeesList
*/
private static void fillEmployeesList(List<Employee> employeesList) {
employeesList.add(new Employee("Bart", "Desy", 34));
employeesList.add(new Employee("Stenn", "Orny", 28));
employeesList.add(new Employee("Lust", "Barbara", 31));
employeesList.add(new Employee("Marriot", "Bill", 36));
employeesList.add(new Employee("Fuert", "Carl", 54));
employeesList.add(new Employee("Cant", "Donald", 48));
employeesList.add(new Employee("Von", "Jack", 22));
}
/**
* Print the list of employees
* @param employeesList
*/
private static void printEmployeesList(List<Employee> employeesList) {
System.out.println("Employee list:".toUpperCase());
System.out.println("Surn.\tName\tAge");
System.out.println(".....................");
// Print all items in the list, using for( )
for(Employee e : employeesList) {
System.out.println(e.ToString());
}
}
/**
* Display the names of employees who have more than 35 years
* @param employeesList
* @param limitAge
*/
private static void printEmployeesList(List<Employee> employeesList, int limitAge) {
System.out.println("Employee list (Age>=".toUpperCase() + limitAge + ")");
System.out.println("Surn.\tName\tAge");
System.out.println(".....................");
// Print all items in the list
for(Employee e : employeesList) {
if(e.getAge()>=limitAge) {
System.out.println(e.ToString());
}
}
}
/**
* Fills the list with the values of products
* @param productsList
*/
private static void fillProductsList(List<Product> productsList) {
productsList.add(new Product("Netbook","LENOVO", 260.00));
productsList.add(new Product("Desktop", "LENOVO", 780.56));
productsList.add(new Product("Notebook", "ASUS", 560.99));
productsList.add(new Product("Netbook","ASUS", 290.00));
productsList.add(new Product("Netbook","ACER", 260.00));
productsList.add(new Product("Server", "DELL", 1630.00));
productsList.add(new Product("Notebook", "HP", 760.50));
productsList.add(new Product("Notebook", "DELL", 633.90));
productsList.add(new Product("Server", "HP", 1499.00));
productsList.add(new Product("Desktop", "ACER", 680.48));
}
/**
* Print the list of products
* @param productsList
*/
private static void printProductsList(List<Product> productsList) {
System.out.println("Products list:".toUpperCase());
System.out.println("Product\t\tPrice");
System.out.println(".....................");
// Print all items in the list, using while( )
Iterator<Product> itrProducts = productsList.iterator();
while(itrProducts.hasNext()) {
Product p =itrProducts.next();
System.out.println(p.ToString());
}
}
/**
* Print the list of products
* @param productsList
*/
private static void printProductsList(List<Product> productsList, String brand) {
System.out.println("Products list:".toUpperCase());
System.out.println("Product\t\tPrice");
System.out.println(".....................");
// Print all items in the list, using while( )
Iterator<Product> itrProducts = productsList.iterator();
while(itrProducts.hasNext()) {
Product p =itrProducts.next();
if(p.getBrand().equals(brand)) {
System.out.println(p.ToString());
}
}
}
}
Result:
EMPLOYEE LIST:
Surn. Name Age
.....................
Bart Desy (34)
Stenn Orny (28)
Lust Barbara (31)
Marriot Bill (36)
Fuert Carl (54)
Cant Donald (48)
Von Jack (22)
EMPLOYEE LIST:
Surn. Name Age
.....................
Von Jack (22)
Stenn Orny (28)
Lust Barbara (31)
Bart Desy (34)
Marriot Bill (36)
Cant Donald (48)
Fuert Carl (54)
PRODUCTS LIST:
Product Price
.....................
Netbook(LENOVO) € 260.0
Desktop(LENOVO) € 780.56
Notebook(ASUS) € 560.99
Netbook(ASUS) € 290.0
Netbook(ACER) € 260.0
Server(DELL) € 1630.0
Notebook(HP) € 760.5
Notebook(DELL) € 633.9
Server(HP) € 1499.0
Desktop(ACER) € 680.48
PRODUCTS LIST:
Product Price
.....................
Netbook(LENOVO) € 260.0
Netbook(ACER) € 260.0
Netbook(ASUS) € 290.0
Notebook(ASUS) € 560.99
Notebook(DELL) € 633.9
Desktop(ACER) € 680.48
Notebook(HP) € 760.5
Desktop(LENOVO) € 780.56
Server(HP) € 1499.0
Server(DELL) € 1630.0
Enter the age
15
EMPLOYEE LIST (AGE>=15)
Surn. Name Age
.....................
Von Jack (22)
Stenn Orny (28)
Lust Barbara (31)
Bart Desy (34)
Marriot Bill (36)
Cant Donald (48)
Fuert Carl (54)
Enter a brand
DELL
PRODUCTS LIST:
Product Price
.....................
Notebook(DELL) € 633.9
Server(DELL) € 1630.0