Skip to content

Instantly share code, notes, and snippets.

View GaneshSamarthyam's full-sized avatar
💭
learning

Ganesh Samarthyam GaneshSamarthyam

💭
learning
View GitHub Profile
@GaneshSamarthyam
GaneshSamarthyam / readListStringsFromCSVFile.java
Last active September 8, 2019 12:57
Simplifying conditional code
private static List<String> readListStringsFromCSVFile(String filePath) throws IOException {
List<String> fileEntries = new ArrayList<>();
BufferedReader br = new BufferedReader(new FileReader(filePath));
String line;
while ((line = br.readLine()) != null) {
fileEntries.add(line);
}
return fileEntries;
}
@GaneshSamarthyam
GaneshSamarthyam / readListStringsFromCSVFileRefactored.java
Created September 8, 2019 09:25
Simplified readListStringsFromCSVFile
private static List<String> readListStringsFromCSVFile(String filePath) throws IOException {
 return Files.lines(Paths.get(filePath)).collect(Collectors.toList());
}
private static List<String> readListStringsFromCSVFile(String filePath) throws IOException {
return Files.lines(Paths.get(filePath)).collect(Collectors.toList());
}
@GaneshSamarthyam
GaneshSamarthyam / appendMapEntries.java
Created September 8, 2019 09:27
refactoring example
// Helper function to append values if not already present in the Map
protected void appendMapEntries(Map<String, List<String>> map, String key, List<String> values) {
for (String value : values) {
List<String> existingValues = map.get(key);
if (existingValues == null) {
existingValues = new ArrayList<String>();
map.put(key, existingValues);
}
else {
existingValues.add(value);
@GaneshSamarthyam
GaneshSamarthyam / appendMapEntriesRefactored.java
Last active September 8, 2019 10:51
Refactored solution for appendMapEntries
// Helper function to append values if not already present in the Map
protected void appendMapEntries(Map<String, List<String>> map, String key, List<String> values) {
values.forEach(value -> map.computeIfAbsent(key, k -> new ArrayList<>()).add(value));
}
import java.util.Scanner;
class Main {
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(System.in);
scanner.nextInt();
} catch (Exception e) {
System.out.println("enter an int");
} finally {