๊ด€๋ฆฌ ๋ฉ”๋‰ด

gi_dor

Java ์ œ๋„ค๋ฆญ, ์ปฌ๋ ‰์…˜ , Set , List , Map , Stack ๋ณธ๋ฌธ

Language/Java

Java ์ œ๋„ค๋ฆญ, ์ปฌ๋ ‰์…˜ , Set , List , Map , Stack

๊ธฐ๋Œ 2023. 11. 20. 17:22

๐Ÿ”ธ ์ œ๋„ค๋ฆญ

  • ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด ๊ฒฐ์ •๋˜์ง€ ์•Š๊ณ  , ํŒŒ๋ผ๋ฏธํ„ฐ ํƒ€์ž…(๋ณ„์นญ)๋งŒ ์ง€์ •ํ•œ๋‹ค ํ‘œ๊ธฐ๋ฐฉ๋ฒ•์€ < > 
  • ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•  ๋ณ€์ˆ˜(๋ฐ์ดํ„ฐ) ์˜ ํƒ€์ž…์„ ์™ธ๋ถ€์—์„œ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค
  • Java ์—์„œ๋Š” ์ œ๋„ค๋ฆญ์„ ์‚ฌ์šฉํ•  ๋•Œ , ๊ธฐ๋ณธ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ํ•ด๋‹น ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž…์˜
    ๋ž˜ํผํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค
  • ๊ฐ์ฒด ์ƒ์„ฑ์‹œ์  , ๊ตฌํ˜„ํด๋ž˜์Šค , ๊ตฌํ˜„์‹œ์   , ๋ฉ”์„œ๋“œ ์‹คํ–‰์‹œ ๋ณ„์นญ์„   ์ง€์ •
  • ๋ฐ์ดํ„ฐ ํƒ€์ž…์€ ํด๋ž˜์Šค ํ˜น์€ ์ธํ„ฐํŽ˜์ด์Šค ํƒ€์ž…๋งŒ ๊ฐ€๋Šฅ

๐Ÿ”น ์ œ๋„ค๋ฆญ ํด๋ž˜์Šค 

  • ํŒŒ๋ผ๋ฏธํ„ฐ ํƒ€์ž…์„ ํ•˜๋‚˜ ์ด์ƒ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํด๋ž˜์Šค
  • ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์˜ ๊ฐ์ฒด๋ฅผ ๋‹ค๋ฃจ๋Š” ํด๋ž˜์Šค๋ฅผ ์ œ๋„ค๋ฆญ ํด๋ž˜์Šค๋กœ ์ •์˜ํ•˜๋ฉด ํƒ€์ž…์˜ ์•ˆ์ •์„ฑ๊ณผ ํ˜•๋ณ€ํ™˜ ์ฝ”๋“œ๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค
  • ์ œ๋„ค๋ฆญ ํด๋ž˜์Šค 
    - ์ž๋ฃŒ๊ตฌ์กฐ :  Set List Map
    - ๋ฐ์ดํ„ฐ ๋ฐ˜๋ณต์ฒ˜๋ฆฌ : Iterator , Enumeration
  • ์ œ๋„ค๋ฆญ ํด๋ž˜์Šค ๊ตฌํ˜„์‹œ ์ฃผ์˜
    - Tํƒ€์ž…์˜ ๋ฐฐ์—ด์ƒ์„ฑ  ๋ถˆ๊ฐ€
    - Tํƒ€์ž…์˜ ํด๋ž˜์Šค ๋ณ€์ˆ˜ ์ƒ์„ฑ ๋ถˆ๊ฐ€
class Box<T> {
T t; //๊ฐ€๋Šฅ
static T t; // cv ์„ ์–ธ๋ถˆ๊ฐ€
T[] data = new T[10]; // Tํƒ€์ž… ๋ฐฐ์—ด ์ƒ์„ฑ๋ถˆ๊ฐ€
    public void add(T t){
 	   this.t = t;
    }

 }

โ—ป ์ œ๋„ค๋ฆญ ํด๋ž˜์Šค

public class GenericBox<T> {
    // <T> ํƒ€์ž… ํŒŒ๋ผ๋ฏธํ„ฐ๋ผ๊ณ  ํ•œ๋‹ค.
    // ํด๋ž˜์Šค๋Š” ์ œ๋„ค๋ฆญ ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜ <T>๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค
    
    private T data; // Tํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ ์ €์žฅํ• ์ˆ˜ ์žˆ๋Š” ๋ฉค๋ฒ„๋ณ€์ˆ˜ data , ์–ด๋–ค ๋ฐ์ดํ„ฐ์˜ ๊ฐ’์„ ์ €์žฅํ• ์ˆ˜ ์žˆ์Œ
    
    public void add(T data) {
        this.data = data;
    }

    public T get() {
        return data;
    }
}
public class App2 {
    public static void main(String[] args) {

        /*
        ์ œ๋„ค๋ฆญ์ด ์ ์šฉ๋œ Box ํด๋ž˜์Šค
            - ๊ฐ์ฒด ์ƒ์„ฑ ์‹œ์ ์— ํƒ€์ž…ํŒŒ๋ผ๋ฏธํ„ฐ์— ๊ทธ ๊ฐ์ฒด์˜ ํƒ€์ž…์„ ์ง€์ •ํ• ์ˆ˜ ์žˆ๋‹ค
            - ๊ฐ์ฒด ์ƒ์„ฑ์‹œ ์ง€์ •ํ•œ ํƒ€์ž…์˜ ๊ฐ์ฒด๋งŒ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค
            - ์ €์žฅ๋œ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ํ˜•๋ณ€ํ™˜์ด ํ•„์š”์—†๋‹ค.
         */

        // ๊ฐ์ฒด ์ƒ์„ฑ์‹œ์ ์— ํƒ€์ž…์„ ๋ช…์‹œํ•จ
        GenericBox<String> box1 = new GenericBox<String>();

        box1.add("๊ธฐ์„ ");
        String value1 = box1.get();

        // int๊ฐ€ ์•„๋‹Œ Wrapper ํƒ€์ž… Integer
        GenericBox<Integer> box2 = new GenericBox<>();
        box2.add(19);
        int value2 = box2.get();


    }
  public static void main(String[] args) {
    // T์˜ ํƒ€์ž…์ด String ํด๋ž˜์Šค๊ฐ€ ๋œ๋‹ค.
    Sample<String> s1 = new Sample<String>();		
    
    // T์˜ ํƒ€์ž…์ด Customer ํด๋ž˜์Šค๊ฐ€ ๋œ๋‹ค.
    Sample<Customer> s2 = new Sample<Customer>();
    
    // T์˜ํƒ€์ž…์ด Apple ํด๋ž˜์Šค๊ฐ€ ๋œ๋‹ค.
    // ์ค‘์ฒฉ๋œ ์ œ๋„ค๋ฆญ ํƒ€์ž…
    // Box๊ฐ€ Apple ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•˜๊ณ  Sampleํด๋ž˜์Šค๊ฐ€ Box ๊ฐ์ฒด๋ฅผ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•ด ์ค‘์ฒฉ ์ œ๋„ค๋ฆญ ์‚ฌ์šฉ
    Sample<Box<Apple>> s3 = new Sample<Box<Apple>>();	
    
    // T์˜ ํƒ€์ž…์ด Object๊ฐ€ ๋œ๋‹ค
    Sample s4 = new Sample();	
    Sample<Object> s5 = new Sample<Object>();

    // Sample<int> s4 = new Sample<int>(); // ์ปดํŒŒ์ผ ์˜ค๋ฅ˜
    // T์˜ ํƒ€์ž…์ด Integer๊ฐ€ ๋œ๋‹ค.
    Sample<Integer> s5 = new Sample<Integer>();

 

๐Ÿ”น ์ œ๋„ค๋ฆญ ํด๋ž˜์Šค๋ฅผ ์™œ ์‚ฌ์šฉ ํ•˜๋Š”๊ฑด๋ฐ ?

  • ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ์˜ ํƒ€์ž…์„ ์™ธ๋ถ€์—์„œ ์ง€์ •ํ•œ๋‹ค
  • ๊ฐ์ฒด ์ƒ์„ฑ ์‹œ์ ์— ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์ง€์ •
  • ์‚ฌ๊ณผ๋งŒ ๋‹ด์„์ˆ˜ ์žˆ๋Š” Box ํด๋ž˜์Šค
  public class Box {
    private Apple fruit; // ๋‹ค๋ฅธ๋ฐ์ดํ„ฐ ํƒ€์ž… ์ €์žฅํ• ์ˆ˜ ์—†๊ฒŒ Apple ํƒ€์ž… ๋ฉค๋ฒ„๋ณ€์ˆ˜ ์„ ์–ธ
    public void setFruit(Apple fruit) {
      this.fruit = fruit;
    }
    public Apple getFruit() {
      return fruit;
    }
  }
  • ์—ฌ๋Ÿฌ ๊ณผ์ผ์„ ๋‹ด์„์ˆ˜ ์žˆ๋Š” Box ํด๋ž˜์Šค
  public class Box {
    private Object fruit;	// Object๋Š” ์ข…๋ฅ˜ ์ƒ๊ด€ ์—†์ด ๋‹ค ์ €์žฅ๋ ์ˆ˜ ์žˆ๋‹ค
    public void setFruit(Object fruit) {
      this.fruit = fruit;
    }
    public Object getFruit() {
      return fruit;
    }
  }
  • ๊ณผ์ผ๋งŒ ์ €์žฅ๋œ๋‹ค๋Š” ๋ณด์žฅ์ด์—†๋‹ค
  • ํ•œ ์ข…๋ฅ˜์˜ ๊ณผ์ผ๋งŒ ์ €์žฅ๋œ๋‹ค๋Š” ๋ณด์žฅ๋„ ์—†๋‹ค
  • ๊บผ๋ƒˆ์„ ๋•Œ ์–ด๋–ค๊ณผ์ผ์ด ๋‚˜์˜ฌ์ง€ ์˜ˆ์ƒํ•  ์ˆ˜ ์—†๋‹ค
  • ๊ฐ์ฒด ์ƒ์„ฑ์‹œ ์ง€์ •๋œ ๊ณผ์ผ๋งŒ ๋‹ด์„์ˆ˜ ์žˆ๋Š” Box ํด๋ž˜์Šค
  public class Box<T> {	//  Tํƒ€์ž… ํŒŒ๋ผ๋ฏธํ„ฐ
    private T fruit;	// T๋กœ ์„ ์–ธ๋˜์—ˆ์œผ๋ฏ€๋กœ Box ๊ฐ์ฒด๋‚ด์—์„œ Tํƒ€์ž… ๋ฐ์ดํ„ฐ ์ €์žฅ๊ฐ€๋Šฅ
    public void setFruit(T fruit) {
      this.fruit = fruit;
    }
    public T getFruit() {
      return fruit;
    }
  }
  // ์•ˆ์ •์„ฑ์œ ์ง€๋ฅผ ํ• ์ˆ˜ ์žˆ๊ฒŒ๋œ๋‹ค
  // ๋˜๋‹ค๋ฅธ ์˜ˆ๋กœ class Box<String> ์‚ฌ์šฉ ์‹œ ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ๋งŒ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค

 

๐Ÿ“Œ ์ œ๋„ค๋ฆญ์„ ์‚ฌ์šฉํ•ด ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ๊ฒฝ์šฐ์˜ ์ด๋กœ์šด ์ 

  • ์›ํ•˜์ง€ ์•Š๋Š” ์ž๋ฃŒํ˜•์˜ ๊ฐ’์ด ์ž…๋ ฅ๋˜๋Š” ๊ฒƒ์„ ์ปดํŒŒ์ผ ์‹œ์ ์— ์•Œ์ˆ˜ ์žˆ๋‹ค
  • ๊ฐ’์„ ๊บผ๋‚ผ ๋•Œ ํƒ€์ž… ํ˜•๋ณ€ํ™˜ ํ•˜์ง€ ์•Š์•„๋„๋œ๋‹ค 

๐Ÿ”น ์™€์ผ๋“œ ์นด๋“œ

  • ์ œ๋„ค๋ฆญ ํƒ€์ž…์„ ์‚ฌ์šฉํ•  ๋•Œ ๋ถˆํŠน์ •ํ•œ ํƒ€์ž…์„ ๋‚˜ํƒ€๋‚ด๋Š”๋ฐ ์“ฐ์ธ๋‹ค
  • ์˜ˆ๋ฅผ๋“ค๋ฉด List<?> ๋Š” ์–ด๋–ค ํƒ€์ž…์˜์š”์†Œ๋„ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋Š” List๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค
  • ํŠน์ • ํƒ€์ž…์„ ์•Œ์ง€ ๋ชปํ•  ๋•Œ ์“ฐ์ธ๋‹ค
  ArrayList<? extends Product> list = new ArrayList<Tv>();
  ArrayList<? extends Product> list = new ArrayList<SmartPhone>();
  // list ์ฐธ์กฐ๋ณ€์ˆ˜๋Š” Product ๋ฐ ๊ทธ ์ž์†์ด ์ œ๋„ค๋ฆญํƒ€์ž…์œผ๋กœ ์ง€์ •๋œ ArrayList๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค.
  	 <?>
            - ์ œํ•œ์—†์Œ
            - ํƒ€์ž… ํŒŒํƒ€๋ฏธํ„ฐ์˜ ๊ตฌ์ฒด์ ์ธ ํƒ€์ž…์œผ๋กœ ๋ชจ๋“  ํด๋ž˜์Šค , ๋ชจ๋“  ์ธํ„ฐํŽ˜์ด์Šค ์ ์š”

        <? extendx ์ƒ์œ„ํƒ€์ž…>
            - ์ƒ์œ„ ํด๋ž˜์Šค ์ œํ•œ
            - ํ•˜์ž… ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ตฌ์ฒด์ ์ธ ํƒ€์ž…์œผ๋กœ ์ƒ์œ„ํƒ€์ž… ํ˜น์€ ์ง€์ •๋œ
            ์ƒ์œ„ ํƒ€์ž…์˜ ํ•˜์œ„ ํƒ€์ž…๋งŒ ๊ฐ€๋Šฅ

          <?  super ํ•˜์œ„ํƒ€์ž…>
          - ํ•˜์œ„ ํด๋ž˜์Šค ์ œํ•œ
          - ํƒ€์ž… ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ตฌ์ฒด์ ์ธ ํƒ€์ž…์œผ๋กœ ํ•˜์œ„ํƒ€์ž…ํ˜น์€ ์ง€์ •๋œ ํ•˜์œ„ํƒ€์ž…์˜ ์ƒ์œ„ํƒ€์ž…๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค

 

class Product {
    // Product ํด๋ž˜์Šค ๋‚ด์šฉ
}

class Tv extends Product {
    // Tv ํด๋ž˜์Šค ๋‚ด์šฉ
}

class SmartPhone extends Product {
    // SmartPhone ํด๋ž˜์Šค ๋‚ด์šฉ
}
ArrayList<Tv> tvList = new ArrayList<>();
tvList.add(new Tv());

ArrayList<SmartPhone> phoneList = new ArrayList<>();
phoneList.add(new SmartPhone());

ArrayList<? extends Product> list1 = tvList; // Tv ํƒ€์ž…์˜ ArrayList๋ฅผ ์ƒํ•œ ์™€์ผ๋“œ์นด๋“œ์— ํ• ๋‹น
ArrayList<? extends Product> list2 = phoneList; // SmartPhone ํƒ€์ž…์˜ ArrayList๋ฅผ ์ƒํ•œ ์™€์ผ๋“œ์นด๋“œ์— ํ• ๋‹น
for (Product p : list1) {
    // Product ๊ฐ์ฒด ๋˜๋Š” Product ํ•˜์œ„ ํด๋ž˜์Šค ๊ฐ์ฒด๋ฅผ ์ฝ์„ ์ˆ˜ ์žˆ์Œ
}

for (Product p : list2) {
    // Product ๊ฐ์ฒด ๋˜๋Š” Product ํ•˜์œ„ ํด๋ž˜์Šค ๊ฐ์ฒด๋ฅผ ์ฝ์„ ์ˆ˜ ์žˆ์Œ
}

๐Ÿ”ธ ์ปฌ๋ ‰์…˜ ํ”„๋ ˆ์ž„์›Œํฌ

๐Ÿ”น ์ปฌ๋ ‰์…˜ ์ธํ„ฐํŽ˜์ด์Šค Collection<E>

  • ๋ชจ๋“  ์ž๋ฃŒ๊ตฌ์กฐ์˜ ์ตœ์ƒ์œ„ ์ธํ„ฐํŽ˜์ด์Šค
boolean add(E e)
// ์ž๋ฃŒ๊ตฌ์กฐ์— ์ƒˆ๋กœ์šด ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.
boolean addAll(Collection<? extends E> c)
// ์ž๋ฃŒ๊ตฌ์กฐ์— ๋‹ค๋ฅธ ์ž๋ฃŒ๊ตฌ์กฐ์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.
void clear()
// ์ž๋ฃŒ๊ตฌ์กฐ์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์‚ญ์ œํ•œ๋‹ค.
boolean contains(Object e)
// ์ž๋ฃŒ๊ตฌ์กฐ์— ์ง€์ •๋œ ๊ฐ์ฒด๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ์กฐํšŒํ•œ๋‹ค.
boolean isEmpty()
// ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ ๋น„์—ˆ๋Š”์ง€ ์กฐํšŒํ•œ๋‹ค.
Iterator<E> iterator()
// ์ž๋ฃŒ๊ตฌ์กฐ์˜ ๊ฐ ์š”์†Œ๋ฅผ ๋ฐ˜๋ณตํ•ด์„œ ์ถ”์ถœํ•ด์ฃผ๋Š” ๋ฐ˜๋ณต์ž๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
boolean remove(Object e)
// ์ž๋ฃŒ๊ตฌ์กฐ์—์„œ ์ง€์ •๋œ ๊ฐ์ฒด๋ฅผ ์‚ญ์ œํ•œ๋‹ค.
int size()
// ์ž๋ฃŒ๊ตฌ์กฐ์— ์ €์žฅ๋œ ์š”์†Œ์˜ ๊ฐฏ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
Object[] toArray()
// ์ž๋ฃŒ๊ตฌ์กฐ์— ์ €์žฅ๋œ ์š”์†Œ๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

 


๐Ÿ”น Set

  • ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋‹ค ( ๋™์ผํ•œ ์š”์†Œ๋ฅผ ์—ฌ๋Ÿฌ๋ฒˆ ์ถ”๊ฐ€ํ•ด๋„ ์ค‘๋ณต ์ €์žฅ๋˜์ง€ ์•Š๋Š”๋‹ค)
  • ์ˆœ์„œ๋ฅผ ๋ณด์žฅ ํ•˜์ง€ ์•Š๋Š”๋‹ค ( ์ˆœ์„œ๋ฅผ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š๊ณ  ๋‹จ์ˆœํžˆ ์ค‘๋ณต ์ œ๊ฑฐ ์šฉ์œผ๋กœ ์“ฐ๋ฉด ๋”ฑ๋ฆฌ๋‹ค)
  • ๊ณ ์œ ํ•œ ์š”์†Œ๋งŒ์„ ์ €์žฅํ•œ๋‹ค
  • ์š”์†Œ์˜ ๋™๋“ฑ์„ฑ(equality)์„ ๊ธฐ์ค€์œผ๋กœ ํŒ๋‹จ ํ•œ๋‹ค  ( equals() , hashCode() ๋ฉ”์„œ๋“œ ์ž˜์“ฐ์ž )
  • HashSet - ์ค‘๋ณตํ—ˆ์šฉ ์•ˆํ•จ ์ค‘๋ณต์ œ๊ฑฐ , ์ˆœ์„œ ๋ณด์žฅ ์•ˆํ•จ
  • TreeSet - ์ค‘๋ณตํ—ˆ์šฉ ์•ˆํ•จ ์ค‘๋ณต์ œ๊ฑฐ , ์˜ˆ์™ธ์ ์œผ๋กœ ์ •๋ ฌํ•จ  ์š”์†Œ์˜ ํฌ๊ธฐ ๋˜๋Š” ๋น„๊ต ๊ธฐ์ค€์— ๋”ฐ๋ผ ๊ฒฐ์ •๋จ

โ—ป TreeSet

  • Set<E> ๊ตฌํ˜„ ํด๋ž˜์Šค
  • ์ค‘๋ณต๋œ ๊ฐ์ฒด์˜ ์ €์žฅ์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š๋‹ค
  • ๊ฐ์ฒด๊ฐ€ ์ €์žฅ๋  ๋•Œ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜์–ด ์ €์žฅ๋œ๋‹ค
  •  TreeSet์— ์ €์žฅํ•˜๋Š” ๊ฐ์ฒด๋Š” ๋ฐ˜๋“œ์‹œ Comparable<E>๋ฅผ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ๊ฐ์ฒด์—ฌ์•ผ ํ•œ๋‹ค
  • String Wrapper ํด๋ž˜์Šค๋Š” Comparable<E> ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ๋‹ค
package Java.collection;

import java.util.TreeSet;

public class TreeSetApp1 {
    public static void main(String[] args) {

        TreeSet<String> set = new TreeSet<String>();
        set.add("๋‚˜ํƒ€");
        set.add("๊ฐ€๋„ค์ƒค");
        set.add("์†ŒํŒจ์™•");
        set.add("๋ด‰์ถ”");
        set.add("ํ˜„๋•");

        // ์ž์Œ ์ˆœ์„œ๋กœ ์ •๋ ฌ๋œ ๊ฑธ ํ™•์ธํ• ์ˆ˜ ์žˆ์Œ
        for (String name : set) {
            System.out.println(name);
        }
    }
}

// ์ถœ๋ ฅ
/*
๊ฐ€๋„ค์ƒค
๋‚˜ํƒ€
๋ด‰์ถ”
์†ŒํŒจ์™•
ํ˜„๋•
*/
public class TreeSetApp2 {

    public static void main(String[] args) {

        // ๋กœ๋˜ ๋ฒˆํ˜ธ ์ถ”์ถœํ•˜๊ธฐ
        // 1. ๋ฒˆํ˜ธ์ถ”์ถœ
        // 2. ์ค‘๋ณต ์ฒดํฌ - Set ์œผ๋กœ ์ฒ˜๋ฆฌ
        // 3. ์ €์žฅ
        // 4. ์ •๋ ฌ - TreeSet ์ฒ˜๋ฆฌ

        Random random = new Random();
        TreeSet<Integer> lotto = new TreeSet<>();
        // - TreeSet<E> ์‚ฌ์šฉ
        // ์ค‘๋ณต ๊ฐ’์€ ์ €์žฅํ•˜์ง€ ์•Š์Œ , ์ž๋™์ •๋ ฌ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ  ํ•ด์คŒ ๊ฐœ๊ฟ€

        while (true) {
            int num = random.nextInt(45) + 1;

            // ๋ณ€์ˆ˜ lotto ์— ๋‹ด์Œ
            lotto.add(num);

            if (lotto.size() == 6) {
                break;
            }
        }
        System.out.println(lotto);
    }
}

 

โ—ป HashSet 

  • Set<E> ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ ๊ตฌํ˜„ ํด๋ž˜์Šค
  • ์ค‘๋ณต๋œ ๊ฐ์ฒด์˜ ์ €์žฅ์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š” ์ž๋ฃŒ๊ตฌ์กฐ ๊ตฌํ˜„ ํด๋ž˜์Šค
  • ๊ฐ์ฒด์˜ ๋™์ผ์„ฑ ๋น„๊ต๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด equals() , hashcode()
    ๋ฉ”์„œ๋“œ๋ฅผ์žฌ ์ •์˜ํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค (HashSet<E> ์— ์ €์žฅ๋˜๋Š” ๊ฐ์ฒด๊ฐ€ ๊ตฌํ˜„ ํ•ด์•ผํ•จ)
  •  String Wrapper ํด๋ž˜์Šค๋Š” equals() ์™€ hashCode()๊ฐ€ ์ด๋ฏธ ์žฌ์ •์˜ ๋˜์–ด์žˆ๋‹ค
package Java.collection;

import java.util.HashSet;
import java.util.stream.Stream;

public class HashSetApp1 {
    public static void main(String[] args) {

        // String ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•˜๋Š” HashSet ๊ฐ์ฒด ์ƒ์„ฑ
        HashSet<String> set = new HashSet<String>();

        // boolean add(E e) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด HashSet ๊ฐ์ฒด์— String ๊ฐ์ฒด์— ์ €์žฅํ•˜๊ธฐ
        set.add("ํ•˜๋‚˜");
        set.add("๋‘˜");
        set.add("์…‹");
        set.add("๋„ท");
        set.add("๋‹ค์„ฏ");
        set.add("์—ฌ์„ฏ");
        set.add("์—ฌ์„ฏ");  // ์ค‘๋ณต ์ €์žฅ์•ˆ๋œ
        set.add("์—ฌ์„ฏ");  // ์ค‘๋ณต ์ €์žฅ์•ˆ๋œ
        set.add("์—ฌ์„ฏ");  // ์ค‘๋ณต ์ €์žฅ์•ˆ๋œ

        // int size() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด HahSet ๊ฐ์ฒด์— ์ €์žฅ๋œ ๊ฐ์ฒด ๊ฐฏ์ˆ˜ ์กฐํšŒ
        int size = set.size();
        System.out.println("์ €์žฅ๋œ ๊ฐ์ฒด์˜ ๊ฐฏ์ˆ˜ : " + size); // 6 ์ถœ๋ ฅ๋จ

        // ํ–ฅ์ƒ๋œ for๋ฌธ์„ ์‚ฌ์šฉํ•ด HashSet ๊ฐ์ฒด์— ์ €์žฅ๋œ ๊ฐ์ฒด๋“ค์„ ํ•˜๋‚˜์”ฉ ์ฒ˜๋ฆฌํ•˜๊ธฐ
        // HashSet์€ index๊ฐ€ ์—†๋‹ค , ์ผ๋ฐ˜ for๋ฌธ์œผ๋กœ HashSet๊ฐ์ฒด์— ์ €์žฅ๋œ ๊ฐ’๋“ค์„ ์ฒ˜๋ฆฌํ• ์ˆ˜ ์žˆ๋‹ค

        for (String name : set) {
            System.out.println(name);
        }

        // Stream ์„ ์‚ฌ์šฉํ•ด์„œ HashSet๊ฐ์ฒด์— ์ €์žฅ๋œ ๊ฐ์ฒด๋“ค์„ ํ•˜๋‚˜์”ฉ ์ฒ˜๋ฆฌ

        System.out.println("### Stream์„ ํ™œ์šฉํ•ด HashSet๊ฐ์ฒด์— ์ €์žฅ๋œ ๊ฐ์ฒด ์ฒ˜๋ฆฌ");
        Stream<String> stream = set.stream();
        stream.forEach((String value) -> System.out.println(value));
//        public void Consumer<String>(){
//            public void accept(String value){
//                System.out.println(value);
//            }
//        });

        // boolean contains(Object o)๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ์ฒด์˜ ์ €์žฅ์—ฌ๋ถ€๋ฅผ ์กฐํšŒ
        boolean isExist = set.contains("๋‘˜");
        System.out.println("์กด์žฌ์—ฌ๋ถ€ : " + isExist);

        // boolean isEmpty() ๋ฅผ ์‚ฌ์šฉํ•ด HashSet๊ฐ์ฒด๊ฐ€ ๋น„์–ด์žˆ๋Š”์ง€ ์กฐํšŒ
        boolean isEmpty = set.isEmpty();
        System.out.println("๋น„์–ด์žˆ๋Š”๊ฐ€ ? :" + isEmpty);

        // void clear() HashSet๊ฐ์ฒด์— ์ €์žฅ๋œ ๋ชจ๋“  ๊ฐ์ฒด ์‚ญ์ œํ•˜๊ธฐ
        set.clear();

        System.out.println("๋น„์–ด์žˆ๋Š”๊ฐ€ 2 ? : " + set.isEmpty());
    }
}
package Java.collection;

import java.util.HashSet;
import java.util.Objects;

public class HashSetApp2 {

    public static void main(String[] args) {

        HashSet<User> set = new HashSet<>();

        set.add(new User("kim", "๊น€์œ ์‹ "));
        set.add(new User("Han", "ํ•œ๋ช…ํšŒ"));
        set.add(new User("Han", "ํ•œ๋ช…ํšŒ"));
        set.add(new User("Han", "ํ•œ๋ช…ํšŒ"));
        set.add(new User("ryu", "๋ฅ˜์Šน๋ฃก"));
        set.add(new User("shin", "์‹ ๋ผ"));
        set.add(new User("kahn", "์นธ ์ผ€์‹"));
        set.add(new User("ahn", "์•ˆ์ค‘๊ทผ"));

        for (User u : set) {
            System.out.println(u.id + ", " + u.name + ", " + u);
        }

    }

    static class User {
        String id;
        String name;

        public User(String id, String name) {
            this.id = id;
            this.name = name;
        }

        /*
        hashCode() ์™€ equals() ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•ด์„œ
        id ๊ฐ’์ด ๊ฐ™์€ User ๊ฐ์ฒด๋Š” ๊ฐ™์€ ๊ฐ์ฒด๋กœ ํŒ๋‹จํ•˜๋„๋ก ํ–ˆ๋‹ค.
         */
        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            User user = (User) o;
            return Objects.equals(id, user.id);
        }

        @Override
        public int hashCode() {
            return Objects.hash(id);    // id๋กœ๋งŒ hashCode ๋งŒ๋“ค์–ด์ง , id๊ฐ€ ๊ฐ™๋‹ค๋ฉด hashCode๊ฐ€ ๊ฐ™๋‹ค๋Š”๊ฒƒ์„ ์˜๋ฏธํ•จ
        }
    }
}

// ์ถœ๋ ฅ ๊ฒฐ๊ณผ
/*
Han, ํ•œ๋ช…ํšŒ, Java.collection.HashSetApp2$User@11a94
ahn, ์•ˆ์ค‘๊ทผ, Java.collection.HashSetApp2$User@17946
kahn, ์นธ ์ผ€์‹, Java.collection.HashSetApp2$User@321cfb
ryu, ๋ฅ˜์Šน๋ฃก, Java.collection.HashSetApp2$User@1bb2d
shin, ์‹ ๋ผ, Java.collection.HashSetApp2$User@35da59
kim, ๊น€์œ ์‹ , Java.collection.HashSetApp2$User@19eee
*/

 

 

๐Ÿ”น List

  • List<E> ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค
  • ๊ฐ€๋ณ€๊ธธ์ด ๋ฐฐ์—ด์„ ํ™œ์šฉํ•ด List<E> ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ ํ–ˆ๋‹ค
  • ์ˆœ์„œ๊ฐ€ ๋ณด์žฅ๋˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ ๊ตฌํ˜„ ํด๋ž˜์Šค
  • ํŠน์ •์œ„์น˜์— ์ €์žฅํ•˜๊ธฐ , ์‚ญ์ œํ•˜๊ธฐ , ๊บผ๋‚ด๊ธฐ 
  • ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ ๊ตฌํ˜„ํด๋ž˜์Šค

๐Ÿ”น ๊ตฌํ˜„ ํด๋ž˜์Šค

  • ArrayList<E> : ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” List๊ตฌํ˜„ ํด๋ž˜์Šค(์ „์ฒด ์ž๋ฃŒ๊ตฌ์กฐ ํด๋ž˜์Šค ์ค‘์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ)
  • LinkedList<E> : ๋”๋ธ”๋งํฌ๋กœ List๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค(์š”์†Œ์˜ ์ถ”๊ฐ€/์‚ญ์ œ ์„ฑ๋Šฅ์ด ์šฐ์ˆ˜ํ•˜๋‹ค.)
  • Vector<E> : ArrayList์™€ ์œ ์‚ฌํ•˜๋Š” List๊ตฌํ˜„ ํด๋ž˜์Šค(๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์— ์•ˆ์ „ํ•˜๋‹ค.)
  • Stack<E> : LIFO(Last-In-First-Out)์œผ๋กœ ๊ตฌํ˜„๋œ List๊ตฌํ˜„ ํด๋ž˜์Šค

 

  // String๊ฐ์ฒด๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ์ €์žฅํ•˜๋Š” ArrayList๊ฐ์ฒด ์ƒ์„ฑํ•˜๊ธฐ
  List<String> list1 = new ArrayList<String>();
  
  // ์‹ค์ˆ˜๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ์ €์žฅํ•˜๋Š” ArrayList๊ฐ์ฒด ์ƒ์„ฑํ•˜๊ธฐ
  List<Double> list2 = new ArrayList<Double>();
  
  // Person๊ฐ์ฒด๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ์ €์žฅํ•˜๋Š” ArrayList๊ฐ์ฒด ์ƒ์„ฑํ•˜๊ธฐ
  List<Person> list3 = new ArrayList<Person>();

 

๐Ÿ”น ArrayList<E> ํ™œ์šฉํ•˜๊ธฐ

 

  public class Person {
    private int no;
    private String name;
    public Person() {}
    public Person(int no, String) {
      this.no = no;
      this.name = name;
    }
    public int getNo() {
      return no;
    }
    public String name() {
      return name;
    }
  }
  public static void main(String[] args) {
    // Person๊ฐ์ฒด๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ArrayList๊ฐ์ฒด ์ƒ
    List<Person> personList = new ArrayList<Person>();
    
    // ArrayList๊ฐ์ฒด์— Person๊ฐ์ฒด์ƒ์„ฑํ•˜๊ธฐ
    personList.add(new Person(100, "๊น€์œ ์‹ "));
    personList.add(new Person(200, "๊ฐ•๊ฐ์ฐฌ"));
    personList.add(new Person(300, "์ด์ˆœ์‹ "));
    personList.add(new Person(400, "๋ฅ˜๊ด€์ˆœ"));
    
    // ArrayList๊ฐ์ฒด์— ์ €์žฅ๋œ Person๊ฐ์ฒด์˜ ๊ฐฏ์ˆ˜๋ฅผ ์กฐํšŒํ•˜๊ธฐ
    int count = personList.size();
    System.out.println("์ €์žฅ๋œ ๊ฐฏ์ˆ˜: " + count); // 4๊ฐ€ ์ถœ๋ ฅ๋จ
    
    // ํ–ฅ์ƒ๋œ for๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ArrayList๊ฐ์ฒด์— ์ €์žฅ๋œ Person๊ฐ์ฒด๋ฅผ ์ˆœํšŒํ•˜๊ธฐ
    for (Person person : personList) {
      int no = person.getNo();
      String name = person.getName();
      System.out.println("๋ฒˆํ˜ธ:" + no + ", ์ด๋ฆ„:" + name);
    }
    
    // ArrayList๊ฐ์ฒด์—์„œ 0๋ฒˆ์— ์ €์žฅ๋œ Person๊ฐ์ฒด ์กฐํšŒํ•˜๊ธฐ
    Person person0 = personList.get(0);
    
    // ArrayList๊ฐ์ฒด์—์„œ 2๋ฒˆ์— ์นธ์— ์ €์žฅ๋œ Person๊ฐ์ฒด ์‚ญ์ œํ•˜๊ธฐ
    personList.remove(2)
    
    // ArrayList๊ฐ์ฒด์—์„œ 1๋ฒˆ์— ์ €์žฅ๋œ ๊ฐ์ฒด๋ฅผ ์ƒˆ๋กœ์šด Person๊ฐ์ฒด๋กœ ๋ณ€๊ฒฝํ•˜๊ธฐ
    personList.set(1, new Person(500, "์•ˆ์ค‘๊ทผ"));
    
    // ArrayList๊ฐ์ฒด์— ์ €์žฅ๋œ ๋ชจ๋“  ๊ฐ์ฒด ์‚ญ์ œํ•˜๊ธฐ
    personList.clear()
  }

๐Ÿ”น Stack<E> ๊ฐ์ฒด ์‚ฌ์šฉ

  public static void main(String[] args) {
    Stack<String> stack = new Stack<String>();
    
      // Stack๊ฐ์ฒด์— ๋ฌธ์ž์—ด ์ €์žฅํ•˜๊ธฐ
      stack.push("๊น€์œ ์‹ ");
      stack.push("๊ฐ•๊ฐ์ฐฌ");
      stack.push("์ด์ˆœ์‹ ");

      // Stack๊ฐ์ฒด์—์„œ ๋งจ ์œ„์— ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ์กฐํšŒํ•œ๋‹ค.
      String value1 = stack.peek();	// ์ด์ˆœ์‹ 
      String value2 = stack.peek();	// ์ด์ˆœ์‹ 
      String value3 = stack.peek();	// ์ด์ˆœ์‹ 
    
    // Stack๊ฐ์ฒด์—์„œ ๋งจ ์œ„์— ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ๊บผ๋‚ธ๋‹ค.(Stack์—์„œ ํ•ด๋‹น ๊ฐ์ฒด๋Š” ์‚ญ์ œ๋œ๋‹ค.)
    String value1 = stack.pop();	// ์ด์ˆœ์‹ 
      String value2 = stack.pop();	// ๊ฐ•๊ฐ์ฐฌ
      String value3 = stack.pop();	// ๊น€์œ ์‹ 
    
    // Stack๊ฐ์ฒด๊ฐ€ ๋น„์–ด์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ
    boolean result = stack.isEmpty(); // true๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค.
  }

 


void add(int index, E e)
// ์ง€์ •๋œ ์œ„์น˜์— ์š”์†Œ๋ฅผ ์ €์žฅํ•œ๋‹ค.

E get(int index)
// ์ง€์ •๋œ ์œ„์น˜์˜ ์š”์†Œ๋ฅผ ๊บผ๋‚ธ๋‹ค

E remove(int index)
// ์ง€์ •๋œ ์œ„์น˜์˜ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•œ๋‹ค.

E set(int index, E e)
// ์ง€์ •๋œ ์œ„์น˜์˜ ์š”์†Œ๋ฅผ ์ƒˆ ์š”์†Œ๋กœ ๊ต์ฒดํ•œ๋‹ค.


๐Ÿ”น Map

  • Map < K ,V > ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ Key์™€ Value์˜ ์Œ์œผ๋กœ ์—ฐ๊ฒฐ์ง€์–ด์„œ ์ €์žฅํ•˜๋Š” ๊ฐ์ฒด
  • Map <K,V> ๊ฐ์ฒด๋Š” Entry < K ,V> ๊ฐ์ฒด๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ ์ €์žฅํ•˜๋Š” ๊ฐ์ฒด๋‹ค
  • Key๋Š” ์ฃผ๋ณต์ด ๋ ์ˆ˜ ์—†์œผ๋ฉฐ ํƒ€์ž…์€ ๋Œ€์ฒด๋กœ String Integer Long ํƒ€์ž…์ด ์ฃผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค
  • Value๋Š” ์‹ค์ œ๋กœ Map์— ์ €์žฅํ•  ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฐ์ฒด๋‹ค

๐Ÿ“Œ Key์™€ Value ํ•œ์Œ์„ ํ•˜๋‚˜์˜ Entery ๋ผ๊ณ  ์นญํ•˜๋ฉฐ , ์ด Entry๋“ค์ด ์—ฌ๋Ÿฌ๊ฐœ๊ฐ€ Map์ด๋ผ ์นญํ•œ๋‹ค

 

Ex. 

Key Value Map < K ,V >
ํ•™๋ฒˆ ํ•™์ƒ ๊ฐ์ฒด Map < Integer , Student > 
์ฃผ๋ฌธ๋ฒˆํ˜ธ ์ฃผ๋ฌธ์ •๋ณด ๊ฐ์ฒด Map < Long , Order > 
์‚ฌ์šฉ์ž ์•„์ด๋”” ์‚ฌ์šฉ์ž ์ •๋ณด ๊ฐ์ฒด Map < String , User >
์ƒํ’ˆ๋ฒˆํ˜ธ ์ƒํ’ˆ์ •๋ณด ๊ฐ์ฒด Map < Integer , Product >

 

V put(K key, V value)
// Map๊ฐ์ฒด์— key, value์˜ ์Œ์„ ์ €์žฅํ•œ๋‹ค.

V get(Object key)
// Map๊ฐ์ฒด์—์„œ ์ง€์ •๋œ key์— ํ•ด๋‹นํ•˜๋Š” value๋ฅผ ์กฐํšŒํ•œ๋‹ค.

V remove(Object key)
// Map๊ฐ์ฒด์—์„œ ์ง€์ •๋œ key์— ํ•ด๋‹นํ•˜๋Š” value๋ฅผ ์‚ญ์ œํ•œ๋‹ค.

void clear()
// Map๊ฐ์ฒด์— ์ €์žฅ๋œ ๋ชจ๋“  key, value์˜ ์Œ์„ ์‚ญ์ œํ•œ๋‹ค.

int size()
// Map๊ฐ์ฒด์— ์ €์žฅ๋œ Key,value์Œ์˜ ๊ฐฏ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

boolean isEmpty()
// Map๊ฐ์ฒด๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

boolean containsKey(Object key)
// Map๊ฐ์ฒด์— ์ง€์ •๋œ key๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

boolean containsValue(Object value)
// Map๊ฐ์ฒด์— ์ง€์ •๋œ value๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

Set<Key> keySet()
// Map๊ฐ์ฒด์˜ ๋ชจ๋“  key๋ฅผ Set๊ฐ์ฒด์— ๋‹ด์•„์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

Collection<V> values()
// Map๊ฐ์ฒด์˜ ๋ชจ๋“  value๋ฅผ Collection๊ฐ์ฒด์— ๋‹ด์•„์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

Set<Map.Entry<K, V>> entrySet()
// Map์— ์ €์žฅ๋œ ๋ชจ๋“  Entry<K, V>๋ฅผ Set์— ๋‹ด์•„์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
// key : ์•„์ด๋””  , value : ๊ณ ๊ฐฑ๋ช…์˜ ์Œ์œผ๋กœ Entry๊ฐ€ ๊ตฌ์„ฑ๋˜๋Š” HashMap๊ฐ์ฒด ์ƒ์„ฑํ•˜๊ธฐ
        Map<String, String> map1 = new HashMap<String, String>();

        // Map ๊ฐ์ฒด์— ์ƒˆ๋กœ์šด Entry ์ €์žฅ
        map1.put("hong", "ํ™๊ธธ๋™");
        map1.put("kim", "๊น€์œ ์‹ ");
        map1.put("Gil", "๊ธธํƒœ๋ฏธ");
        map1.put("Chuck", "์ฒ™๊ณ„๊ด‘");

        System.out.println(map1);	// {hong=ํ™๊ธธ๋™, Chuck=์ฒ™๊ณ„๊ด‘, Gil=๊ธธํƒœ๋ฏธ, kim=๊น€์œ ์‹ }

        String value1 = map1.get("Gil");	
        String value2 = map1.get("chuck");	
        String value3 = map1.get("Han");	

        System.out.println(value1);	// ๊ธธํƒœ๋ฏธ
        System.out.println(value2);	// null - ๋Œ€์†Œ๋ฌธ์ž ํ‹€๋ฆผ
        System.out.println(value3);	// null - ์กด์žฌํ•˜์ง€ ์•Š์Œ
        
                // map ๊ฐ์ฒด์—์„œ ์ง€์ •๋œ key์— ํ•ด๋‹นํ•˜๋Š” Entry ์‚ญ์ œ
        map1.remove("hong");

        // ์ด๋ฏธ ์กด์žฌํ•˜๋Š” key๋กœ ๋‹ค์‹œ ๊ฐ’์„ ์ €์žฅํ•˜๋ฉด ๊ธฐ์กด ๊ฐ’์„ ๋ฎ์–ด์“ด๋‹ค
        map1.put("kim", "๊น€์ขŒ์ง„");

        System.out.println(map1);   // {Chuck=์ฒ™๊ณ„๊ด‘, Gil=๊ธธํƒœ๋ฏธ, kim=๊น€์ขŒ์ง„}

 

๐Ÿ”น Map ๊ฐ์ฒด

Map<string,Object> map = new HashMap<>()

ํ•ด๋‹น ์ฝ”๋“œ ๊ฐ™์€ ํ˜•ํƒœ๋กœ Map๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋ฉด key , value์˜ ์Œ์œผ๋กœ ๋‹ค์–‘ํ•œ ํƒ€์ž…์˜ ๊ฐ’์„ ์ €์žฅ ํ•  ์ˆ˜ ์žˆ๋‹ค

๐Ÿ“Œ ์ฒ˜๋ฆฌํ•  ์ •๋ณด์˜ ํ˜•ํƒœ๊ฐ€ ๋‹ค์˜ํ•œ ๊ฒฝ์šฐ (๊ฐ’์˜ ๊ฐฏ์ˆ˜ , ๊ฐ’์˜ ์ข…๋ฅ˜๊ฐ€ ๋‹ค์–‘ํ•˜๋Š” ๋ณ€ํ•˜๋Š” ์ •๋ณด ๋‹ค๋ฃฐ ๋•Œ)

if๋ฌธ์˜ ์กฐ๊ฑด์‹์—์„œ ๋น„๊ต ๊ฐ’์œผ๋กœ ์ด์šฉ๋˜๋Š” ๊ฐ’์„ Map ๊ฐ์ฒด์˜ key๋กœ ํ™œ์šฉใ„นํ•˜๋ฉด if๋ฌธ์„ ์ œ๊ฑฐ ํ•  ์ˆ˜ ์žˆ๋‹ค

Map < String , Double > map = new HashMap<>();
map.put("gold" , 0.05);
map.put("silver" , 0.02);
map.put("bronze" , 0.01);

// ๊ณ ๊ฐ๋“ฑ๊ธ‰๊ณผ ๊ตฌ๋งค๊ธˆ์•ก์„ ์ „๋‹ฌ๋ฐ›์•„ ํฌ์ธํŠธ ๋ฐ˜ํ™˜
// ํฌ์ธํŠธ ์ ๋ฆฝ๋ฅ ์€ ๊ณ ๊ฐ๋“ฑ๊ธ‰์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง€๋Š”๋ฐ Map์— ํฌ์ธํŠธ ๋“ฑ๊ธ‰์„ key , ์ ๋ฆฝ๋ฅ ์„ value๋กœ ์ €์žฅ
// ๋งต์—์„œ ๊ณ ๊ฐ๋“ฑ๊ธ‰์˜ key๋กœ ์ ๋ฆฝ๋ฅ ์„ ์กฐํšŒํ• ์ˆ˜ ์žˆ๋‹ค


public int getPoint(String grade, int orderPrice) {
double pointDepositRate = map = get(grade);
return (int) (orderPrice * pointDepositRate);
	}
    
// ์—ฌ๋Ÿฌ๊ฐœ์˜ ๊ฐ™์€ ๊ฐ’์„ ๊ฐ€์ง„ ๊ฒƒ๋“ค ๋ผ๋ฆฌ ๋ถ„๋ฅ˜
// Ex)
// ๊ณ ๊ฐ์ด๋ฆ„์—์„œ ์„ฑ์”จ๋ณ„ ์ธ์›์ˆ˜ ๊ณ„์‚ฐํ•˜๊ธฐ
// ์ƒํ’ˆ๋ชฉ๋ก์—์„œ ์ƒํ’ˆ ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„๋กœ ์ƒํ’ˆ ์ˆ˜ ๊ณ„์‚ฐ
// ์—ฐ์„ค๋ฌธ์—์„œ ๋‹จ์–ด๋ณ„ ๋นˆ๋„์ˆ˜ ๊ณ„์‚ฐํ•˜๊ธฐ

 

 // VO ๊ฐ์ฒด๋ฅผ ๋Œ€์ฒดํ•˜๋Š” Map๊ฐ์ฒด

   //     ์ƒํ’ˆ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๋Š” Product ํด๋ž˜์Šค
  /*      public class Product{
            private int no;
            private String name;
            private String maker;
            private int price;
            private double discountRate;
        }

        Product item1 = new Product(100,"galaxy22","samsung",990000,0.05);
        Product item2 = new Product(100,"galaxy23","samsung",990000,0.05);
        Product item3 = new Product(100,"galaxy24","samsung",990000,0.05);
        Product item4 = new Product(100,"galaxy25","samsung",990000,0.05);


     //   ์ƒํ’ˆ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๋Š” Map ๊ฐ์ฒด
        Map<String,Object> item = new HashMap<>();
        item.put("no",100);
        item.put("name","galaxy22");
        item.put("maker","samsung");
        item.put("price",990000);
        item.put("discountRate",0.05);
        
        */
        
            Map<String, Object> item2 = new HashMap<String, Object>();
        item2.put("no", 101);
        item2.put("name", "galaxt22");
        item2.put("maker", "samsung");
        item2.put("price", 900000);
        item2.put("discountRate", 0.05);

        // ์ƒํ’ˆ์ •๋ณด๊ฐ€ ์ €์žฅ๋œ Map๊ฐ์ฒด๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ ์ €์žฅํ•˜๋Š” List๊ฐ์ฒด ์ƒ์„ฑ
        List<Map<String, Object>> items = new ArrayList<>();
        items.add(item1);
        items.add(item2);
        
        // List ๊ฐ์ฒด์— ๋ชจ๋“  ์ •๋ณด ์ถ”๋ ฅ
            for (Map<String, Object> map : items) {
            int no = (Integer) map.get("no");
            String name = (String) map.get("name");
            String maker = (String) map.get("maker");
            int price = (Integer) map.get("price");
            double discountRate = (double) map.get("discountRate");

            System.out.println("๋ฒˆํ˜ธ : " + no);
            System.out.println("์ด๋ฆ„ : " + name);
            System.out.println("์ œ์กฐ์‚ฌ : " + maker);
            System.out.println("๊ฐ€๊ฒฉ : " + price);
            System.out.println("ํ• ์ธ์œจ : " + discountRate);
            System.out.println();
            System.out.println();

        }

    public static void main(String[] args) {
        Map<String, MessageSender> map = new HashMap<String, HashMapApp4.MessageSender>();

        map.put("sms", new SmsMessageSender());
        map.put("Email", new EmailMessageSender());
        map.put("kakao", new KakaotalkMessageSender());

        User user1 = new User();
        user1.name = "ํ™๊ธธ๋™";
        user1.senderTypes = new String[]{"sms", "kakao"};
        System.out.println("###" + user1.name + "์—๊ฒŒ ์ด๋ฒคํŠธ ๋ฉ”์‹œ์ง€ ์ „์†กํ•˜๊ธฐ");

        for (String type : user1.senderTypes) {   // type์—๋Š” "sms", "kakao"๊ฐ€ ์ „๋‹ฌํ•œ๋‹ค
            MessageSender ms = map.get(type);
            ms.send();
        }
    }


    static class User {
        String name;         // ๊ณ ๊ฐ๋ช…
        String[] senderTypes;   // ๊ณ ๊ฐ์ด ์ˆ˜์‹ ๋™์•ˆ ๋ฉ”์‹œ์ง€ ์ˆ˜์‹ ๋ฐฉ๋ฒ•๋“ค
    }

    static interface MessageSender {
        void send();
    }

    static class SmsMessageSender implements MessageSender {
        public void send() {
            System.out.println("SMS๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ธ๋‹ค.");
        }
    }

    static class EmailMessageSender implements MessageSender {
        public void send() {
            System.out.println("Email๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ธ๋‹ค");
        }
    }

    static class KakaotalkMessageSender implements MessageSender {
        public void send() {
            System.out.println("์นด์นด์˜คํ†ก์œผ๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ธ๋‹ค");
        }
    }

Map < String , Double > map = new HashMap<>();
map.put("gold" , 0.05);
map.put("silver" , 0.02);
map.put("bronze" , 0.01);

// ๊ณ ๊ฐ๋“ฑ๊ธ‰๊ณผ ๊ตฌ๋งค๊ธˆ์•ก์„ ์ „๋‹ฌ๋ฐ›์•„ ํฌ์ธํŠธ ๋ฐ˜ํ™˜
// ํฌ์ธํŠธ ์ ๋ฆฝ๋ฅ ์€ ๊ณ ๊ฐ๋“ฑ๊ธ‰์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง€๋Š”๋ฐ Map์— ํฌ์ธํŠธ ๋“ฑ๊ธ‰์„ key , ์ ๋ฆฝ๋ฅ ์„ value๋กœ ์ €์žฅ
// ๋งต์—์„œ ๊ณ ๊ฐ๋“ฑ๊ธ‰์˜ key๋กœ ์ ๋ฆฝ๋ฅ ์„ ์กฐํšŒํ• ์ˆ˜ ์žˆ๋‹ค


public int getPoint(String grade, int orderPrice) {
double pointDepositRate = map = get(grade);
return (int) (orderPrice * pointDepositRate);
	}
    
// ์—ฌ๋Ÿฌ๊ฐœ์˜ ๊ฐ™์€ ๊ฐ’์„ ๊ฐ€์ง„ ๊ฒƒ๋“ค ๋ผ๋ฆฌ ๋ถ„๋ฅ˜
// ๊ณ ๊ฐ์ด๋ฆ„์—์„œ ์„ฑ์”จ๋ณ„ ์ธ์›์ˆ˜ ๊ณ„์‚ฐํ•˜๊ธฐ

๐Ÿ”น Stack ํด๋ž˜์Šค

  •  last-in-first-out (LIFO) ์ž๋ฃŒ๊ตฌ์กฐ ๊ตฌํ˜„ ํด๋ž˜์Šค
  • ํ›„์ž…์„ ์ถœ : ๊ฐ€์žฅ ๋‚˜์ค‘์— ์ถ”๊ฐ€ํ•œ ๊ฐ’์ด ์ œ์ผ ๋จผ์ € ์ธ์ถœ๋œ๋‹ค.
  • Stack์€ List ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„์ฒด๋‹ค
  • Stack ๊ตฌํ˜„ํด๋ž˜์Šค์˜ ๊ณ ์œ ํ•œ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” Stack ํƒ€์ž…์˜ ์ฐธ์กฐ๋ณ€์ˆ˜์— ๊ฐ์ฒด๋ฅผ ๋Œ€์ž…ํ•ด์•ผ ํ•œ๋‹ค
    E push(E item)
//        ์ง€์ •๋œ ๊ฐ์ฒด๋ฅผ ์Šคํƒ์˜ ๋งจ ์œ„์— ์ถ”๊ฐ€ํ•œ๋‹ค

   E pop()
//   ์Šคํƒ์˜ ๋งจ ์œ„์— ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ์‚ญ์ œํ•˜๊ณ , ๊ทธ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค

    E peek()
//      ์Šคํƒ์˜ ๋งจ ์œ„์— ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ์‚ญ์ œ์—†์ด ๋ฐ˜ํ™˜ํ•œ๋‹ค.
 Stack<String> stack = new Stack<String>();

        stack.push("ํ™๊ธธ๋™");
        stack.push("๊ณ ๊ธธ๋™");
        stack.push("๊น€์œ ์‹ ");
        stack.push("๊น€์‹œ๋ฏผ");

        System.out.println(stack);	// [ํ™๊ธธ๋™, ๊ณ ๊ธธ๋™, ๊น€์œ ์‹ , ๊น€์‹œ๋ฏผ]

        String name2 = stack.pop();
        System.out.println("์ธ์ถœํ•  ๊ฐ์ฒด : " + name2);	// ์ธ์ถœํ•  ๊ฐ์ฒด : ๊น€์‹œ๋ฏผ
        System.out.println("stack ํ™•์ธ : " + stack);	// stack ํ™•์ธ : [ํ™๊ธธ๋™, ๊ณ ๊ธธ๋™, ๊น€์œ ์‹ ]
728x90

'Language > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Java I / O  (0) 2023.11.23
Java Exception  (4) 2023.11.22
Java Iterator , Score CRUD  (1) 2023.11.17
Java Random , Math  (0) 2023.11.16
Java Wrapper , Date , Calendar , StringBuilder , Buffer  (6) 2023.11.15