diff --git a/Week 02/Lecture 03/Assignment 05/ListToMap.java b/Week 02/Lecture 03/Assignment 05/ListToMap.java index 686f587..de47030 100644 --- a/Week 02/Lecture 03/Assignment 05/ListToMap.java +++ b/Week 02/Lecture 03/Assignment 05/ListToMap.java @@ -1,7 +1,7 @@ import java.util.Arrays; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; +import java.util.TreeMap; // Assume Employee class with fields: int employeeID, String name, String department class Employee { @@ -46,11 +46,15 @@ public static void main(String[] args) { new Employee(3, "Charlie", "Finance") ); - // Convert List to Map using employeeID as key - Map employeeMap = employees.stream() - .collect(Collectors.toMap(Employee::getEmployeeID, emp -> emp)); + // Creating employeeMap using TreeMap + Map employeesMap = new TreeMap<>(); + + // Convert List to Map + for (Employee emp : employees){ + employeesMap.put(emp.getEmployeeID(), emp.getName()); + } // Print the resulting Map - employeeMap.forEach((id, emp) -> System.out.println("Employee ID: " + id + ", Employee: " + emp)); + employeesMap.forEach((id, emp) -> System.out.println("Employee ID: " + id + ", Employee: " + emp)); } } \ No newline at end of file diff --git a/Week 02/Lecture 03/Assignment 05/README.md b/Week 02/Lecture 03/Assignment 05/README.md index 6769ca7..55b8de1 100644 --- a/Week 02/Lecture 03/Assignment 05/README.md +++ b/Week 02/Lecture 03/Assignment 05/README.md @@ -80,7 +80,7 @@ To remove duplicate lines from a file: 4. **Writing Unique Lines**: Write only those lines to a new file that haven't been seen before (not in the `HashSet`). #### 📋 Case CSV Content -In this program, i use CSV file [`input.csv`](/Week%2002%20-%20Jun%2017-21/Lecture%2003/Assignment%205/data/input.csv) with content like this. +In this program, i use CSV file [`input.csv`](/Week%2002/Lecture%2003/Assignment%2005/RemoveDuplicates.java) with content like this. ```csv employeeID,name,department 1,Alice,HR @@ -106,6 +106,16 @@ Detail implementation is written on [this code](/Week%2002%20-%20Jun%2017-21/Lec The output of the program shows on this [`output.csv`](/Week%2002%20-%20Jun%2017-21/Lecture%2003/Assignment%205/data/output.csv) +Here is how to run the updated code of program +```bash +$ java RemoveDuplicates +``` + +for example. +```bash +java RemoveDuplicates data/input.csv data/output.csv 0 +``` +
### 🖨️ Task 4 - Get a Shallow Copy of a `HashMap` @@ -166,7 +176,7 @@ Here i implement class `BankAccount` and `BankAccountDemo`. 3. **Use Java Streams for Transformation**: Utilize Java Streams API to transform the `List` into a `Map`. 4. **Collect into Map**: Use the `Collectors.toMap()` method to collect elements of the `List` into a `Map` using the specified key and value mappings. -Detail implementation is written on [this code](/Week%2002%20-%20Jun%2017-21/Lecture%2003/Assignment%205/ListToMap.java), and the output of the program shows like this. +Detail implementation is written on [this code](/Week%2002/Lecture%2003/Assignment%2005/ListToMap.java), and the output of the program shows like this (updated based on comment). ![Screenshot](img/Task5.png) diff --git a/Week 02/Lecture 03/Assignment 05/RemoveDuplicates.java b/Week 02/Lecture 03/Assignment 05/RemoveDuplicates.java index f364dd0..19287f7 100644 --- a/Week 02/Lecture 03/Assignment 05/RemoveDuplicates.java +++ b/Week 02/Lecture 03/Assignment 05/RemoveDuplicates.java @@ -3,29 +3,42 @@ import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; +import java.util.ArrayList; import java.util.HashSet; +import java.util.List; import java.util.Set; public class RemoveDuplicates { public static void main(String[] args) { - String inputFileName = "data/input.csv"; - String outputFileName = "data/output.csv"; - String delimiter = ","; // Parse CSV format - - // Set to store unique keys (employeeID in this case) + if (args.length < 3) { + System.out.println("Usage: java RemoveDuplicates "); + return; + } + + String inputFileName = args[0]; + String outputFileName = args[1]; + int keyFieldIndex; + try { + keyFieldIndex = Integer.parseInt(args[2]); + } catch (NumberFormatException e) { + System.out.println("Invalid keyFieldIndex. It must be an integer."); + return; + } + + // Set to store unique keys Set seenKeys = new HashSet<>(); - + try (BufferedReader reader = new BufferedReader(new FileReader(inputFileName)); PrintWriter writer = new PrintWriter(new FileWriter(outputFileName))) { String line; while ((line = reader.readLine()) != null) { - // Split the line into fields - String[] fields = line.split(delimiter); - - // Ensure there are enough fields and key field is valid - if (fields.length > 1) { - String key = fields[0]; // Assuming employeeID is the first field + // Properly split the line respecting quoted commas + String[] fields = parseCsvLine(line); + + // Ensure key field index is valid + if (fields.length > keyFieldIndex) { + String key = fields[keyFieldIndex]; if (!seenKeys.contains(key)) { seenKeys.add(key); // Add the key to set (marks as seen) writer.println(line); // Write the line to output @@ -36,7 +49,36 @@ public static void main(String[] args) { System.out.println("Duplicates removed successfully. Output written to " + outputFileName); } catch (IOException e) { - System.out.println("I/O Error occured:" + e); + System.out.println("I/O Error occurred: " + e); } } -} + + // Function to parse CSV line while handling commas within quotes + private static String[] parseCsvLine(String line) { + boolean inQuotes = false; + StringBuilder field = new StringBuilder(); + List fields = new ArrayList<>(); + + for (char c : line.toCharArray()) { + switch (c) { + case '"': + inQuotes = !inQuotes; // Toggle the inQuotes flag + break; + case ',': + if (inQuotes) { + field.append(c); // Inside quotes, include comma + } else { + fields.add(field.toString()); + field.setLength(0); // Reset the field buffer + } + break; + default: + field.append(c); // Add character to field buffer + break; + } + } + fields.add(field.toString()); // Add last field + + return fields.toArray(new String[0]); + } +} \ No newline at end of file diff --git a/Week 02/Lecture 03/Assignment 05/img/Task5.png b/Week 02/Lecture 03/Assignment 05/img/Task5.png index ec754bd..620f659 100644 Binary files a/Week 02/Lecture 03/Assignment 05/img/Task5.png and b/Week 02/Lecture 03/Assignment 05/img/Task5.png differ diff --git a/Week 02/Lecture 04/Assignment 06/README.md b/Week 02/Lecture 04/Assignment 06/README.md index 7a36c3c..26cad3a 100644 --- a/Week 02/Lecture 04/Assignment 06/README.md +++ b/Week 02/Lecture 04/Assignment 06/README.md @@ -119,11 +119,22 @@ Here’s a detailed process on how to remove duplicate lines from files based on 4. **Write the Output**: Write the processed, duplicate-free data to a new file. #### 👨🏻‍💻 Implementation -Detail implementation is written on [this code](/Week%2002%20-%20Jun%2017-21/Lecture%2004/Assignment%206/RemoveDuplicatesCSV.java). Here’s what the program actually done. -1. **Read All Lines**: `Files.readAllLines(Paths.get(inputFilePath))` reads the CSV file into a list of strings. -2. **Extract Header**: The first line is treated as the header to determine the key field's index. -3. **Stream Processing**: The stream skips the header, then collects lines into a map using the key field (`id`). If a duplicate key is found, the first occurrence is retained. -4. **Write Results**: The header is re-added, and the list is written to the new file. +Detail implementation is written on [this code](/Week%2002/Lecture%2004/Assignment%2006/RemoveDuplicatesCSV.java). Here’s what the program actually done (updated based on comment). +1. **Initialize Readers and Writers** + - `BufferedReader` is used to read the input CSV file line by line. + - `BufferedWriter` is used to write the unique lines to the output CSV file. +2. **Extract and Write Header** + - The first line, which is the header, is read using `reader.readLine()`. + - This header is written immediately to the output file using `writer.write(header)`. +3. **Determine Key Field Index** + - The header is split to determine the index of the key field (`id`). + - This is done by iterating through the headers to find the matching field. +4. **Stream Processing and Duplicate Removal** + - A `Set` is used to keep track of the keys that have already been processed. + - For each subsequent line, the key field's value is checked against the `Set`. If the key is unique, the line is written to the output file. +5. **Read and Write Lines** + - The program continues to read each line from the input file, splits it to get the key field, and checks the key against the `Set`. + - If the key is not in the `Set`, the line is written to the output file and the key is added to the `Set`. **Best Practices Highlighted** 1. **`BufferedReader` for Large Files**: Using `BufferedReader` with `lines()` streams data efficiently. diff --git a/Week 02/Lecture 04/Assignment 06/RemoveDuplicatesCSV.java b/Week 02/Lecture 04/Assignment 06/RemoveDuplicatesCSV.java index 77d29de..01e728e 100644 --- a/Week 02/Lecture 04/Assignment 06/RemoveDuplicatesCSV.java +++ b/Week 02/Lecture 04/Assignment 06/RemoveDuplicatesCSV.java @@ -1,10 +1,10 @@ import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; +import java.util.HashSet; +import java.util.Set; public class RemoveDuplicatesCSV { public static void main(String[] args) { @@ -12,35 +12,44 @@ public static void main(String[] args) { String outputFilePath = "data/unique.csv"; String keyFieldName = "id"; - try (BufferedReader reader = Files.newBufferedReader(Paths.get(inputFilePath))) { - List lines = reader.lines().collect(Collectors.toList()); - if (lines.isEmpty()) return; + try (BufferedReader reader = Files.newBufferedReader(Paths.get(inputFilePath)); + BufferedWriter writer = Files.newBufferedWriter(Paths.get(outputFilePath))) { + + String header = reader.readLine(); + if (header == null) return; - // Extract header and determine the key field index - String header = lines.get(0); - List headers = Arrays.asList(header.split(",")); - int keyIndex = headers.indexOf(keyFieldName); - if (keyIndex == -1) throw new IllegalArgumentException("Invalid key field name"); + // Write header to the output file + writer.write(header); + writer.newLine(); - // Process lines and remove duplicates based on the key field - List uniqueLines = lines.stream() - .skip(1) // Skip header - .collect(Collectors.toMap( - line -> line.split(",")[keyIndex], // Use the key field - line -> line, // Use the line as value - (existing, replacement) -> existing // Keep the first occurrence - )) - .values() - .stream() - .collect(Collectors.toList()); + // Determine the key field index + String[] headers = header.split(","); + int keyIndex = -1; + for (int i = 0; i < headers.length; i++) { + if (headers[i].trim().equals(keyFieldName)) { + keyIndex = i; + break; + } + } + if (keyIndex == -1) throw new IllegalArgumentException("Invalid key field name"); - // Add header back to the list - uniqueLines.add(0, header); + // Use a Set to track unique keys + Set seenKeys = new HashSet<>(); - // Write the results to a new file - Files.write(Paths.get(outputFilePath), uniqueLines); + // Read and process each line + String line; + while ((line = reader.readLine()) != null) { + String[] fields = line.split(","); + if (fields.length > keyIndex) { + String key = fields[keyIndex]; + if (seenKeys.add(key)) { // Add returns false if the key was already present + writer.write(line); + writer.newLine(); + } + } + } } catch (IOException e) { - System.out.println("I/O Error occured:" + e); + System.out.println("I/O Error occurred: " + e); } } -} +} \ No newline at end of file