Java BasicMap

4147 ๋‹จ์–ด
Set๊ณผ Array List์— ์ด์–ด ์˜ค๋Š˜ ๋ฐฐ์šด ๊ฒƒ์€ ๋งต์œผ๋กœ, ์ด์ „ ๋‚ด์šฉ๊ณผ ์—ฐ๊ด€์ด ์žˆ๊ณ  ๋‹ค๋ฅด๋‹ค.Map๊ณผ Set์˜ ์—ฐ๊ฒฐ์„ฑ์ด ๋น„๊ต์  ํฌ๋‹ค.๊ตฌํ˜„ ํด๋ž˜์Šค:HashMap
์ •์˜ ๋ฐฉ๋ฒ•: Map
Map student = new HashMap<>();

์„ธ ๊ฐœ์˜ ๊ตฌ์„ฑ ์›์†Œ๋กœ
  • Key
  • Value
  • Entry(๋˜ ๋‹ค๋ฅธ ์ง‘ํ•ฉ ์œ ํ˜•, ์ƒ๊ธฐ ๋‘ ๋ถ€๋ถ„์„ ํฌ๊ด„): ์ •์˜ ๋ฐฉ๋ฒ•Entry
  • ์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•ด์•ผ ํ•  ๊ฒƒ์€ Map๊ณผ ์ด์ „์˜ Set๊ณผ Array List๋Š” ๋‹ค๋ฅด๋‹ค. ์ด ๋‘ ๊ฐ€์ง€๋Š” ๋Œ€์ƒ์„ ์ €์žฅํ•˜๋Š” ์šฉ๊ธฐ์— ํ•ด๋‹นํ•˜๊ณ  ๋Œ€์ƒ์„ ๋งŽ์ด ๊ณฑํ•˜๋ฉด ํ•˜๋‚˜์˜ ์ง‘ํ•ฉ์ธ Set์ด๋‚˜ Array List๊ฐ€ ๋œ๋‹ค.Map์€ ์ด ๋‘ ๊ฐœ๋ณด๋‹ค ํ•œ ์ธต ๋” ๋†’์•„์„œ ๋‘ ๋Œ€์ƒ์˜ ๋Œ€์‘ ๊ด€๊ณ„, ์ฆ‰ Key์™€ Value๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ด ๋‘ ๋ถ€๋ถ„์ด ๊ฐ๊ฐ ์Œ“์ธ ํ›„์— ์„ž์ผ ์ˆ˜ ์—†๋‹ค. ๊ฐ์ž์˜ ์šฉ๊ธฐ๊ฐ€ ์žˆ์–ด์•ผ Key๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ์šฉ๊ธฐ๋Š” Set์ด๊ณ  Value๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ์šฉ๊ธฐ๋Š” Collection์ด๊ธฐ ๋•Œ๋ฌธ์— Map์€ ํ•˜๋‚˜์˜ Set๊ณผ ํ•˜๋‚˜์˜ CollectionKey์™€ Value๊ฐ€ ์žˆ๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค.๊ทธ๋Ÿฌ๋ฉด ๋‹น์—ฐํžˆ ์กฐ์ž‘ ๊ณผ์ •์—์„œ ์ƒ์‘ํ•˜๋Š” Set(keyset ์‚ฌ์šฉ)๊ณผCollection(values ์‚ฌ์šฉ))์„ ๋˜๋Œ๋ ค์ฃผ๊ณ  ๊ฐ์ž ์ง‘ํ•ฉ ๋‚ด๋ถ€์— ๋Œ€์‘ํ•˜๋Š” ๋Œ€์ƒ์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.๊ทธ ๋ฐ–์— Map์—๋Š” ๋˜ ํ•˜๋‚˜์˜ ์—”ํŠธ๋ฆฌ ์ง‘ํ•ฉ์ด ์žˆ๋Š”๋ฐ ํ‚ค์™€ ๋ฐธ๋ฅ˜, ํ‚ค์™€ ๋ฐธ๋ฅ˜๊ฐ€ ์Œ์„ ์ด๋ฃจ๋Š” ๊ฒƒ์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜๋‚˜์˜ ์กฐํ•ฉ ํ˜•์‹์œผ๋กœ ๋ณด๋ฉด ํ•˜๋‚˜์˜ ์—”ํŠธ๋ฆฌ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์—”ํŠธ๋ฆฌ๊ฐ€ ๋งŽ์ด ์Œ“์ด๊ณ  ํ•˜๋‚˜์˜ Set์ด ๋œ๋‹ค.
    Method: EntrySet(), KeySet(), Values()๋Š” ๊ฐœ๋ณ„ ์ปฌ๋ ‰์…˜์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
    public class MapTest {
        private Map student;
        
        //initiate Student in constructor 
        
        public MapTest(){
            student = new HashMap<>();
        }
        
        
            
        public void testKeyset(){
    // Set
            Set kSet;
            kSet = student.keySet();
    //Set Key ๏ผŒ String 
            for(String stID: kSet){
                System.out.println("Student:" + stID);
            }
        }
        
        
        public void testEntryset(){
    // Entry 
            Set> eSet = student.entrySet();
            for(Entry e:eSet){
                System.out.print("ID is: " + e.getKey());
                System.out.println("Name is: " + e.getValue().getName());
                        }
        }
    
    

    ์ฆ๊ฐ€, ์‚ญ์ œ, ๋ณ€๊ฒฝ, ์กฐ์‚ฌ:put() ์ฆ๊ฐ€, ์ฆ๊ฐ€ ๊ณผ์ •์—์„œ get() ๋ฐฉ๋ฒ•๊ณผ ํ•จ๊ป˜ ๊ฐ™์€ ๋Œ€์ƒ์ด ์›๋ž˜ ์ง‘ํ•ฉ์— ์ด๋ฏธ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ:student.get (ID) ์€ ๋งต ํ˜•์‹ ์ง‘ํ•ฉ์— ์ƒˆ๋กœ์šด ์š”์†Œ๋กœ ๋“ค์–ด๊ฐ€๋Š” ํŒ๋‹จ ์กฐ๊ฑด์ž…๋‹ˆ๋‹ค. ๋งต ์•„๋ž˜์˜ Set ํด๋ž˜์Šค๋Š” ๊ฐ™์€ ์š”์†Œ๊ฐ€ ์žˆ์„ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
        public void testPut(){
            Scanner console = new Scanner(System.in);
            String ID;
            Student st;
            Student newStudent;
            String name;
            
            int i=0;
            while(i<3){
                System.out.println("Input student ID: ");
                ID = console.next();
                st = student.get(ID);
    
    // get ๏ผŒ value.         
                if(st == null){
                System.out.println("Input student name: ");
                name = console.next();
                newStudent = new Student(ID, name);
    
    // put ๏ผŒ get 
                
                student.put(ID, newStudent);
                System.out.println("Student added: " + student.get(ID).getName());
                System.out.println();
                i++;
                }
    // 
                else{
                    System.out.println("The ID's been taken");
                    continue;
                }
                
            }
    

    ์‚ญ์ œ๋Š”remove๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  get()์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฏธ๋ฆฌ ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค
    public void testRemove(){
            
            Scanner console = new Scanner(System.in);
            String ID;
            Student st;
            Student newStudent;
            String name;
            
            while(true){
                System.out.println("Input the name to be removed: ");
                ID = console.next();
                st = student.get(ID);
                if(st==null) {
                    System.out.println("The input ID doesn't exist!" );
                    continue;
                }
                else{
                student.remove(ID);
                break;
                }
                
            }
        }
    
    
    
    

    ์ƒ๊ธฐ 2๋ก€์—์„œ ์ˆœํ™˜์˜ ์‚ฌ์šฉ์„ ๋™์‹œ์— ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค. ๋งŒ์•ฝ์— ์ˆœํ™˜ํ•˜๋Š” ๊ณผ์ •์—์„œ ์กฐ๊ฑด ํŒ๋‹จ ํ›„์˜ ์ง‘ํ–‰์ด ์ˆœํ™˜ ๊ณ„์ˆ˜์™€ ์ˆœํ™˜ ์ œ์–ด์— ์˜ํ–ฅ์„ ์ค„ ์ˆ˜ ์žˆ์œผ๋ฉดwhile๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.foreach๋ณด๋‹ค ์ข€ ๋” ์œ ์—ฐํ•˜๋‹ค.
    int i=0;
    while(i<3){
    if(...){
    ...
    i++;
    }
    else{
    }
    
    elsei 
    
    
    

    ๋˜ํ•œ:
    while(true){
               if(... ){  ...
      
                    continue;
                }
                else{...
    
                break;
    }
    
    

    ์ˆœํ™˜์— ๋Œ€ํ•œ ์ œ์–ด: ์‚ฌ์ˆœํ™˜ +while (true) + continue, ๊ทธ๋ฆฌ๊ณ  ์ˆœํ™˜ ์ข…๊ฒฐ:break.๊ฐ€์žฅ ์ข‹์€while ์ˆœํ™˜์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
    ๋ณ€๊ฒฝ, ์—ฌ์ „ํžˆput์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ™์€ ID๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ์ง์ ‘ ์ˆ˜์ •
    String name = scanner.next();
                st = new Student(ID, name);
                student.put(ID, st);
    

    ์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ