[Day26] Java 26 [9/15]
HashMap๊ณผ Hashtable
1) Map ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ์ปฌ๋ ์ ํด๋์ค
2) key + value ํ์์ผ๋ก ์ ์ฅ/์ฝ๊ธฐ == ์ํธ๋ฆฌ( Entry )
3) Hash : [ํด์ฑ ๊ธฐ๋ฒ]์ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ [๋ง์ ์์ ๋ฐ์ดํฐ]๋ฅผ [๊ฒ์]ํ ๋ ์ฑ๋ฅ์ ๋ฐ์ด๋๋ค.
4) Hashtable ( ๊ตฌ ) < ๊ถ์ฅ HashMap( ์ )
๋๊ธฐํ O X
์ค๋ ๋ ์์ X
Vector ArrayList
5) key ์ค๋ณต X
value ์ค๋ณต O
HashMap<String, String> hm = new HashMap<>();
hm.put("kenik", "์ด์ฐฝ์ต");
hm.put("hong", "ํ๊ธธ๋");
hm.put("hong", "ํ๊ธฐ์"); // ์๋ก ์ถ๊ฐ๋ ์ํธ๋ฆฌ๋ก ์ฒ๋ฆฌ.
System.out.println( hm );
HashMap<String, Student > hm = new HashMap< >();
hm.put( "ss5001" , new Student("ss5001", "ํ๊ธธ๋" )); // X
hm.put( "ss5001" , new Student("ss5001", "์ด์ฌ๋ฏผ" ));
// null ํค๋ก ์ฌ์ฉํ ์ ์๋ค + 1๋ฒ ์ฌ์ฉ ( ์ค๋ณต ํ์ฉ X )
hm.put( null, new Student(null, "ํ์ ์ฐ" )); // X
hm.put( null, new Student(null, "์ํํ" ));
// value ์ค๋ณต ํ์ฉํ๋ค.
hm.put( "ss5002" , new Student("ss5001", "์ด์ฌ๋ฏผ" )); // X
// System.out.println( hm );
// ํ๋ฒ( key ) ์ด "ss5003" ํ์ ์กด์ฌ ์ ๋ฌด ํ์ธ
boolean isExist = hm.containsKey( "ss5001" );
System.out.println( isExist ); // true
// hm.containsValue(hm)
if( hm.containsKey( "ss5001" ) ) {
// value = Student ๊ฐ์ฒด ์ป์ด์ ์ถ๋ ฅ.
// key๋ฅผ ์๋ value ์ด๋ป๊ฒ ์ป์ด์ค๋?
Student v = hm.get( "ss5001" );
System.out.println( v );
}
// 1. ๋ชจ๋ key ๊ฐ์ ์ป์ด์์ ์ถ๋ ฅ - keySet() ๋ฉ์๋ ( ์ํ )
Set<String> ks = hm.keySet();
Iterator<String> ir = ks.iterator();
while (ir.hasNext()) {
String key = ir.next();
Student v = hm.get(key);
System.out.printf(" key=%s - value=%s \n", key, v );
}
// 2. ๋ชจ๋ value ๊ฐ์ ์ป์ด์์ ์ถ๋ ฅ
Collection<Student> vc = hm.values();
Iterator<Student> ir2 = vc.iterator();
while (ir2.hasNext()) {
Student s = ir2.next();
System.out.println( s );
}
// 3. ๋ชจ๋ key- value ์ถ๋ ฅ ์ํธ๋ฆฌ(entry)(k+v) ( ์ํ )
Set<Entry<String, Student>> es = hm.entrySet();
Iterator<Entry<String, Student>> ir3 = es.iterator();
while (ir3.hasNext()) {
Entry<String, Student> entry = ir3.next();
String key = entry.getKey();
Student value = entry.getValue();
System.out.printf(" key=%s - value=%s \n", key, value );
}
LinkedHashMap<String, ArrayList<Member>> ht = new LinkedHashMap<>();
// ํ๋ช
์ ์ ์ฅํ๊ณ ์๋ ๋ฐฐ์ดX, ArrayList
ArrayList<String> tnList = new ArrayList<>();
tnList.add("์๋ฐ++"); // tnList.get(0)
tnList.add("๋ชฉ์จ๊ฑธ๊ณ ");
tnList.add("๊ฐ๋ณด์๊ณ ");
tnList.add("Dev");
ArrayList<Member> team1 = new ArrayList<>();
team1.add(new Member("์ด์ฌ๋ฏผ", 90));
team1.add(new Member("๊ฐ์์", 70));
team1.add(new Member("๊น์งํ", 80));
ArrayList<Member> team2 = new ArrayList<>();
team2.add(new Member("๊ถ์ฌํ", 88));
team2.add(new Member("๋
ธ์ฉ์ค", 56));
team2.add(new Member("๋ฐํ์", 80));
team2.add(new Member("์ํํ", 80));
ArrayList<Member> team3 = new ArrayList<>();
team3.add(new Member("๊น๊ฐ์จ", 90));
team3.add(new Member("๋ฌธํ๋น", 70));
ht.put( tnList.get(0) , team1 );
ht.put( tnList.get(1) , team2 );
ht.put( tnList.get(2) , team3 );
Set<Entry<String, ArrayList<Member>>> es= ht.entrySet();
Iterator<Entry<String, ArrayList<Member>>> ir = es.iterator();
int no = 1;
while (ir.hasNext()) {
Entry<String, ArrayList<Member>> entry = ir.next();
String teamName = entry.getKey();
ArrayList<Member> mList = entry.getValue();
System.out.printf("%d์กฐ - %s ( %d๋ช
)\n", no++, teamName, mList.size());
//
Iterator<Member> ir2 = mList.iterator();
int seq = 1;
while (ir2.hasNext()) {
Member m = ir2.next();
System.out.printf( "\t\t %d๋ฒ - %s\n", seq++, m );
} // while
} // while
class Member{
String name;
int score;
public Member(String name, int score) {
super();
this.name = name;
this.score = score;
}
@Override
public String toString() {
return "Member [name=" + name + ", score=" + score + "]";
}
}
ํด์ฑ
1) ํด์ฑ ? ํด์ ํจ์๋ฅผ ์ด์ฉํด์ (ํด์ํ ์ด๋ธ์) ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ณ ๊ฒ์ํ๋ ๊ธฐ๋ฒ.
ํด์ฑ ๊ตฌ์กฐ = ๋ฐฐ์ด + ๋งํฌ๋ ๋ฆฌ์คํธ์ ์กฐํฉ
2) ํด์ ํจ์ ? ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ ๊ณณ์ ์๋ ค์ค๋ค.
๋๋์ ๋ฐ์ดํฐ ์ค์ ์ํ๋ ๋ฐ์ดํฐ๋ฅผ ๋น ๋ฅด๊ฒ ๊ฒ์ํ ์ ์๋ค.
3) ํด์ฑ์ ๊ตฌํํ ํด๋์ค : HashSet, HashMap, Hashtable ๋ฑ๋ฑ
* ์๋ฅผ๋ค๋ฉด ์ฃผ๋ฏผ๋ฒํธ์ ๊ฒฝ์ฐ ํด์ฑ๋ ๋ฐฐ์ด ์นธ(์ฐ๋๋ณ๋ก ํด์ฑ๋ ๊ฐ์ด ๋ฐฐ์ด์ ์ ์ฅ๋์ด์์) ์ ๊ฐ์ ๋งํฌ๋ ๋ฆฌ์คํธ๋ก ์ฐ๊ฒฐ๋ ๊ฒ๋ค ์ค์์ ๊ฐ์ ์ฐพ๋๋ค!
* ๋ฐฐ์ด์ ์ฌ์ฉํ๋ ์ด์ ๋ ์ธ๋ฑ์ค์ฒ๋ผ ํ์ฉํ๊ธฐ ์ํด์ (๋งํฌ๋ ๋ฆฌ์คํธ๋ก ๊ตฌํ๋ ๊ฒฝ์ฐ ์์ ์ญ์ ๋ ๋น ๋ฅด์ง๋ง ์ ๊ทผ์ด ๋๋ฆฌ๊ธฐ ๋๋ฌธ์ X)
TreeMap
1) entry( key + value )
2) Tree ๊ตฌ์กฐ - ์ ๋ ฌ, ๊ฒ์, ๋ฒ์ ๊ฒ์ ์ฑ๋ฅ ๋น ๋ฅด๋ค.
TreeMap<String, Integer> tm = new TreeMap<String, Integer>();
// tm.ceilingEntry(null);
// tm.floorEntry(null)
// tm.firstEntry();
// tm.lastEntry();
// tm.get(key)
// tm.entrySet();
// tm.subMap(null, null) ๋ถ๋ถ ๊ฒ์
// tm.headMap(null);
// tm.tailMap(null);
tm.put("a", 1);
tm.put("b", 2);
tm.put("c", 3);
// tm.entrySet();
Properties ์ปฌ๋ ์ ํด๋์ค
1) Hashtable์ ์์ ํด๋์ค
2) key, value ๋ชจ๋ String ์ด๋ค.
3) ์ ์ฉํ ๋ฉ์๋
ํ๋ก๊ทธ๋จ ์ ํ๋ฆฌ์ผ์ด์ ์ - "ํ๊ฒฝ์ค์ " ๊ฐ ์ ์ฅ/์ฝ๊ธฐ์ ์ฃผ๋ก ์ฌ์ฉ
4) setProperty() / getProperty()
5) ํ์ผ์ ํ์ฅ์ : .properties
// Hashtable<String, String>
/* [ ์ค๋ผํด DBMS ์ฐ๊ฒฐํ ๋ ์ฌ์ฉ๋๋ ํ๊ฒฝ ์ค์ ๊ฐ. ]
* key value
String className = "oracle.jdbc.driver.OracleDriver"; fullname
String url = "jdbc:oracle:thin:@localhost:1521:xe"; ์๋ฒ
String user = "scott"; ๊ณ์ (์ฌ์ฉ์)
String password = "tiger"; ๊ณ์ ์ ๋น๋ฐ๋ฒํธ
*/
Properties prop = new Properties();
prop.setProperty( "className", "oracle.jdbc.driver.OracleDriver" );
prop.setProperty( "url" , "jdbc:oracle:thin:@localhost:1521:xe" );
prop.setProperty( "user", "scott" );
prop.setProperty( "password", "tiger" );
// prop.put(key, value) X ๊ฐ๋ฅ์ ํ์ง๋ง ์ฐ์ง ์์ (๋ถ๋ชจ๊ฐ ํด์ฌํ
์ด๋ธ์ด๋)
// prop.get(key) X
System.out.println( prop.getProperty("className") );
System.out.println( prop.getProperty("url") );
System.out.println( prop.getProperty("user") );
System.out.println( prop.getProperty("password") );
// C:\Class\WorkSpace\JavaClass\javaPro
String path = System.getProperty("user.dir");
//System.out.println( path );
// ํ์ผ๋ก ์ ์ฅ -> ํ์ผ๋ช
.properties (๋ณดํต propertiesํด๋์ค ์๋ฃํ์ผ๋ก ํ์ผ์ ์ ์ฅํ ๋)
String fileName = path +"\\src\\com\\util\\jdbcXML.properties";
/*
try ( FileWriter fw = new FileWriter(fileName) ) {
String content = String.format("%s=%s", "className", prop.getProperty( "className"));
fw.write( content +"\r\n" );
content = String.format("%s=%s", "user", prop.getProperty( "user"));
fw.write( content + "\r\n" );
System.out.println( "END");
} catch (Exception e) {
e.printStackTrace();
}
*/
try ( FileWriter fw = new FileWriter(fileName) ) {
// ํ๊ฒฝ์ค์ ๊ฐ๋ค์ ์ง์ ์ ์ฅ ์ฝ๋ฉ. X
String comments = "> Oracle JDBC Configuration <";
prop.store(fw, comments);
System.out.println( "END");
} catch (Exception e) {
e.printStackTrace();
}
* prop ๋ฉ์๋ (prop.store) ์ฐ๋ฉด๋จ!! ์๋์ผ๋ก ์ ์ฅ๋จ
.properties ํ์ผ ์ฝ์ด์ค๊ธฐ
String path = System.getProperty("user.dir");
String fileName = path +"/src/com/util/jdbcXML.properties";
try ( FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr)) {
String line = null;
Properties prop = new Properties();
while( (line= br.readLine() ) != null ) {
// # ์ฃผ์์ฒ๋ฆฌ ์ ์ธ
if( !line.startsWith("#") ) {
System.out.println( line );
String [] kv = line.split("=");
String key = kv[0];
String value = kv[1];
prop.setProperty(key, value);
} // if
} // while
System.out.println( prop );
} catch (Exception e) {
e.printStackTrace();
}
๋ ์ฌ์ด๋ฒ! (prop.load ์ฌ์ฉํ๋ฉด๋จ) Reader ๊ฐ์ฒด์ธ fr ๋ง ๋ฃ์ด์ฃผ๋ฉด ์๋์ผ๋ก ์ฝ์ด์ด
String path = System.getProperty("user.dir");
String fileName = path +"\\src\\com\\util\\jdbc.properties";
Properties prop = new Properties();
// prop.store(null, fileName); // ****
try (FileReader fr = new FileReader(fileName); ) {
prop.load( fr ); // ****
System.out.println( prop );
} catch (Exception e) {
e.printStackTrace();
}
XML๋ก ์ ์ฅํ๊ธฐ! prop.storeToXML
Properties prop = new Properties();
prop.setProperty( "className", "oracle.jdbc.driver.OracleDriver" );
prop.setProperty( "url" , "jdbc:oracle:thin:@localhost:1521:xe" );
prop.setProperty( "user", "scott" );
prop.setProperty( "password", "tiger" );
String path = System.getProperty("user.dir");
String fileName = path +"\\src\\com\\util\\jdbcXML.properties";
try ( FileOutputStream fos = new FileOutputStream(fileName) ) {
String comments = "> Oracle JDBC Configuration <";
prop.storeToXML(fos, comments);
System.out.println( "END");
} catch (Exception e) {
e.printStackTrace();
}
์๊น prop.load๋ก ์ป์ด์จ prop ๊ฐ์ฒด์ ๋ชจ๋ ํค๊ฐ์ ์ป์ด์์ ์ถ๋ ฅ(ํ์ธ)
String path = System.getProperty("user.dir");
String fileName = path +"\\src\\com\\util\\jdbc.properties";
// Hashtable ์ปฌ๋ ์
ํด๋์ค (๋ถ๋ชจ)
Properties prop = new Properties();
try (FileReader fr = new FileReader(fileName); ) {
prop.load( fr );
System.out.println( prop );
} catch (Exception e) {
e.printStackTrace();
}
// 1. Properties ๋ชจ๋ ํค๊ฐ์ ์ป์ด์์ ์ถ๋ ฅ(ํ์ธ)
// ใฑ. prop.keySet();
// value = prop.getProperty(key)
// ใด. 658 ํ 11-20 Properties ์์ฑ์ + ๋ฉ์๋
// propertyNames() : ๋ชจ๋ ํค(key) ๊ฐ ๋ด๊ธด ์ด๊ฑฐ์( enumeration) ์ ๋ฐํํ๋ ๋ฉ์๋
Enumeration<String> en = (Enumeration<String>) prop.propertyNames();
while (en.hasMoreElements()) {
String key = en.nextElement();
System.out.println( key );
}
System : ์์คํ ํ๊ฒฝ์ค์ ๊ฐ์ ๊ฐ์ง๊ณ ์๋ Properties ๊ฐ์ฒด! key๊ฐ์ ํตํด value ์ป์ด์ฌ์์๋ค.
// String path = System.getProperty("user.dir");
// ์์คํ
์ ํ๊ฒฝ์ค์ ๊ฐ์ ๊ฐ์ง๊ณ ์๋ ๊ฐ์ฒด .
Properties prop = System.getProperties();
Enumeration<String> en = (Enumeration<String>) prop.propertyNames();
while (en.hasMoreElements()) {
String key = en.nextElement();
System.out.printf("%s=%s\n", key , prop.getProperty(key) );
}
properties์์ ์ฝ์ด์์ ๊ฐ ๊ฐ๊ณต
String path = System.getProperty("user.dir");
String fileName = path +"\\src\\days26\\input.txt";
// key=value
Properties p = new Properties();
p.load( new FileReader(fileName) );
String name = p.getProperty("name");
String data = p.getProperty("data"); // "9,1,5,2,8,13,26,11,35,1"
String [] datas = data.split(",\\s*"); //,๋ก ๋๋ ์ฃผ๊ณ s=๊ณต๋ฐฑ์ด์์ด๋์ข๊ณ ์์ด๋์ข๊ณ * ๋ง์ง
int max, min , tot = 0;
double avg;
max = min = Integer.parseInt(datas[0]);
for (int i = 0; i < datas.length; i++) {
tot += Integer.parseInt(datas[i]);
int value = Integer.parseInt(datas[i]);
if( max < value ) {
max = value;
}else if( min > value ) {
min = value;
}
}
avg = (double)tot / datas.length;
System.out.printf("์ด๋ฆ : %s\n", name);
System.out.printf("ํฉ๊ณ : %d\n", tot);
System.out.printf("ํ๊ท : %.2f\n", avg);
Collections
Collecionํด๋์ค๋ฅผ ๋ค๋ฃฐ ์ ์๋ Collections ํด๋์ค
1)
ArrayList , HashMap ๋๊ธฐํ ์ฒ๋ฆฌ X + ๋๊ธฐํ ์ฒ๋ฆฌ ํ๊ณ ์ถ๋ค..
synchronized ํค์๋ = ์ค๋ ๋ + ๋๊ธฐํ ์ฒ๋ฆฌ
[synchronized]Collection()
[synchronized]List()
[synchronized]Set()
[synchronized]Map()
๋ฑ๋ฑ
Vector , Hashtable ๋๊ธฐํ ์ฒ๋ฆฌ O
2) ๋ณ๊ฒฝ ๋ถ๊ฐ ์ปฌ๋ ์ ๋ง๋ค๊ธฐ.
[unmodifiable]Collection()
[unmodifiable]List()
[unmodifiable]Set()
[unmodifiable]Map()
3) ์ฑ๊ธํค ์ปฌ๋ ์ ๋ง๋ค๊ธฐ - ๋จ ํ๋์ ๊ฐ์ฒด๋ง์ ์ ์ฅํ๋ ์ปฌ๋ ์
[singleton]List()
[singleton]Map()
4) ํ ํ์ ์ ๊ฐ์ฒด๋ง ์ ์ฅํ๋ ์ปฌ๋ ์ ๋ง๋ค๊ธฐ
[checked]Collection();
[checked]List();
[checked]Set();
import static java.util.Collections.*; ํ๋ฉด ๋ฐ๋ก์ฌ์ฉ๊ฐ๋ฅ
List list = new ArrayList();
//Collections.addAll( list , 3,5,2,4,1 );
addAll( list , 3,5,2,4,1 );
//int [] m = { 3,5,2,4,1};
// addAll(list, m); // T...args ๊ฐ๋ณ์ธ์
sort(list); // asc
sort(list, reverseOrder()); // desc
shuffle(list); // [2, 4, 1, 3, 5] [3, 2, 4, 1, 5]
System.out.println( list );
//fill(list, -1); ๋์ผํ ๊ฐ ์ฑ์๋ฃ์
int index = binarySearch(list, 3); // ์ธ๋ฑ์ค ๋ฐํ
System.out.println( index ); // ์์ผ๋ฉด -6
int max = (int) max(list);
1. ์ ๋ค๋ฆญ( Generics )
* ใฑ. JDK1.5 ์ฒ์ ๋์
* ใด. JDK1.8 ์ฒ์ ๋์ - ๋๋ค์
* ใด. ์ปดํ์ผ ํ ๋ ํ์ ๊ฒฐ์ ํด ์ฃผ๋ ๊ธฐ๋ฅ
* ? ๋ค์ํญ ํ์ (์๋ฃํ)์ ๊ฐ์ฒด -> ๋ฉ์๋(), ์ปฌ๋ ์ ํด๋์ค์ ๋ค๋ฃฐ ๋
* ์ด์ ? 1) ๊ฐ์ฒด ํ์ ์ ์์ ์ฑ
* 2) ํ๋ณํ ๋ฒ๊ฑฐ๋ก์์ ์ค์ผ ์ ์๋ค.
*
* 2. ์ ๋ค๋ฆญ ์ฅ์
* 1) ํ์ ์ ์์ ์ฑ
* 2) ํ์ ์ฒดํฌ + ํ๋ณํ ์๋ต -> ์ฝ๋ ๊ฐ๊ฒฐ.
// "์ ๋ค๋ฆญ ํด๋์ค" ์ ์ธ
// 1) Box<T> : Box ์ ๋ค๋ฆญ ํด๋์ค
// 2) Box : ์์ ํ์
// 3) T : ํ์ ๋ณ์ ๋๋ ํ์ ๋งค๊ฐ๋ณ์ ( T ํ์ ๋ฌธ์ )
// E element
// K key
// V value
// T type
// ๋ฑ๋ฑ ๋ง๋๋ก ์ด๋ฆ ์ฃผ๋ฉด
// 1) ํ์ ์์ ์ฑ
Box<Integer> box1 = new Box<Integer>(3.14);
Box<Integer> box1 = new Box<Integer>(14);
int value = box1.getValue();
Box<Double> box2 = new Box<Double>(3.14);
double value2 = box2.getValue(); // ํ๋ณํ ํ์ X
// ์ปดํ์ผ ์ T ๊ฒฐ์
class Box<T>{
// field
private T value;
// constructor
public Box(T value) {
super();
this.value = value;
}
// getter, setter
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
}
์ ๋ค๋ฆญ์ ์ ํ
* ์ ๋ค๋ฆญ ํด๋์ค ์ ์ธ
* class Box<T>{
* ์คํํฑ ๋ฉค๋ฒ ์ฌ์ฉ ๊ฐ๋ฅ ?
* static T value;
* static int compare( T t1, T t2){}
*
* T [] toArray(){
* T [] temp = new T[10]; // ์ ๋ค๋ฆญ ๋ฐฐ์ด
* return temp;
* }
*
* }
*
* 1. ์ ๋ค๋ฆญ์ ์ ํ ?
* 1) ์คํํฑ ๋ฉค๋ฒ๋ ์ฌ์ฉ(์ ์ธ)ํ ์ ์๋ค.
* 2) new T[10] ์ ๋ค๋ ๋ฐฐ์ด์ ์ฌ์ฉํ ์ ์๋ค.
* ์ฆ, new, instanceof ์ฐ์ฐ์ ์ฌ์ฉํ๋ ๋ฐฐ์ด X
* */
์ ๋ค๋ฆญ ํด๋์ค์ ๊ฐ์ฒด ์์ฑ๊ณผ ์ฌ์ฉ
Box2<Fruit> fruitBox = new Box2<Fruit>();
fruitBox.add( new Fruit());
fruitBox.add( new Apple());
fruitBox.add( new Grape());
System.out.println( fruitBox ); // [๊ณผ์ผ, ์ฌ๊ณผ, ํฌ๋]
// Type mismatch: cannot convert from Box2<Grape> to Box2<Apple>
// Box2<Apple> appleBox = new Box2<Grape>();
// The method add(Fruit) in the type Box2<Fruit> is not applicable for the arguments (Toy)
// fruitBox.add( new Toy() );
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(100);
// list.add( 3.14);
// ๊ณผ์ผ ํด๋์ค
class Fruit implements Eatable{ public String toString() { return "๊ณผ์ผ"; } }
class Apple extends Fruit{ public String toString() { return "์ฌ๊ณผ"; }}
class Grape extends Fruit{ public String toString() { return "ํฌ๋"; }}
// ์ฅ๋๊ฐ
class Toy{ public String toString() { return "์ฅ๋๊ฐ"; } }
// ์ ๋ค๋ฆญ ํด๋์ค ์ ์ธ
class Box2<T>{
ArrayList<T> list = new ArrayList<T>();
void add(T item) { list.add(item); }
T get(int idx) { return this.list.get(idx) ; }
int size() { return this.list.size(); }
public String toString() { return list.toString(); }
}
-> Fruit์ด ์ ๋ค๋ฆญ์ผ๋ก ๋ค์ด๊ฐ๋ฉด Apple, Grape ์ธ์คํด์ค ๋ชจ๋ ์ง์ด๋ฃ๊ธฐ ๊ฐ๋ฅ
์ ํ๋ ์ ๋ค๋ฆญ ํด๋์ค (์ด๊ฑด ์์ ์ ํ์ด๋ ๋ค๋ฆ)
์ธ์คํด์ค ์์ฑํ ๋
public class Ex14 {
public static void main(String[] args) {
// ๊ณผ์ผ์์ - ์ฌ๊ณผ, Grape
// - ์ฅ๋๊ฐ X ์ ํ
FruitBox<Apple> fbox = new FruitBox<>();
FruitBox<Grape> gbox = new FruitBox<>();
// ์ฌ์ฉํ ์ ์๋ค.
// FruitBox<Toy> tbox = new FruitBox<>(); // ์ ํ
} // main
} // class
interface Eatable{}
// ํ์
์ ํ - Fruit ํด๋์ค ์์ and Eatable ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ํด๋์ค(ํ์
)์ผ๋ก ์ ํ
class FruitBox<T extends Fruit & Eatable>{ // T ํ์
์ ์ ํํฉ๋๋ค. ์ ํ๋ ์ ๋ค๋ฆญ ํด๋์ค
ArrayList<T> list = new ArrayList<T>();
}
-> ๊ฐ์ฒด ์์ฑํ ๋ ์ ๋ค๋ฆญ T์ ์๋ฃํ์ ์ ํํ ์ ์๋ค๋ ๋ง์ (ํด๋์ค, ์ธํฐํ์ด์ค ๊ฐ๋ฅ)
public class Ex02 {
public static void main(String[] args) {
FruitBox<Apple> appleBox = new FruitBox<>();
Juice appleJuice = Juicer.makeJuice(appleBox);
FruitBox<Grape> grapeBox = new FruitBox<>();
/// The method makeJuice(FruitBox<Apple>) in the type Juicer
// is not applicable for the arguments
Juice grapeJuice = Juicer.makeJuice(grapeBox);
/*
p 686 ์ ์ผ ํ๋จ ์ค๋ช
.
Collections.sort(null);
@SuppressWarnings("unchecked")
public static <T extends Comparable<? super T>> void sort(List<T> list) {
list.sort(null);
}
*/
// p 687 ์ ๋ค๋ฆญ ํ์
์ ํ๋ณํ
// p 689 ์ ๋ค๋ฆญ ํ์
์ ์ ๊ฑฐ
// ArrayList list = new ArrayList(); // ํธํ์ฑ
} // main
} // class
class Fruit{ public String toString() { return "๊ณผ์ผ"; } }
class Apple extends Fruit { public String toString() { return "์ฌ๊ณผ"; } }
class Grape extends Fruit{ public String toString() { return "ํฌ๋"; } }
class FruitBox<T extends Fruit >{ // ์ ํ๋ ์ ๋ค๋ฆญ ํด๋์ค
ArrayList<T> list = new ArrayList<T>();
}
// ์ฅฌ์ค
class Juice{}
// ์ฅฌ์ค ๋ง๋๋ ๊ธฐ๊ณ + ์ฌ๋
class Juicer{
/*
// ์ค๋ฒ๋ก๋ฉ X == ์ค๋ณต ํจ์ X
// ๊ณผ์ผ์์ -> ์ฅฌ์ค ๋ง๋๋ ๋ฉ์๋
*/
/*
// ์์ผ๋ ์นด๋ ์ฌ์ฉ - ๋งค๊ฐ๋ณ์ ๊ตฌํ(์ ์ธ)
static Juice makeJuice( FruitBox<? extends Fruit> box ) {
return new Juice();
}
*/
// ์ ๋ค๋ฆญ ๋ฉ์๋๋ก ์ ์ธ
static <T extends Fruit> Juice makeJuice( FruitBox<T> box ) {
return new Juice();
}
}
์์ผ๋ ์นด๋ (=?, ๋ฌผ์ํ)
<? extends T> : T์ ๊ทธ ์์๋ค๋ง ๊ฐ๋ฅ
<?> == <? extends Object> : ๋ชจ๋ ํ์ ์ด ๊ฐ๋ฅ, ์ ํ ์์
<? super T> : T์ ๊ทธ ์กฐ์๋ค๋ง ๊ฐ๋ฅ
// ์๋ ์
์บ์คํ
// Employee emp = new Regular();
Vector<Regular> v1 = new Vector<Regular>();
v1.add(new Regular());
v1.add(new Regular());
v1.add(new Regular());
// Type mismatch: cannot convert from Vector<Regular> to Vector<Employee>
// Vector<Employee> v2 = v1;
Vector<? extends Employee> v2 = v1;
// v2.addAll( Collection<? extends Employee > c )
// Collection c
// ArrayList<Temp> list -> List -> Collection
ArrayList<Temp> list = new ArrayList<Temp>();
list.add(new Temp());
list.add(new Temp());
list.add(new Temp());
// v2.addAll( list );
์ต๊ทผ๋๊ธ