-
Notifications
You must be signed in to change notification settings - Fork 0
Revision on Assignment Week 02 #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 37 additions & 28 deletions
65
Week 02/Lecture 04/Assignment 06/RemoveDuplicatesCSV.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,46 +1,55 @@ | ||
| 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) { | ||
| String inputFilePath = "data/data.csv"; | ||
| String outputFilePath = "data/unique.csv"; | ||
| String keyFieldName = "id"; | ||
|
|
||
| try (BufferedReader reader = Files.newBufferedReader(Paths.get(inputFilePath))) { | ||
| List<String> 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<String> 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<String> 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<String> 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); | ||
| } | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For best practice, we should create constant for number 3 to prevent magic number. I dont know what is 3 mean ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello! thanks for the review
The reason why i should mention at least 3 since the format of I/O on terminal looks something like this
For example:
is it okay to do that? i will create a constant if this is fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, that's fine, just avoid magic number by naming the variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mikeleo03 You can merge this PR to mark as done for week #2. Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thankyou!