본문 바로가기

Java

(11)
string replace ${val} (특수 형태 치환) My name is ${name} and my age is ${age} 위와 같은 string에 대해서 map 에 있는 값으로 치환하고자 할 때 사용 String target = "My name is ${name} and my age is ${age}"; Map map; // 치환할 name, age를 key로하는 value가 있음 Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}"); Matcher matcher = pattern.matcher(target); StringBuilder builder = new StringBuilder(); int i = 0; while (matcher.find()) { String replacement = map.get(match..
file search java 8 - TEST 폴더 포함 try (Stream walk = Files.walk(Paths.get("./TEST"))) { List result = walk.map(x -> x.toFile()).collect(Collectors.toList()); for (File f : result) System.out.println(String.format("isFile : %5s - %s", f.isFile(), f.getCanonicalPath())); } catch (IOException e) { e.printStackTrace(); } java 7 - TEST 폴더 미포함 public class Test { public static void main(String[] args) throws IOExce..
array duplicates delete (array 중복 삭제) String[] array = {"name1","name2","name3","name4","name2","name3"}; // Set Set set = new LinkedHashSet(Arrays.asList(array)); String[] removeArray = set.toArray(new String[] {}); System.out.println(Arrays.toString(removeArray)); // Temporary Arrays.sort(array); String[] temp = new String[array.length]; int idx = 0; for (int i=0 ; i
array duplicates count (array 중복 개수) // java7 List asList = Arrays.asList(array); Set set = new HashSet(asList); Map map = new HashMap(); for (String str : set) map.put(str, Collections.frequency(asList, str)); System.out.println(map); // java8 Map map2 = Arrays.stream(array).collect(Collectors.groupingBy(s->s,Collectors.counting())); System.out.println(map2);
array & list (sort, convert) array sort String[] array = {"name1","name2","name3","name4","name2","name3"}; System.out.println(Arrays.toString(array)); Arrays.sort(array); System.out.println(Arrays.toString(array)); list sort List list = Arrays.asList(array); System.out.println(list); Collections.sort(list, new Comparator() { @Override public int compare(String o1, String o2) { return o2.compareTo(o1); //DESC } }); System.o..
string date 계산 String now = "20190823 10:20:10"; String min = "15"; String sec = "55"; System.out.println(getDate(now, min, sec)); public static String getDate(String now, String min, String sec) { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss"); try { Date nowDate = sdf.parse(now); long nowMilliSeconds = nowDate.getTime(); nowMilliSeconds += Integer.parseInt(min) * 60 * 1000; nowMilliSeconds ..
chat server & client SERVER public class Server { private static final int PORT = 9001; private static HashSet names = new HashSet(); private static HashSet writers = new HashSet(); public static void main(String[] args) throws Exception { System.out.println("The chat server is running."); ServerSocket listener = new ServerSocket(PORT); try { while (true) { new Handler(listener.accept()).start(); } } finally { liste..
line copy public static void lineCopy() { String rootPath = "."; String outFolder = "/output"; String fileName = "Client.java"; File oFolder = new File(rootPath+outFolder); if (!oFolder.exists()) oFolder.mkdirs(); File inFile = new File(rootPath, fileName); File outFile = new File(rootPath+outFolder, fileName); BufferedReader in = null; BufferedWriter out = null; try { in = new BufferedReader(new FileRead..