[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 );

 

  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ