Created
August 11, 2022 13:52
-
-
Save kaffein/fdfe630a7a755c4c4c5dcd8408e3487e to your computer and use it in GitHub Desktop.
For Java interview
This file contains 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
package ch.deepimpact; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.List; | |
import java.util.Vector; | |
import java.util.stream.Collectors; | |
public class Palindromes { | |
public static boolean isPalindrome(String string) { | |
return new StringBuffer(string).reverse().toString().equals(string); | |
} | |
public static Vector<String> filterForPalindromes(List<String> strings) { | |
Vector<String> results = new Vector<>(); | |
for (String string : strings) { | |
if (isPalindrome(string)) | |
results.add(string); | |
} | |
return results; | |
} | |
static List<String> loadStrings(String path) { | |
List<String> strings = null; | |
try { | |
strings = Files.lines(Paths.get(path)).collect(Collectors.toList()); | |
} catch (IOException e) { } | |
return strings; | |
} | |
public static void main(String[] args) { | |
List<String> strings = Palindromes.loadStrings(args[0]); | |
List<String> palindromes = Palindromes.filterForPalindromes(null); | |
palindromes.forEach(System.out::println); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment