예제) 리스트에 학생 이름 추가한 후 출력하기
import java.util.ArrayList;
import java.util.List;
public class ListExcercise {
private List<String> students; //String을 원소로 가지는 List 선언
public List<String> getStudents() {
return this.students;
} // list가 private 하므로 getter를 통해서 접근 가능
public ListExcercise(){
this.students = new ArrayList<>(); // List는 Arraylist임
this.students.add("김경록");
this.students.add("김미미");
this.students.add("김나나");
}
}
// ListExcercise의 main 클래스
import java.util.List;
public class ListExcerciseMain {
public static void main(String[] args) {
ListExcercise listExcercise = new ListExcercise();
// 인스턴스 생성
List<String> students = listExcercise.getStudents();
// getter로 list에 접근
for (String student: students){
System.out.println(student);
}
System.out.println(students.size());
}
}
public void testAddObject() {
Patient patient1 = new Patient("김영환", 33);
Patient patient2 = new Patient("김영환", 33);
Set<Patient> setPatient = new HashSet<>();
setPatient.add(patient1);
setPatient.add(patient2);
System.out.println(setPatient);
//[domain.Patient@7f31245a, domain.Patient@135fbaa4]
A. 이진 탐색트리 저장(TreeSet)
Integer인 경우 부모 노드보다 작은 숫자는 왼쪽, 큰 숫자는 오른쪽으로 배열함
트리의 깊이만큼의 시간이 걸림 -> 트리의 깊이가 얕을 수록 빠른 속도
값이 잘 분산되어있다면 효율적이지만 값이 편향되어 있는 경우(ex. 0,1,2,4,5,6,7,8,9,101,102)는 비효율적임
이를 보완하기 위해 TreeMap에서는 레드 블랙트리 방법이 도입됨
B. 해싱(Hashing)
예제1) 랜덤한 숫자 생성한 후 Set에 넣고 출력하기
// 인터페이스
public interface NumberGenerator11 {
int generate(int num);
}
// 인터페이스를 상속한 generator class
public class RandomNumberGenerator11 implements NumberGenerator11{
@Override
public int generate (int num){
return (int)(Math.random()*num); // 0이상 num 미만의 정수 생성
}
}
// Main 클래스
import java.util.HashSet;
import java.util.Set;
public class RandomNumberWithoutDuplicated {
public static void main(String[] args) {
RandomNumberGenerator11 randomNumberGenerator11 = new RandomNumberGenerator11();
// Set에서는 중복제거를 할 수 있음
Set<Integer> dupliOut = new HashSet<>();
for (int i=0; i<50 ; i++){
int r = randomNumberGenerator11.generate(10); // 0~9까지의 정수 생성
System.out.println(r);
dupliOut.add(r);
}
System.out.println(dupliOut);
// [1,2,3,4,5,6,7,8,9]
}
}
예제2) 랜덤한 알파벳 대문자 생성한 후 Set에 넣고 출력하기
// generator 클래스
public class RandomLetterGenerator{
public char generate() {
return (char)(int)(Math.random()*26+65);
// 아스키코드 65~90 영어 대문자
}
}
// main 클래스
import java.util.HashSet;
import java.util.Set;
public class RandomLetterWithoutDuplicated {
public static void main(String[] args) {
RandomLetterGenerator randomLetterGenerator = new RandomLetterGenerator();
Set<Character> alphabetDupliOut = new HashSet<>();
for (int i=0; i<50 ; i++){
char alphabet = randomLetterGenerator.generate();
alphabetDupliOut.add(alphabet);
}
System.out.println(alphabetDupliOut);
}
}
예제) url에 있는 영어의 개수를 Map에 입력하기 - key에는 문자, value에는 숫자
import java.util.HashMap;
import java.util.Map;
public class MapExcercise2 {
public static void main(String[] args) {
String repoAddr = "https://github.com/seanee3670/CodeLion/tree/master/src";
String repoAddrLower = repoAddr.toLowerCase(); // 소문자 변환
Map<String, Integer> alphabetCnt = new HashMap<>();
// 65~90 영어 대문자
// 97~122 영어 소문자
// map에 글자 먼저 넣기
for (int i=0; i<26; i++){
alphabetCnt.put(Character.toString((char)(i+97)),0);
}
// map의 key 값을 이용해서 알파벳 문자인지 확인하기
for (int i=0; i<repoAddr.length();i++){
String letter = String.valueOf(repoAddrLower.charAt(i));
if (alphabetCnt.keySet().contains(letter)){
alphabetCnt.put(letter,(alphabetCnt.get(letter) +1));
}
}
System.out.println(alphabetCnt.get("b"));
System.out.println(alphabetCnt.get("c"));
System.out.println(alphabetCnt.get("r"));
}
}
추가자료 출처
1. 자바의 정석
2. https://coding-factory.tistory.com/557