Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; public class Main { public static void main(String[] args) { ArrayList sorted = loadEmployee(NamesOfEmployees.csv); //

import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Collections;

public class Main {

public static void main(String[] args) {

ArrayList sorted = loadEmployee("NamesOfEmployees.csv");

// created a shuffled version ArrayList unsorted = new ArrayList(sorted); Collections.shuffle(unsorted); // Add codes to sort the ArrayList and try them out // 1. Create and use an anonymous class that sorts by first name high to low // 2. Create and use a lambda expression that sorts by last name low to high // 3. Create and use a lambda expression that sorts by title // 4. Create and use a lambda expression that sorts first by title then by Level // 5. Create and use a lambda expression that sorts first by last name then by first name Collections.sort(unsorted); // Use Lambda expression to print all elements of unsorted ArrayList }

public static ArrayList loadEmployee(String filename) {

BufferedReader br = null; FileReader fr = null;

ArrayList employees = new ArrayList(); ArrayList lines = new ArrayList(); try {

fr = new FileReader(filename); br = new BufferedReader(fr); String line; br = new BufferedReader(new FileReader(filename));

while ((line = br.readLine()) != null) { lines.add(line); }

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (br != null) br.close();

if (fr != null) fr.close();

} catch (IOException ex) {

} int count = 0; for (int i = 0; i < lines.size(); i++) {

String field[] = lines.get(count++).split(",");

employees.add(new Employee(field[0], field[1], field[2], String.format("%010d", i), field[3])); count = count % lines.size();

}

}

return employees; }

}

public class Employee implements Comparable{ private String firstName; private String lastName; private String title; private String ID; private String level; public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public String getTitle() { return title; } public String getID() { return ID; } public String getLevel() { return level; } public Employee(String firstName, String lastName, String title, String iD, String level) { super(); this.firstName = firstName; this.lastName = lastName; this.title = title; ID = iD; this.level = level; } @Override public String toString() { return String.format("%-15s %-15s %-40s %5s %5s ", firstName ,lastName, title, level, ID); } @Override public int compareTo(Employee o) { return this.ID.compareTo(o.ID); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases And Information Systems 1 International Baltic Conference Dbandis 2020 Tallinn Estonia June 19 2020 Proceedings

Authors: Tarmo Robal ,Hele-Mai Haav ,Jaan Penjam ,Raimundas Matulevicius

1st Edition

303057671X, 978-3030576714

More Books

Students also viewed these Databases questions

Question

=+ Are unions company-wide, regional, or national?

Answered: 1 week ago

Question

=+j Explain the litigation risks in international labor relations.

Answered: 1 week ago