<JAVA>
------------------------------------------------------------------------
문자 확인 및 변환
char ch1 = 'a';
Character.isUpperCase(ch1);
Character.isLowerCase(ch1);
Character.isDigit(ch1);
Character.toUpperCase(ch1);
Character.toLowerCase(ch1);
Character.isAlphabetic(ch1);
Character.isLetter(ch1);
------------------------------------------------------------------------
문자열 관련
String str1 = "abcd...a";
str1.length();
str1.charAt(1);
"abcd...a".equals(str1);
str1.toUpperCase();
str1.toLowerCase();
str1.indexOf("a");
str1.indexOf("abc");
str1.indexOf("bc");
str1.indexOf("bca");
str1.replace("ab", "z")
str1.replace("..", ".")
while (str1.indexOf("..") != -1) {
str1 = str1.replace("..", ".");
}
str1.isEmpty();
str1.substring(1);
str1.substring(1,3);
String[] str1Arr = str1.split("bc");
str1.startsWith("ab")
str1.endsWith("a")
str1.contains("cd")
"A".charAt(0);
(int)"A".charAt(0);
(byte)"A".charAt(0);
'A'
(int)'A';
(byte)'A';
대문자 A ~ Z : 65 ~ 90
소문자 a ~ z : 97 ~ 122
------------------------------------------------------------------------
문자열을 숫자로
String s = "123";
Integer.parseInt(s);
Float.parseFloat(s);
Double.parseDouble(s);
------------------------------------------------------------------------
int n = 32;
String strNum3 = Integer.toString(n, 3);
StringBuilder sb = new StringBuilder(strNum3);
String strNum3Reverse = sb.reverse().toString();
int answer = Integer.parseInt(strNum3Reverse, 3);
sb.setLength(0);
String binaryNum = Integer.toBinaryString(n);
char[] charArr = binaryNum.toCharArray();
------------------------------------------------------------------------
String : 문자열 연산이 적고 멀티쓰레드 환경일 경우
StringBuffer : 문자열 연산이 많고 멀티쓰레드 환경일 경우
StringBuilder : 문자열 연산이 많고 단일쓰레드이거나 동기화를 고려하지 않아도 되는 경우
------------------------------------------------------------------------
절대값
int num = Math.abs(-10);
제곱근값
double sqrtN = Math.sqrt(n);
------------------------------------------------------------------------
해시맵 안에 해시셋
HashMap<String, HashSet<String>> setHashMap = new HashMap<>();
setHashMap.put("key1", new HashSet<>());
setHashMap.get("key1").add("value1");
setHashMap.get("key1").add("value1");
setHashMap.get("key1").add("value2");
setHashMap.get("key1").add("value3");
setHashMap.get("key1").remove("value3");
setHashMap.containsKey("key1");
------------------------------------------------------------------------
해시맵 키, 값 세팅 및 꺼내기
HashMap<String, String> strHashMap = new HashMap<>();
strHashMap.put("key1", "value1");
strHashMap.put("key1", "value2");
for (int i = 0; i < strHashMap.size(); i++) {
String hmKey = strHashMap.keySet().toArray()[i].toString();
String hmValue = strHashMap.values().toArray()[i].toString();
}
------------------------------------------------------------------------
HashMap<String, Integer> intHashMap = new HashMap<>();
for (int i = 0; i < genres.length; i++) {
intHashMap.put(genres[i], intHashMap.getOrDefault(genres[i], 0) + plays[i]);
}
------------------------------------------------------------------------
해시셋 값 세팅 및 꺼내기
HashSet<String> hs = new HashSet<>();
hs.add("value10");
hs.add("value11");
hs.add("value12");
HashSet 출력 방법1
for (String hsValue : hs) {
System.out.print(hsValue + ",");
}
HashSet 출력 방법2
Iterator it = hs.iterator();
while (it.hasNext()) {
System.out.print(it.next() + ",");
}
------------------------------------------------------------------------
Hash의 특성 : 중복된 키를 허용하지 않는다.
Set의 특성 : 중복된 값을 허용하지 않는다.
------------------------------------------------------------------------
String s = "example";
if (s.contains("ex")) {
s = s.replace("ex", "xe");
}
------------------------------------------------------------------------
정렬 및 비교
String[] participant = {"leo", "kiki", "eden"};
Arrays.asList(participant);
Collections.frequency(리스트, 찾고자 하는 객체);
Arrays.sort(participant);
Arrays.sort(participant, Collections.reverseOrder());
int[] arrA = {1, 1, 1, 6, 0};
int[] arrTempA = arrA;
Arrays.sort(arrTempA);
Integer[] tempA = Arrays.stream(arrA).boxed().toArray(Integer[]::new);
Arrays.sort(tempA, Collections.reverseOrder());
int[] arrTempA = Arrays.stream(tempA).mapToInt(Integer::intValue).toArray();
int[][] arr = {{5, 4}, {5, 2}, {1, 2}, {3, 1}, {1, 3}};
Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if(o1[0] == o2[0]) {
return o1[1] - o2[1];
}else {
return o1[0] - o2[0];
}
}
});
Arrays.sort(strNumbers, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return (o2+o1).compareTo(o1+o2);
}
});
String answer = "YES";
if ("YES".equals(answer))
ArrayList<String> strArrayList = new ArrayList<>();
Collections.sort(strArrayList);
static class Music{
String genre;
int play;
int idx;
public Music(String genre, int play, int idx) {
this.genre = genre;
this.play = play;
this.idx = idx;
}
}
ArrayList<Music> list = new ArrayList<>();
Collections.sort(list, (o1, o2) -> o1.play - o2.play);
Collections.sort(list, (o1, o2) -> o2.play - o1.play);
int idx = Arrays.asList(strArr).indexOf("Kim");
boolean containCheck = Arrays.asList(strArr).contains("Kim");
------------------------------------------------------------------------
코딩테스트 볼 때
import java.util.*; 쓰고 시작하자
------------------------------------------------------------------------
continue;
break;
return;
------------------------------------------------------------------------
문자 치환
String match1 = "[^a-zA-Z]";
str1 = str1.replaceAll(match1, "");
"[a-zA-Z]" : 영문자
"[^0-9a-zA-Z]" : 숫자, 영문자 제외
"[^\uAC00-\uD7A3]" : 특수문자 제외
"[^\uAC00-\uD7A30-9a-zA-Z]" : 특수문자, 숫자, 영문자 제외
------------------------------------------------------------------------
int[] numArr = {9, 3, 9, 3, 9, 7, 9};
Arrays.stream(numArr).sum();
------------------------------------------------------------------------
System.out.println(2f);
int i = 100;
long l1 = 100L;
long l2 = 100l;
double d1 = 1.23;
double d2 = 1.23D;
double d3 = 1.23d;
float f1 = 1.23F;
float f2 = 1.23f;
------------------------------------------------------------------------