2025๋ 3์ 11์ผ
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 ์ถ๋ ฅ (์ค๋ณต ์ ๊ฑฐ๋จ)
}
}
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 ์ถ๋ ฅ
}
}
HashMap
์ ํ์ฉํ ํค-๊ฐ ์ ์ฅ ๋ฐ ์กฐํ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 ์ถ๋ ฅ
}
}
Properties
ํ์ผ์ ํ์ฉํ ์ค์ ๊ฐ ๊ด๋ฆฌapplication.properties
)์ Java์์ ์ฝ์ด์ฌ ์ ์์ application.properties
ํ์ผ ์์ url=jdbc:mysql://localhost:3306/shopdb
username=root
password=1234
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);
}
}
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 | ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ ์ฒ๋ฆฌ |