๐Ÿ“Œ Java ๊ฐ์ฒด ๋น„๊ต, ์ปฌ๋ ‰์…˜ ํ”„๋ ˆ์ž„์›Œํฌ, Properties ์„ค์ •, ๊ทธ๋ฆฌ๊ณ  GUI(Swing) ํ™œ์šฉ

My Pale Blue Dotยท2025๋…„ 3์›” 11์ผ
0

JAVA

๋ชฉ๋ก ๋ณด๊ธฐ
19/35
post-thumbnail

๐Ÿ“… ๋‚ ์งœ

2025๋…„ 3์›” 11์ผ

๐Ÿ“ ํ•™์Šต ๋‚ด์šฉ

โœ… 1. ๊ฐ์ฒด ๋น„๊ต - equals() & hashCode()

๐Ÿ“Œ ๊ฐœ๋…

  • == ์—ฐ์‚ฐ์ž๋Š” ๊ฐ์ฒด์˜ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ ๋น„๊ตํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๊ฐ’์ด ๊ฐ™์•„๋„ ๋‹ค๋ฅธ ๊ฐ์ฒด๋กœ ์ธ์‹๋จ.
  • equals()๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•˜๋ฉด ๊ฐ์ฒด์˜ ํŠน์ • ์†์„ฑ์ด ๊ฐ™์œผ๋ฉด ๋™์ผ ๊ฐ์ฒด๋กœ ์ธ์‹ ๊ฐ€๋Šฅ.
  • hashCode()๋„ ํ•จ๊ป˜ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•˜๋ฉด ์ปฌ๋ ‰์…˜(Set, Map)์—์„œ ์ค‘๋ณต์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Œ.

๐Ÿ“Œ ์˜ˆ์ œ ์ฝ”๋“œ (Person ํด๋ž˜์Šค)

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

class Person {
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object obj) {  
        if (obj instanceof Person) {
            Person other = (Person) obj;
            return this.name.equals(other.name) && this.age == other.age;
        }
        return false;
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}

public class EqualsHashCodeExample {
    public static void main(String[] args) {
        Set<Person> set = new HashSet<>();
        
        Person p1 = new Person("ํ™๊ธธ๋™", 30);
        Person p2 = new Person("ํ™๊ธธ๋™", 30);
        
        set.add(p1);
        set.add(p2);
        
        System.out.println("Set ํฌ๊ธฐ: " + set.size());  // 1 ์ถœ๋ ฅ (์ค‘๋ณต ์ œ๊ฑฐ๋จ)
    }
}

โœ… 2. HashSet์„ ํ™œ์šฉํ•œ ์ค‘๋ณต ์ œ๊ฑฐ

๐Ÿ“Œ ๊ฐœ๋…

  • HashSet์€ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š” ์ž๋ฃŒ๊ตฌ์กฐ
  • ๋‚ด๋ถ€์ ์œผ๋กœ hashCode()์™€ equals()๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ค‘๋ณต์„ ๋ฐฉ์ง€

๐Ÿ“Œ ์˜ˆ์ œ ์ฝ”๋“œ

import java.util.HashSet;
import java.util.Set;

public class HashSetExample {
    public static void main(String[] args) {
        Set<String> set = new HashSet<>();
        set.add("Java");
        set.add("Python");
        set.add("Java");  // ์ค‘๋ณต๋œ ๊ฐ’ -> ์ €์žฅ๋˜์ง€ ์•Š์Œ

        System.out.println("Set ํฌ๊ธฐ: " + set.size());  // 2 ์ถœ๋ ฅ
    }
}

โœ… 3. HashMap์„ ํ™œ์šฉํ•œ ํ‚ค-๊ฐ’ ์ €์žฅ ๋ฐ ์กฐํšŒ

๐Ÿ“Œ ๊ฐœ๋…

  • ํ‚ค-๊ฐ’(Key-Value) ํ˜•ํƒœ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ
  • ํ‚ค๋Š” ์ค‘๋ณต๋  ์ˆ˜ ์—†์œผ๋ฉฐ, ๊ธฐ์กด ํ‚ค์— ์ƒˆ ๊ฐ’์„ ๋„ฃ์œผ๋ฉด ๊ธฐ์กด ๊ฐ’์ด ๋ฎ์–ด์“ฐ๊ธฐ๋จ

๐Ÿ“Œ ์˜ˆ์ œ ์ฝ”๋“œ

import java.util.HashMap;
import java.util.Map;

public class HashMapExample {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();

        // ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€
        map.put("Java", 90);
        map.put("Python", 85);
        map.put("Java", 100); // ๊ธฐ์กด ๊ฐ’ ๋ฎ์–ด์“ฐ๊ธฐ

        // ๋ฐ์ดํ„ฐ ์กฐํšŒ
        System.out.println("Java ์ ์ˆ˜: " + map.get("Java")); // 100

        // ๋ฐ์ดํ„ฐ ์‚ญ์ œ
        map.remove("Python");
        System.out.println("Map ํฌ๊ธฐ: " + map.size());  // 1 ์ถœ๋ ฅ
    }
}

โœ… 4. Properties ํŒŒ์ผ์„ ํ™œ์šฉํ•œ ์„ค์ •๊ฐ’ ๊ด€๋ฆฌ

๐Ÿ“Œ ๊ฐœ๋…

  • ์™ธ๋ถ€ ์„ค์ •ํŒŒ์ผ (application.properties)์„ Java์—์„œ ์ฝ์–ด์˜ฌ ์ˆ˜ ์žˆ์Œ
  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค URL, ์‚ฌ์šฉ์ž๋ช…, ๋น„๋ฐ€๋ฒˆํ˜ธ ๋“ฑ์˜ ์„ค์ • ์ €์žฅ ๊ฐ€๋Šฅ

๐Ÿ“Œ application.properties ํŒŒ์ผ ์˜ˆ์ œ

url=jdbc:mysql://localhost:3306/shopdb
username=root
password=1234

๐Ÿ“Œ Java์—์„œ Properties ํŒŒ์ผ ์ฝ๊ธฐ

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class PropertiesExample {
    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();
        FileInputStream fin = new FileInputStream("application.properties");
        properties.load(fin);

        String url = properties.getProperty("url");
        System.out.println("DB URL: " + url);
    }
}

โœ… 5. Java Swing๊ณผ JFrame์„ ํ™œ์šฉํ•œ GUI ๊ฐœ๋ฐœ

๐Ÿ“Œ ๊ฐœ๋…

  • JFrame์„ ์‚ฌ์šฉํ•ด ์œˆ๋„์šฐ ์ฐฝ์„ ์ƒ์„ฑํ•˜๊ณ  ๋ฒ„ํŠผ ์ถ”๊ฐ€ ๊ฐ€๋Šฅ
  • ActionListener๋ฅผ ์‚ฌ์šฉํ•ด ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ

๐Ÿ“Œ ๊ธฐ๋ณธ ์ฐฝ ์ƒ์„ฑ

import javax.swing.JFrame;

public class SwingExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Swing ์˜ˆ์ œ");
        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

๐Ÿ“Œ ๋ฒ„ํŠผ ์ถ”๊ฐ€ ๋ฐ ํด๋ฆญ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("๋ฒ„ํŠผ ํด๋ฆญ ์˜ˆ์ œ");
        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        JButton button = new JButton("ํด๋ฆญํ•˜์„ธ์š”!");
        JLabel label = new JLabel("ํด๋ฆญ ์ „");

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                label.setText("๋ฒ„ํŠผ์ด ํด๋ฆญ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!");
            }
        });

        frame.setLayout(new java.awt.FlowLayout());
        frame.add(button);
        frame.add(label);

        frame.setVisible(true);
    }
}

๐Ÿ”ฅ ์ •๋ฆฌ

๊ฐœ๋…์„ค๋ช…
equals() & hashCode()๊ฐ์ฒด ๋น„๊ต ๋ฐ HashSet ์ค‘๋ณต ์ œ๊ฑฐ
HashSet์ค‘๋ณต ์—†๋Š” ๋ฐ์ดํ„ฐ ์ €์žฅ
HashMapํ‚ค-๊ฐ’ ๊ธฐ๋ฐ˜ ๋ฐ์ดํ„ฐ ์ €์žฅ ๋ฐ ์กฐํšŒ
Properties ํด๋ž˜์Šค.properties ์„ค์ • ํŒŒ์ผ ๋กœ๋“œ ๋ฐ ํ™œ์šฉ
JFrame (Swing)GUI ์ฐฝ ์ƒ์„ฑ ๋ฐ ๋ฒ„ํŠผ ์ถ”๊ฐ€
ActionListener๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ

๐Ÿ”— ์ฐธ๊ณ  ์ž๋ฃŒ


profile
Here, My Pale Blue.๐ŸŒ

0๊ฐœ์˜ ๋Œ“๊ธ€