- Today
- Total
- μ‘Έμ μν
- λ°±μλ
- pytorch
- μλ°μμ μ
- μμμ λ ¬
- database
- 그리λ
- μλ£κ΅¬μ‘°
- MST
- dp
- CS
- OOP
- array
- PS
- Graph
- ꡬν
- λ¬Έλ²
- μλ°
- λ€μ΅μ€νΈλΌ
- tree
- Algorithm
- java
- spring
- νλ‘κ·Έλλ¨Έμ€
- λ°±μ€
- BFS
- leetcode
- μΈν΄
- λ°μ΄ν°λ² μ΄μ€
- 벨λ§ν¬λ
Partially Committed
[CH11] Collections framework λ³Έλ¬Έ
λ³Έ ν¬μ€ν μ μλ°μ μ μ κ΅μ¬λ₯Ό 곡λΆνλ©°, κ°λ¨ν μ 리/κΈ°λ‘ μ©λλ‘ μμ±νμμ΅λλ€. νΉμ¬, μλͺ»λ λ΄μ©μ΄ μλ€λ©΄ μ§μ ν΄μ£Όμλ©΄ κ°μ¬νκ² μ΅λλ€.
0. Collections Framework
Collections Framework λ data group μ λ€λ£¨κ³ νννκΈ° μν λ¨μΌνλ architecture μ λ§νλ€. Collections Framework μ ν΅μ¬ μΈν°νμ΄μ€λ List, Set, Map μ΄λ€. List λ (μμκ° μλ) λ°μ΄ν°μ μ€λ³΅μ νμ©νλ©° ArrayList, LinkedList, Stack, Vector λ±μΌλ‘ ꡬνλλ€. Set μ (μμκ° μλ) λ°μ΄ν°μ μ€λ³΅μ νμ©νμ§ μλ κ²μΌλ‘ HashSet, TreeSet λ±μΌλ‘ ꡬνλλ€. Map μ key - value pair λ‘ μ΄λ€μ§ κ²μΌλ‘ μμλ μ μ§λμ§ μμΌλ©° ν€λ μ€λ³΅μ νμ©νμ§ μκ³ κ°μ νμ©νλ€. HashMap, TreeMap, HashTable, Properties λ±μΌλ‘ ꡬνλλ€.
List μΈν°νμ΄μ€λ μ€λ³΅μ νμ©νλ©΄μ μ μ₯ μμλ μ μ§λλ 컬λ μ μ ꡬννλλ°μ μ¬μ©νλ€.
Set μΈν°νμ΄μ€λ μ€λ³΅μ νμ©νμ§ μκ³ μμλ₯Ό κ³ λ €νμ§ μλ ν΄λμ€λ₯Ό ꡬννλλ°μ μ¬μ©λλ€.
Map μΈν°νμ΄μ€λ key- value λ₯Ό pair λ‘ κ°μ§λ ν΄λμ€λ₯Ό ꡬννλλ°μ μ¬μ©λλ€. ν€λ μ€λ³΅λ μ μμΌλ value λ μ€λ³΅μ νμ©νλ©°, μ€λ³΅λ ν€μ κ°μ μ μ₯νλ©΄ λ§μ§λ§μ μ μ₯λ κ°μ΄ λ¨λλ€.
Map.Entry μΈν°νμ΄μ€λ Map μΈν°νμ΄μ€μ λ΄λΆ μΈν°νμ΄μ€(inner interface)μ΄λ€.
public interface Map {
...
public static interface Entry{
Object getKey();
Object getValue();
Object setValue(Object value);
boolean equals(Object o);
int hashCode();
...
}
}
1. ArrayList
ArrayList λ List μΈν°νμ΄μ€μ ꡬνμ΄κΈ° λλ¬Έμ λ°μ΄ν°μ μ μ₯ μμκ° μ μ§λκ³ μ€λ³΅μ νμ©νλ€. Vector μ κ°μ ν κ²μΌλ‘, λ°μ΄ν°λ₯Ό Object λ°°μ΄μ μ΄μ©ν΄μ μμ°¨μ μΌλ‘ μ μ₯νλ€.
public class ArrayList extends AbstractList implements List, RandomAccess, Cloneable, java.io.Serializable {
...
transient Object[] elementData;
...
}
ArrayList λ elementData μ λ©€λ²λ³μλ‘ μ μΈνκ³ μκ³ , Object λ°°μ΄μ΄λ―λ‘ λͺ¨λ type μ κ°μ²΄λ₯Ό λ΄μ μ μλ€.
import java.util.ArrayList;
import java.util.Collections;
class source{
public static void main(String[] args) {
ArrayList list1 = new ArrayList(10);
list1.add(5);
list1.add(4);
list1.add(2);
list1.add(0);
list1.add(1);
list1.add(3);
ArrayList list2 = new ArrayList(list1.subList(1,4));
print(list1, list2);
Collections.sort(list1);
Collections.sort(list2);
System.out.println("list1.containsAll(list2): " + list1.containsAll(list2));
list2.add("B");
list2.add("C");
list2.add(3, "A");
print(list1, list2);
list2.set(3, "AA");
print(list1, list2);
System.out.println("list1.retainAll(list2):" + list1.retainAll(list2));
print(list1, list2);
for(int i = list2.size()-1; i>=0 ;i--){
if(list1.contains(list2.get(i)))
list2.remove(i);
}
print(list1, list2);
}
static void print(ArrayList list1, ArrayList list2){
System.out.println("List1:" + list1);
System.out.println("List2:" + list2);
System.out.println();
}
}
import java.util.ArrayList;
import java.util.List;
class source{
public static void main(String[] args) {
final int LIMIT = 10;
String source = "0123456789abcdefghijABCDEFGHIJ!@#$%^&*()ZZZ";
int length = source.length();
List list = new ArrayList(length/LIMIT + 10);
for(int i=0; i < length; i+=LIMIT){
if(i+LIMIT < length)
list.add(source.substring(i, i+LIMIT));
else
list.add(source.substring(i));
}
for(int i=0; i<list.size(); i++){
System.out.println(list.get(i));
}
}
static void print(ArrayList list1, ArrayList list2){
System.out.println("List1:" + list1);
System.out.println("List2:" + list2);
System.out.println();
}
}
import java.util.ArrayList;
import java.util.Vector;
class source{
public static void main(String[] args) {
Vector v = new Vector(5);
v.add("1");
v.add("2");
v.add("3");
print(v);
v.trimToSize();
System.out.println("=== After trimToSize() ===");
print(v);
v.ensureCapacity(6);
System.out.println("=== After ensureCapacity(6) ===");
print(v);
v.setSize(7);
System.out.println("=== After setSize(7) ===");;
print(v);
v.clear();
System.out.println("=== After clear() === ");
print(v);
}
static void print(ArrayList list1, ArrayList list2){
System.out.println("List1:" + list1);
System.out.println("List2:" + list2);
System.out.println();
}
static void print(Vector v){
System.out.println(v);
System.out.println("size :" + v.size());
System.out.println("capacity :" + v.capacity());
}
}
3λ²μ§Έ μ½λμ κ²°κ³Όλ μλμ κ°λ€.
1. 맨 μ²μμλ capacity κ° 5 μΈ Vector μΈμ€ν΄μ€ v λ₯Ό μμ±νκ³ , 3κ°μ κ°μ²΄λ₯Ό μ μ₯νκΈ° λλ¬Έμ size κ° 3, cap μ΄ 5 κ° λμ¨λ€.
2. μ΄ν v.trimToSize(); λ₯Ό νΈμΆνλ©΄ λΉ κ³΅κ°μ μμ λ―λ‘ size = 3, cpa = 3 μ΄ λλ€. λ°°μ΄μ ν¬κΈ°λ₯Ό λ³κ²½ν μ μμΌλ―λ‘ μλ‘μ΄ λ°°μ΄μ μμ±ν λ€μ μ£Όμκ°μ λ³μ v μ ν λΉνλ κ²μ΄λ€. κΈ°μ‘΄μ Vector μΈμ€ν΄μ€λ Garbage collector μ μν΄ λ©λͺ¨λ¦¬μμ μ κ±°λλ€.
3. κ·Έλ¦¬κ³ ensureCapacity(6) μ νΈμΆνλ©΄ μΈμ€ν΄μ€ v μ capacity κ° μ΅μν 6μ΄ λλλ‘ νλ€. v μ cap μ΄ λ§μ½ 6λ³΄λ€ μ»Έλ€λ©΄ μλ¬΄λ° λ³νκ° μΌμ΄λμ§ μκ³ , 6λ³΄λ€ μμλ€λ©΄ ν¬κΈ°κ° 6μΈ λ°°μ΄μ μμ±ν΄μ vμ λ΄μ©μ 볡μ¬νλ€.
4. v.setSize(7) μ μΈμ€ν΄μ€ v μ size λ₯Ό 7λ‘ λ§λ€μ΄μ€λ€. capacity κ° μΆ©λΆνλ€λ©΄ μλ‘μ΄ μΈμ€ν΄μ€λ₯Ό λ§λ€μ§ μμΌλ, capacity κ° λΆμ‘±νλ€λ©΄ μλ‘μ΄ μΈμ€ν΄μ€λ₯Ό μμ±νλλ°, λ΄λΆ ꡬν λ°©μμ λ°λΌ capacity λ 2λ°°κ° λλ€.
5. v.clear(); λ v μ λͺ¨λ μμλ₯Ό μμ νλ κ²μΌλ‘ size κ° 0μ΄ λλ€.
ArrayList λ Vecotr μ κ°μ΄ λ°°μ΄μ μ΄μ©ν μλ£κ΅¬μ‘°λ λ°μ΄ν°μ read/write ν¨μΈμ΄ μ’μ νΈμ΄μ§λ§,
Capacity λ₯Ό λ³κ²½ν΄μΌ νλ μν©μλ μλ‘μ΄ λ°°μ΄μ μμ±ν λ€μμ 볡μ¬νλ―λ‘
ν¨μ¨μ΄ μλΉν λ¨μ΄μ§λ€λ λ¨μ μ΄ μλ€.
import java.util.List;
public class MyVector implements List{
Object[] data = null;
int capacity = 0;
int size = 0;
public MyVector(int capacity){
if(capacity < 0)
throw new IllegalArgumentException("invalid value. : " + capacity);
this.capacity = capacity;
data = new Object[capacity];
}
public MyVector(){
this(10);
}
public void ensureCapacity(int minCapacity){
if(minCapacity - data.length>0)
setCapacity(minCapacity);
}
public boolean add(Object obj){
ensureCapacity(size+1);
data[size++] = obj;
return true;
}
public Object get(int index){
if(index < 0 || index >= size)
throw new IndexOutOfBoundsException("OutOfBounds...");
return data[index];
}
public Object remove(int index){
Object oldObj = null;
if(index < 0 || index >= size)
throw new IndexOutOfBoundsException("OutOfBounds...");
oldObj = data[index];
if(index != size-1){
System.arraycopy(data, index+1, data, index, size-index-1);
}
data[size-1] = null;
size--;
return oldObj;
}
public boolean remove(Object obj){
for(int i = 0 ; i < size; i ++){
if(obj.equals(data[i])){
remove(i);
return true;
}
}
return false;
}
public void trimToSize(){
setCapacity(size);
}
private void setCapacity(int capacity){
if(this.capacity == capacity) return;
Object[] tmp = new Object[capacity];
System.arraycopy(data, 0, tmp, 0, size);
data = tmp;
this.capacity = capacity;
}
public Object[] toArray(){
Object[] result = new Object[size];
System.arraycopy(data, 0, result, 0, size);
return result;
}
public boolean isEmpty() { return size==0; }
public int capacity() { return capacity; }
public int size() { return size; }
public boolean contains(Object o){
if(indexOf(o) >= 0) return true;
else return false;
}
public Object set(int index, Object element){
if(index >= size || index < 0)
throw new IndexOutOfBoundsException();
else{
data[index] = element;
return data;
}
}
public void add(int index, Object element){
if(index >= size || index < 0)
throw new IndexOutOfBoundsException();
else {
ensureCapacity(size+1);
data[++size] = element;
}
}
public int indexOf(Object o){
for(int i = 0 ; i < size; i ++){
if(data[i].equals(o)){
return i;
}
}
return -1;
}
public int lastIndexOf(Object o){
for(int i = size - 1 ; i >=0 ; i--){
if(data[i].equals(o))
return i;
}
return -1;
}
public String toString(){
String str = "[";
for(int i = 0 ; i < size; i ++){
str += data[i];
if(i < size-1){
str +=",";
}
}
return str + "]";
}
/*
μ΄ν μλ΅
*/
}
2. LinkedList
λ§ν¬λ 리μ€νΈλ λ°°μ΄μ λ¨μ (1. ν¬κΈ°λ₯Ό λ³κ²½ν μ μλ€. 2. λΉμμ°¨μ μΈ λ°μ΄ν°μ μΆκ°/μμ μ λΉν¨μ¨μ μ΄λ€) μ 보μνκ³ μ κ³ μλ μλ£κ΅¬μ‘°μ΄λ©° λΆμ°μμ μΌλ‘ μ‘΄μ¬νλ λ°μ΄ν°λ₯Ό μλ‘ linking ν ννμ΄λ€. κ°κ°μ μμλ€μ λ€μ μμμ λν μ°Έμ‘°(μ£Όμκ°)μ λ°μ΄ν°λ‘ ꡬμ±λμ΄μλ€.
class Node{
Node next;
Object obj;
}
λ€λ§ single linked list λ μ΄λ λ°©ν₯μ΄ λ¨λ°©ν₯μ΄λΌ λ€μ μμμ λν μ κ·Όμ ν¨μ¨μ μ΄μ§λ§ μ΄μ μμμ λν μ κ·Όμ μ΄λ ΅λ€. μ΄λ₯Ό 보μν κ²μ μ΄μ μμμ λν μ°Έμ‘°λ μ μ₯νλ double linked list μ΄λ€.
class Node{
Node next;
Node previous;
Object obj;
}
double linked list μ μ κ·Όμ±μ λ³΄λ€ ν₯μ μν¨ κ²μ double circular linked list μΌλ‘ 첫 λ²μ§Έ μμμ λ§μ§λ§ μμλ₯Ό μ°κ²°ν κ²μ΄λ€. μ€μ JAVA μ LinkedList ν΄λμ€λ double linked list λ‘ κ΅¬νλμ΄ μλ€.
1. μμ°¨μ μΌλ‘ μΆκ°/μμ νλ κ²½μ°μλ ArrayList κ° LinkedList λ³΄λ€ λΉ λ₯΄λ€.
2. μ€κ° λ°μ΄ν°λ₯Ό μΆκ°/μμ νλ κ²½μ°μλ LinkedList κ° ArrayList λ³΄λ€ λΉ λ₯΄λ€.
3. Stack and Queue
Stack μ ArrayList μ κ°μ λ°°μ΄ κΈ°λ° μ»¬λ μ ν΄λμ€λ‘ ꡬννκ³ , Queue λ ArrayList λ₯Ό μ¬μ©νλ€λ©΄ λ°μ΄ν°λ₯Ό κΊΌλΌ λλ§λ€ λΉ κ³΅κ°μ μ±μ°κΈ° μν λ°μ΄ν°μ λ³΅μ¬ μμ μ΄ νμνλ―λ‘ μ΄λ³΄λ€λ LinkedList λ‘ κ΅¬ννλ κ²μ΄ μ ν©νλ€.
import java.util.EmptyStackException;
import java.util.Vector;
class MyStack extends Vector {
public Object push(Object item){
addElement(item);
return item;
}
public Object pop(){
Object obj = peek();
removeElementAt(size()-1);
return obj;
}
public Object peek(){
int len = size();
if(len == 0)
throw new EmptyStackException();
return elementAt(len-1);
}
public boolean empty(){
return size()==0;
}
public int search(Object o){
int i = lastIndexOf(o);
if(i>=0)
return size()-i;
return -1;
}
}
μ€νμ λνμ μΈ νμ© μ¬λ‘λ μμ κ³μ°, μμκ΄νΈκ²μ¬, μλνλ‘μΈμμ undo/redo, μΉλΈλΌμ°μ μ λ€λ‘/μμΌλ‘μ΄λ©°, νμ λνμ μΈ νμ© μ¬λ‘λ μ΅κ·Όμ¬μ©λ¬Έμ, μΈμμμ λκΈ°λͺ©λ‘, buffer λ±μ΄ μλ€.
import java.util.Stack;
class source{
public static Stack back = new Stack();
public static Stack forward = new Stack();
public static void main(String[] args) {
goURL("1.google");
goURL("2.naver");
goURL("3.daum");
goURL("4.yahoo");
printStatus();
goBack();
System.out.println("push \"go back\" ");
printStatus();
goBack();
System.out.println("push \"go back\" ");
printStatus();
goForward();
System.out.println("push \"go forward\" ");
printStatus();
goURL("codechobo.com");
System.out.println("push \"go to new URL\" ");
printStatus();
}
public static void printStatus(){
System.out.println("back:"+back);
System.out.println("forward:"+forward);
System.out.println("present: " + back.peek());
System.out.println();
}
public static void goURL(String url){
back.push(url);
if(!forward.empty())
forward.clear();
}
public static void goForward(){
if(!forward.empty())
back.push(forward.pop());
}
public static void goBack(){
if(!back.empty())
forward.push(back.pop());
}
}
μ μμ λ μΉ λΈλΌμ°μ μ μμΌλ‘κ°κΈ°, λ€λ‘κ°κΈ°λ₯Ό κ°λ¨ν ꡬνν κ²μΌλ‘ BACK, FORWARD μν μ νλ 2 κ°μ STACK μΌλ‘ ꡬνμ΄ κ°λ₯νλ€.
import java.util.EmptyStackException;
import java.util.Stack;
public class source{
public static void main(String[] args) {
Stack st = new Stack();
String expression = "((2+3)*1)+3";
System.out.println("Expression:" + expression);
try{
for(int i = 0 ; i < expression.length(); i++){
char ch = expression.charAt(i);
if(ch=='('){
st.push(ch+"");
} else if(ch==')'){
st.pop();
}
}
if(st.isEmpty()){
System.out.println("correct!");
}
else{
System.out.println("something wrong...");
}
} catch (EmptyStackException e){
System.out.println("something wrong...");
}
}
}
μ μμ λ μμμ κ΄νΈλ₯Ό κ²μ¬νλ κ²μΌλ‘, stack μ μ΄μ©νμ¬ κ°λ¨ν ꡬνν μ μλ€.
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Queue;
import java.util.Scanner;
class source{
static Queue q =new LinkedList();
static final int MAX_SIZE = 5;
public static void main(String[] args) {
System.out.println("You can view the guide by typing \"help\". ");
while(true){
System.out.print(">>");
try{
Scanner s = new Scanner(System.in);
String input = s.nextLine().trim();
if("".equals(input)) continue;
if(input.equalsIgnoreCase("q")){
System.exit(0);
} else if(input.equalsIgnoreCase("help")){
System.out.println(" help - Shows guide.");
System.out.println(" q or Q - Exit");
System.out.println(" history - Shows " + MAX_SIZE + " commands " +
"that you have recently entered.");
} else if(input.equalsIgnoreCase("history")){
int i = 0;
save(input);
LinkedList tmp = (LinkedList)q;
ListIterator it = tmp.listIterator();
while(it.hasNext())
System.out.println(++i+"."+it.next());
} else{
save(input);
System.out.println(input);
}
} catch (Exception e) {
System.out.println("invalid command.");
}
}
}
public static void save(String input){
if(!"".equals(input))
q.offer(input); // queue μ κ°μ²΄λ₯Ό μ μ₯
if(q.size() > MAX_SIZE)
q.remove();
}
}
μ μμ λ Queue λ₯Ό μ΄μ©νμ¬ μ λμ€μ history λͺ λ Ήμ΄λ₯Ό ꡬνν κ²μ΄λ€.
PriorityQueue λ Queue μΈν°νμ΄μ€μ ꡬν체 μ€ νλλ‘, μ°μ μμμ λ°λΌ μ μ₯λλ queue μ΄λ€. null μ μ μ₯ν μ μκ³ , μ μ₯ 곡κ°μΌλ‘ λ°°μ΄μ μ¬μ©νλ©°, κ°κ°μ μμλ₯Ό heap ννλ‘ μ μ₯νλ€. heap μ μ΄μ§ νΈλ¦¬μ ν μ’ λ₯λ‘ max κ°μ΄λ min κ°μ ν¨μ¨μ μΌλ‘ μ°Ύμ μ μλ€.
Deque(Double-Ended Queue) λ μλ°©ν₯ Queue μ΄λ€.
4. Interator, ListIterator, Enumeration
컬λ μ μ μ μ₯λ μμλ₯Ό μ κ·Όνλλ°μ μ¬μ©νλ μΈν°νμ΄μ€μ΄λ€. Enumeration μ Iterator μ ꡬλ²μ μ΄κ³ , ListIterator μ Iterator μ λ³΄λ€ κ°μ ν κ²μ΄λ€.
Collections framework μλ Iterator μΈν°νμ΄μ€λ‘ μ μ₯λ μμλ₯Ό μ½μ΄μ€λ λ°©λ²μ νμ€ννμλ€.
public interface Iterator{
boolean hasNext();
Object next();
void remove();
}
map μΈν°νμ΄μ€λ₯Ό ꡬνν Collection class λ key-value λ₯Ό pair λ‘ μ μ₯νκΈ° λλ¬Έμ iterator μ μ§μ νΈμΆν μ μλ€. λμ keySet() μ΄λ entrySet() λ©μλλ₯Ό μ΄μ©νμ¬ key μ value λ₯Ό κ°κ° set μ ννλ‘ μ»μ΄μ¨ λ€μ iterator λ₯Ό νΈμΆνλ κ²μ΄ κ°λ₯νλ€.
import java.util.ArrayList;
import java.util.Iterator;
class source{
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
list.add("5");
Iterator it = list.iterator();
while(it.hasNext()){
Object obj = it.next();
System.out.println(obj);
}
}
}
ListIterator μ λ¨λ°©ν₯ μνλ§ κ°λ₯ν Iterator μ κ°μ νμ¬ μλ°©ν₯ μνκ° κ°λ₯νλ° λ€λ§ ArrayList λ LinkedList μ²λΌ List μΈν°νμ΄μ€λ₯Ό ꡬνν 컬λ μ μ λν΄μλ§ μ¬μ©μ΄ κ°λ₯νλ€.
import java.util.ArrayList;
import java.util.ListIterator;
class source{
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
ListIterator it = list.listIterator();
while(it.hasNext()){
System.out.println(it.next());
}
System.out.println();
while(it.hasPrevious()){
System.out.println(it.previous());
}
System.out.println();
}
}
import java.util.ArrayList;
import java.util.Iterator;
public class source{
public static void main(String[] args) {
ArrayList original = new ArrayList(10);
ArrayList copy1 = new ArrayList(10);
ArrayList copy2 = new ArrayList(10);
for(int i=0; i<10; i++)
original.add(i+"");
Iterator it = original.iterator();
while(it.hasNext())
copy1.add(it.next());
System.out.println("= copy from Original to copy1 =");
System.out.println("original:" + original);
System.out.println("copy1:" + copy1);
System.out.println();
it = original.iterator();
while(it.hasNext()){
copy2.add(it.next());
it.remove();
}
System.out.println("= move from original to copy2 = ");
System.out.println("original:" + original);
System.out.println("copy2:" + copy2);
}
}
iterator μ remove() λ νΉμ μμΉμ μμκ° μλ μ½μ΄μ¨ κ²μ μμ νλ κ²μ μ μνμ.
5. Arrays
Arrays ν΄λμ€μλ λ°°μ΄μ λ€λ£¨λ λ°μ μ μ©ν λ©μλκ° μ μλμ΄ μλ€.
λ°°μ΄μ λ³΅μ¬ : copyOf(), copyOfRange()
λ°°μ΄ μ±μ°κΈ° : fill(), setAll()
λ°°μ΄μ μ λ ¬κ³Ό κ²μ : sort(), binarySearch()
λ°°μ΄μ λΉκ΅μ μΆλ ₯ : equals(), toString(), deepToString(), deepEquals()
* λ€μ°¨μ λ°°μ΄μ equals() λ‘ λΉκ΅νλ©΄ λ°°μ΄μ μ£Όμλ₯Ό λΉκ΅νλ―λ‘ false κ° λ°νλλ―λ‘ deepEquals() λ₯Ό μ¬μ©ν κ².
λ°°μ΄μ List λ‘ λ³ν : asList(Object... a)
* μμ) List list = Arrays.asList(new Integer[]{1,2,3,4,5});
List list2 = Arrays.asList(1,2,3,4,5);
* λ€λ§ asList λ‘ μ»μ List λ ν¬κΈ°λ₯Ό λ³κ²½ν μ μλ€. ν¬κΈ°λ₯Ό λ³κ²½νκ³ μ νλ€λ©΄ μλμ κ°μ΄ μ μΈν κ².
List list = new ArrayList(Arrays.asList(1,2,3,4,5));
parallelXXX(), spliterator(), stream()
: parallel λ‘ μμνλ λ©μλλ€μ λ³΄λ€ λΉ λ₯Έ κ²°κ³Όλ₯Ό μ»κΈ° μν΄ μ¬λ¬ μ°λ λκ° μμ μ λλμ΄μ μ²λ¦¬νλ€. spliterator() μ μ¬λ¬ μ°λ λκ° μ²λ¦¬ν μ μλλ‘ νλμ μμ μ μ¬λ¬ μμ μΌλ‘ λλλ Spliterator μ λ°ννλ€. stream μ 컬λ μ μ μ€νΈλ¦ΌμΌλ‘ λ³ννλ€.
6. Comparator and Comparable
Arrays.sort() λ Character ν΄λμ€μ Comparable μ ꡬνμΌλ‘ λΆν° κ°λ₯νλ κ²μ΄λ€. Comparator μ Comparable μ interface λ‘ collection μ sorting νλλ°μ νμν λ©μλλ₯Ό μ μνκ³ μλλ°, Comparable μ ꡬννκ³ μλ ν΄λμ€λ€μ κ°μ νμ μ μΈμ€ν΄μ€λΌλ¦¬ μλ‘ λΉκ΅ν μ μλ ν΄λμ€λ€μ΄λ©° κΈ°λ³Έμ μΌλ‘ μ€λ¦μ°¨μμΌλ‘ μ λ ¬λλλ‘ κ΅¬νλμ΄ μλ€. κ·Έλμ Comparable μ ꡬνν ν΄λμ€λ sorting μ΄ κ°λ₯νλ€λ κ²μ΄λ€.
public interface Comparaotr{
int compare(Object o1, Object o2);
boolean equals(Object obj);
}
public interface Comparable{
public int compareTo(Object o);
}
compare() μ compareTo() λ κ°μ κΈ°λ₯μ λͺ©μ μΌλ‘ κ³ μλ κ²μ΄λ€.
public final class Integer extends Number implements Comparable {
...
public int compareTo(Object o){
return compareTo((Integer)o);
}
public int compareTo(Integer anotherInteger){
int thisVal = this.value;
int anotherVal = anotherInteger.value;
return (thisVal<anotherVal ? -1 : (thisVal == anotherVal ? 0 : 1));
}
...
}
Integer ν΄λμ€λ₯Ό 보면 Comparable μΈν°νμ΄μ€μ compareTo(Object to) μ ꡬνν κ²μ μ μ μλ€. Comparable μ ꡬνν ν΄λμ€λ€μ κΈ°λ³Έμ μΌλ‘ μ€λ¦μ°¨μ μ λ ¬μ΄ λμ΄μμ§λ§, λ€λ₯Έ κΈ°μ€μΌλ‘ μ λ ¬νκ³ μ ν λμλ Comparator μ ꡬνν΄μ μ λ ¬ κΈ°μ€μ λ§λ€ μ μλ€.
import java.util.Arrays;
import java.util.Comparator;
public class source{
public static void main(String[] args) {
Integer[] arr = {1,2,3,4,5};
Arrays.sort(arr, new Comparator<Integer>(){
@Override
public int compare(Integer i1, Integer i2){
return i2-i1;
}
});
print(arr);
}
public static void print(Integer[] arr){
System.out.print("arr :");
for(int ele : arr){
System.out.print(ele + " ");
}
}
}
7. HashSet
HashSet μ μλ‘μ΄ μμλ₯Ό μΆκ°ν λμλ add λ addAll μ μ¬μ©νλλ°, μ΄λ―Έ μ μ₯λ μμμ μ€λ³΅λ μμλ₯Ό μΆκ°νλ €κ³ νλ€λ©΄ false λ₯Ό λ°ννλ€. HashSet μ μ μ₯μμλ₯Ό μ μ§νμ§ μμΌλ―λ‘, λ§μ½ μ μ₯μμλ₯Ό μ μ§νκ³ μ νλ€λ©΄ LinkedHashSet μ μ¬μ©νμ¬μΌ νλ€.
import java.util.HashSet;
import java.util.Set;
class source{
public static void main(String[] args) {
Object[] arr = {"1", 1, "2", "2", "3", "3", "4", "4", "4"};
Set set = new HashSet();
for(int i = 0 ; i < arr.length; i++){
set.add(arr[i]);
}
System.out.println(set);
}
}
λ§μ½ μ€λ³΅μ μ κ±°ν¨κ³Ό λμμ μ μ₯ μμλ₯Ό μ μ§νκ³ μ νλ€λ©΄ LinkedHashSet μ μ¬μ©νλ©΄ λλ€.
import java.util.*;
class source{
public static void main(String[] args) {
Set set = new HashSet();
for(int i = 0 ; set.size()<6; i++){
int num = (int)(Math.random()*45)+1;
set.add(num);
}
List list = new LinkedList(set); // LinkedList(Collection c)
Collections.sort(list);
System.out.println(list);
}
}
μλλ 1~50 μ μ«μ μ€, μ€λ³΅μμ΄ 25κ°λ₯Ό 골λΌμ 5 by 5 matrix λ‘ λ§λ€μ΄ μΆλ ₯νλ μμ μ΄λ€.
import java.util.*;
class source {
public static void main(String[] args) {
Set set = new LinkedHashSet();
int[][] board = new int[5][5];
for (int i = 0; set.size() < 25; i++) {
set.add((int) (Math.random() * 50) + 1 + "");
}
Iterator it = set.iterator();
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
board[i][j] = Integer.parseInt((String) it.next());
System.out.print((board[i][j] < 10 ? " " : " ") + board[i][j]);
}
System.out.println();
}
}
}
μλμ Person ν΄λμ€λ₯Ό μμλ‘ κ°μ§λ HashSet μ μ΄ν΄λ³΄μ.
import java.util.HashSet;
class source {
public static void main(String[] args) {
HashSet set = new HashSet();
set.add("abc");
set.add("abc");
set.add(new Person("David", 10));
set.add(new Person("David", 10));
System.out.println(set);
}
}
class Person{
String name;
int age;
Person(String name, int age){
this.name = name;
this.age = age;
}
public String toString(){
return name +":"+ age;
}
}
μ€νκ²°κ³Όλ₯Ό 보면 μμκ³Όλ λ¬λ¦¬ David:10 μ΄ μ€λ³΅λμ΄ μ μ₯λ κ²μ λ³Ό μ μλ€. HashSet λ©μλμ add λ©μλλ μλ‘μ΄ μμλ₯Ό μΆκ°ν λ, κΈ°μ‘΄ μ μ₯λ μμμ κ°μμ§ νλ³νκΈ° μν΄ μΆκ°νλ €λ μμμ equals() λ©μλμ hashCode() λ₯Ό νΈμΆν΄μ μ΄μ λν μ€λ²λΌμ΄λ©μ ν΄μ£Όμ΄μΌ μλλλ‘ λμνλ€.
import java.util.HashSet;
class source {
public static void main(String[] args) {
HashSet set = new HashSet();
set.add("abc");
set.add("abc");
set.add(new Person("David", 10));
set.add(new Person("David", 10));
System.out.println(set);
}
}
class Person{
String name;
int age;
Person(String name, int age){
this.name = name;
this.age = age;
}
public String toString(){
return name +":"+ age;
}
public boolean equals(Object obj){
if(obj instanceof Person){
Person tmp = (Person) obj;
return name.equals(tmp.name) && age==tmp.age;
}
return false;
}
public int hashCode(){
return (name+age).hashCode();
}
}
μ€λ²λΌμ΄λ©μ ν΅ν΄ μμ±ν hashcode() λ©μλλ μλμ 3 κ°μ§ μ¬νμ λ§μ‘±ν΄μΌνλ€.
1. μ€νμ€μΈ application λ΄ λμΌν κ°μ²΄μ λν΄ hashCode() λ₯Ό μ¬λ¬ λ² νΈμΆ νλλΌλ λμΌν int κ°μ λ°νν΄μΌ νλ€. νμ§λ§, μ€νμλ§λ€ λμΌν int κ°μ λ°νν νμλ μλ€. (λ¨, equals λ©μλμ ꡬνμ μ¬μ©ν λ©€λ²λ³μμ κ°μ΄ λ°λμ§ μμλ€κ³ κ°μ ν¨.)
2. equals λ©μλλ₯Ό μ΄μ©ν λΉκ΅ μ°μ°μ μν΄ true λ₯Ό μ»μ λ κ°μ²΄μ λνμ¬ κ°κ° hashCode() λ₯Ό νΈμΆν΄μ μ»μ κ²°κ³Όλ λ°λμ κ°μμΌ νλ€.
3. equals λ©μλλ₯Ό νΈμΆνμ λ false λ₯Ό λ°ννλ λ κ°μ²΄λ hashcode() νΈμΆμ λν΄ κ°μ int κ°μ λ°ννλ κ²½μ°κ° λ°μν΄λ λμ§λ§, hashing μ μ¬μ©νλ collection μ μ±λ₯μ ν₯μμν€κΈ° μν΄μ λ°λμ λ€λ₯Έ int κ°μ λ°ννλλ‘ νλ κ²μ΄ μ’λ€.
8. TreeSet
TreeSet μ λ°μ΄λ리νΈλ¦¬ ννλ‘ λ°μ΄ν°λ₯Ό μ μ₯νλ Collection class μ΄λ€. λ°μ΄λ리 νΈλ¦¬λ sorting, searching, range searching μ λμ μ±λ₯μ 보μ΄λ©°, TreeSet μ λ°μ΄λ리 νΈλ¦¬λ₯Ό κ°μ μν¨ Red - Black Tree κ΅¬μ‘°λ‘ λμ΄μλ€. Set μΈν°νμ΄μ€λ₯Ό ꡬνν κ²μ΄λ―λ‘ μ€λ³΅μ νμ©νμ§ μμΌλ©°, μ μ₯ μμλ μ μ§νμ§ μλλ€.
import java.util.TreeSet;
class source {
public static void main(String[] args) {
TreeSet set = new TreeSet();
int[] score = {80, 95, 50, 35, 45, 65, 10, 100};
for(int i=0; i<score.length; i++)
set.add(score[i]);
System.out.println("less than 50 : " + set.headSet(50));
System.out.println("bigger than 50 : " + set.tailSet(50));
}
}
headSet λ©μλμ tailSet λ©μλλ₯Ό μ΄μ©νλ©΄ κΈ°μ€ κ°λ³΄λ€ ν°/μμ κ°μ²΄λ€μ μ»μ μ μλ€.
9. HashMap
HashMap μ Map μ ꡬνν κ²μΌλ‘ key-value ννμ κ°μ λ°μ΄ν°λ‘ μ μ₯νλ€. λν hashing μ μ¬μ©νλ―λ‘ κ²μμ μμ΄μ λ°μ΄λ μ±λ₯μ 보μΈλ€.
public class HashMap extends AbstractMap implements Map, Cloneable, Serializable{
trainsient Entry[] table;
...
static class Entry implements Map.Entry{
final Object key;
Object value;
...
}
}
μλ μ€μ μμ€μ½λμ μΌλΆμΈλ°, Entry λΌλ inner class λ₯Ό μ μνκ³ Entry νμ μ λ°°μ΄μ μ μΈνκ³ μλ€. key - value ννμ λ°μ΄ν°μ΄κΈ° λλ¬Έμ νλμ ν΄λμ€λ‘ μ μν΄μ λ€λ£¨λ κ²μ΄ data integrity μΈ‘λ©΄μμ λ°λμ§νκΈ° λλ¬Έμ΄λ€.
import java.util.HashMap;
import java.util.Scanner;
class source {
public static void main(String[] args) {
HashMap map = new HashMap();
map.put("myId", "1234");
map.put("asdf", "1111");
map.put("asdf", "1234");
Scanner s = new Scanner(System.in);
while (true) {
System.out.println("Enter Id, password");
System.out.print("id :");
String id = s.nextLine().trim();
System.out.print("password :");
String password = s.nextLine().trim();
System.out.println();
if (!map.containsKey(id)) {
System.out.println("there is no id, please enter again.");
continue;
}
if (!(map.get(id)).equals(password)) {
System.out.println("Passwords do not match, please try again.");
} else {
System.out.println("success!!");
break;
}
}
}
}
맨 μ²μμ asdf λΌλ key λ₯Ό μ€λ³΅ν΄μ λ£μκΈ° λλ¬Έμ, λ§μ§λ§μ put ν key-value λ§ μ΄μλ¨λλ€.
import java.util.*;
class source {
public static void main(String[] args) {
HashMap map = new HashMap();
map.put("Kim", 100);
map.put("Lee", 100);
map.put("Kang", 100);
map.put("Ahn", 100);
Set set = map.entrySet();
Iterator it = set.iterator();
while(it.hasNext()){
Map.Entry e = (Map.Entry)it.next();
System.out.println("name : " + e.getKey() + "score : " + e.getValue());
}
set = map.keySet();
System.out.println("participant list : " + set);
Collection values = map.values();
it = values.iterator();
int total = 0;
while(it.hasNext()){
Integer i = (Integer)it.next();
total += i.intValue();
}
System.out.println("total : " + total);
System.out.println("average : " + (float)total/set.size());
System.out.println("max : " + Collections.max(values));
System.out.println("min : " + Collections.min(values));
}
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
class source {
static HashMap phoneBook = new HashMap();
public static void main(String[] args) {
addPhoneNo("Friend", "Lee", "010-1111-1111");
addPhoneNo("Friend", "Ki", "010-1121-1141");
addPhoneNo("Friend", "Kim", "010-3111-1111");
addPhoneNo("Job", "Ted", "010-5111-1111");
addPhoneNo("Job", "James", "010-6111-1111");
addPhoneNo("Job", "Travis", "010-7111-1111");
addPhoneNo("Job", "Gael", "010-8111-1111");
addPhoneNo("Food", "KFC", "010-9111-1111");
printList();
}
static void addPhoneNo(String groupName, String name, String tel){
addGroup(groupName);
HashMap group = (HashMap)phoneBook.get(groupName);
group.put(tel, name);
}
static void addGroup(String groupName){
if(!phoneBook.containsKey(groupName))
phoneBook.put(groupName, new HashMap());
}
static void addPhoneNo(String name, String tel){
addPhoneNo("etc", name, tel);
}
static void printList(){
Set set = phoneBook.entrySet();
Iterator it = set.iterator();
while(it.hasNext()){
Map.Entry e = (Map.Entry)it.next();
Set subSet = ((HashMap)e.getValue()).entrySet();
Iterator subIt = subSet.iterator();
System.out.println(" * " + e.getKey()+"[" + subSet.size() + "]");
while(subIt.hasNext()){
Map.Entry subE = (Map.Entry)subIt.next();
String telNo = (String)subE.getKey();
String name = (String)subE.getValue();
System.out.println(name + " " + telNo);
}
System.out.println();
}
}
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
class source {
static HashMap phoneBook = new HashMap();
public static void main(String[] args) {
String[] data = {"A", "B", "C", "D", "E", "F", "G", "A", "A", "B", "C", "G", "A", "A", "A"};
HashMap map = new HashMap();
for(int i=0; i<data.length; i++){
if(map.containsKey(data[i])){
Integer value = (Integer) map.get(data[i]);
map.put(data[i], value.intValue() +1);
} else {
map.put(data[i], 1);
}
}
Iterator it = map.entrySet().iterator();
while(it.hasNext()){
Map.Entry entry = (Map.Entry)it.next();
int value = ((Integer)entry.getValue()).intValue();
System.out.println(entry.getKey() + " : " + printBar('#', value) + " " + value);
}
}
public static String printBar(char ch, int value){
char[] bar = new char[value];
for(int i=0; i<bar.length; i++)
bar[i] = ch;
return new String(bar);
}
}
String λ°°μ΄μΈ data λ₯Ό μννλ©°, νμ¬ HashMap μ data[i] μ μ€λ³΅λλ key κ° μμΌλ©΄ map.put(data[i], new Integer(1)); μ μννκ³ λ§μ½ HashMap μ data[i] μ μ€λ³΅λλ key κ° μ‘΄μ¬νλ©΄ value λ₯Ό μ»μ΄μ€κ³ , value μ 1 μ λν κ°μ value λ‘ ν΄μ λ€μ put ν΄μ€λ€. κ·Έλ¬λ©΄ HashMap μλ data λ°°μ΄μ μμμ λΉλκ° μ μ₯λλ©° μ΄λ₯Ό iterator μ μ΄μ©νμ¬ μννλλ°, printBar ν¨μλ₯Ό μ΄μ©ν΄μ κ·Έλνλ₯Ό κ·Έλ €μ£Όλ μμ μ΄λ€.
[ Hash Function ]
Hashing μ ꡬνν 컬λ μ ν΄λμ€λ HashSet, HashMap, Hashtable λ±μ΄ μμΌλ©° Hashtable μ collections framework κ° λμ λλ©° HashMap μΌλ‘ λ체λμμΌλ, μ΄μ μ μμ±λ μ½λμμ νΈνμ μν΄ λ¨μμλ κ²μ΄λ€.
10. TreeMap
λ°μ΄λ리μμΉνΈλ¦¬ ννλ‘ key-value μ pair λ°μ΄ν°λ₯Ό μ μ₯νλ―λ‘ κ²μκ³Ό μ λ ¬μ μ ν©ν ν΄λμ€μ΄λ€. λ€λ§ κ²μ μ±λ₯μ κ΄ν΄μλ HashMap μ΄ TreeMap λ³΄λ€ λλΆλΆ μ°μνλ―λ‘ HashMap μ μ¬μ©νλ κ²μ΄ μ’λ€. range search λ sorting μ κ²½μ°μλ TreeMap μ΄ μ°μνλ€.
11. Properties
Hashtable μ μμλ°μ ꡬνν κ²μΌλ‘ key - value κ°μ String, String ννλ‘ μ μ₯νλ€. μ΄ν리μΌμ΄μ μ νκ²½μ€μ κ³Ό κ΄λ ¨λ property λ₯Ό μ μ₯νλλ°μ μ£Όλ‘ μ¬μ©λλ©° λ°μ΄ν°λ₯Ό νμΌλ‘λΆν° μ½κ³ μ°λ νΈλ¦¬ν κΈ°λ₯μ μ 곡νλ€.
import java.util.Enumeration;
import java.util.Properties;
class source{
public static void main(String[] args) {
Properties prop = new Properties();
prop.setProperty("timeout", "30");
prop.setProperty("language", "kr");
prop.setProperty("size", "10");
prop.setProperty("capacity", "10");
Enumeration e = prop.propertyNames();
while(e.hasMoreElements()){
String element = (String)e.nextElement();
System.out.println(element + "=" + prop.getProperty(element));
}
System.out.println();
prop.setProperty("size", "20");
System.out.println("size=" + prop.getProperty("size"));
System.out.println("capacity=" + prop.getProperty("capacity", "20"));
System.out.println("load-factor=" + prop.getProperty("load-factor", "0.75"));
System.out.println(prop);
prop.list(System.out);
}
}
12. Collections
Collections ν΄λμ€λ 컬λ μ κ³Ό κ΄λ ¨λ fill, copy, sort, binarySearch λ±μ λ©μλλ₯Ό μ 곡νλ€. multi-thread νλ‘κ·Έλλ°μμ νλμ κ°μ²΄λ₯Ό μ¬λ¬ thread κ° λμμ μ κ·Όν μ μμ΄μ data inconsistency λ₯Ό μν΄μλ 곡μ λλ κ°μ²΄μ synchronization μ ν΄μ£Όμ΄μΌ νλ€. Vector, Hashtable μ μ체μ μΌλ‘ λκΈ°ν μ²λ¦¬κ° λμ΄μμ§λ§ multi-threading μ μ¬μ©νμ§ μλ κ²½μ°μλ μ±λ₯μ μ νμν€λ μμΈμ΄ λλ―λ‘, JAVA μ μλ‘ μΆκ°λ ArrayList μ HashMap λ±μ λκΈ°ν μ²λ¦¬κ° λμ΄μμ§ μμλ°, μ΄λ νμν κ²½μ°μλ§ java.util.Collections μμ Synchronization λ©μλλ₯Ό μ΄μ©ν΄μ μ²λ¦¬ν΄μ£Όλλ‘ νκΈ° μν¨μ΄λ€.
λν 컬λ μ μ λ°μ΄ν°λ₯Ό 보νΈνκΈ° μν΄ read - only λ‘ λ§λ€μ΄μΌ ν κ²½μ°κ° λ°μνλλ° μ΄λ Collection μ unmodifiable... λ©μλλ₯Ό μ¬μ©νλ©΄ λλ€. κ·Έλ¦¬κ³ μ€μ§ νλμ κ°μ²΄λ§ μ μ₯νλ 컬λ μ (μ±κΈν€ 컬λ μ )μ λ§λ€κ³ μ ν λμλ singleton... λ©μλλ₯Ό μ¬μ©νλ©΄ λλ€. 컬λ μ μ ν μ’ λ₯μ κ°μ²΄λ§ μ μ₯νλλ‘ νλ κ²½μ°μλ checked ... λ©μλλ₯Ό μ¬μ©νλ©΄ λλ€.
References
http://www.yes24.com/Product/Goods/24259565
'π» Study ! > JAVA' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[CH14] Lambda & Stream (0) | 2022.09.03 |
---|---|
[CH12] Generics, Enumeration, Annotation (0) | 2022.08.30 |
[CH09] java.lang ν¨ν€μ§ (0) | 2022.08.21 |
[CH08] μμΈμ²λ¦¬(Exception handling) (0) | 2022.08.02 |
[CH07] κ°μ²΄μ§ν₯νλ‘κ·Έλλ°4 (OOP) (0) | 2022.08.02 |