Created
January 23, 2019 06:21
-
-
Save Jeraldy/673f461f99b901e73e9448538e9cb94d to your computer and use it in GitHub Desktop.
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 yourpackagename; | |
import java.io.IOException; | |
import java.nio.charset.Charset; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import java.util.stream.Collectors; | |
/** | |
* | |
* @author Jeraldy Deus | deusjeraldy@gmail.com | |
*/ | |
public class utils { | |
public static String open(String path, Charset encoding) { | |
byte[] encoded = null; | |
try { | |
encoded = Files.readAllBytes(Paths.get(path)); | |
} catch (IOException ex) { | |
Logger.getLogger(utils.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
return new String(encoded, encoding); | |
} | |
public static char[] listUniqueChars(String data) { | |
String s = data | |
.chars() | |
.distinct() | |
.mapToObj(c -> String.valueOf((char) c)) | |
.collect(Collectors.joining()); | |
char[] chars = new char[s.length() + 1]; | |
for (int i = 0; i < s.length(); i++) { | |
char c = s.charAt(i); | |
chars[i] = c; | |
} | |
return chars; | |
} | |
public static Map<Character, Integer> charToIx(char[] chars) { | |
Map<Character, Integer> dict = new HashMap<>(); | |
for (int i = 0; i < chars.length; i++) { | |
dict.put(chars[i], i); | |
} | |
return dict; | |
} | |
public static Map<Integer, Character> ixToChar(char[] chars) { | |
Map<Integer, Character> dict = new HashMap<>(); | |
for (int i = 0; i < chars.length; i++) { | |
dict.put(i, chars[i]); | |
} | |
return dict; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment