[Day24] Java 24 [9/13]
컬λ μ νλ μμν¬ (JCF)
<1> 컬λ μ νλ μμν¬
γ±. 컬λ μ ( collection )
λ°μ΄ν° μ§ν©
γ΄. νλ μμν¬ ( framework )
ν( κ·λ² ) / 볡μ‘ν λ¬Έμ λ₯Ό ν΄κ²°νκΈ° μν λΌλ μν ꡬ쑰 / νμ€νλ νλ‘κ·Έλλ° λ°©μμ μλ―Έ
-> λ°μ΄ν° κ΅°(μ§ν©)μ μ μ₯(λ€λ£¨λ)νλ ν΄λμ€λ€μ νμ€νν μ€κ³
-> νμ€ν 체κ³( λΌλ, ν, μ€κ³ )
<2> λ°°μ΄ - λ°μ΄ν° μ§ν© + κ°λ°μ μ§μ ꡬν
<3> 컬λ μ νλ μμν¬ κ΅¬μ± - ν΅μ¬ μΈν°νμ΄μ€ ( 3κ°μ§ ) μ΄ν΄
Collection μΈν°νμ΄μ€ Map μΈν°νμ΄μ€
p580 [ν 11-2] 15 μΆμλ©μλ ν€(key)+κ°(value) μ -> λ°μ΄ν° μ§ν©
λ°μ΄ν° μ§ν© - λ° μΆ(add, addAll) μνΈλ¦¬ (Entry = key + value)
λ° μ ( clear,remove) μμ X, ν€ μ€λ³΅ X, κ° μ€λ³΅ O
↑
List μΈν°νμ΄μ€ Set μΈν°νμ΄μ€
λΆλͺ¨ - 15 μΆμλ©μλ
μμ O μμ X
μ€λ³΅ O μ€λ³΅ X
<4>
List ꡬνν ν΄λμ€ : [ArrayList] Ex08_02.java, LinkedList, Stack, Vector λ±λ±
Set " : HashSet, TreeSet λ±λ±
Map " : HashMap, TreeMap, Hashtable, Properties λ±λ±
ArrayList 컬λ μ ν΄λμ€
1) κ°μ₯ λ§μ΄ μ¬μ©λλ 컬λ μ ν΄λμ€μ΄λ€.
2) Collection μΈν° -> List μΈνλ₯Ό ꡬνν ν΄λμ€
νΉμ§ : μμ O, μ€λ³΅ νμ© O
μ μ λ°μ΄ν°μ μ§ν© - λ°°μ΄ μ¬μ©
λ°°μ΄ λ¬Έμ μ (λ¨μ ) ArrayList
1) 100 -> μλμΌλ‘ λ°°μ΄ν¬κΈ° μ¦κ° X ==> ν¬κΈ° μλ μ¦κ°
2) λ°μ΄ν° μ²λ¦¬νλ μμ ( μΆκ°, μμ , μ½μ , μμ ) λ±λ±
// <E> μ λ€λ¦ X
// ArrayList list = new ArrayList( ); // 10 μ΄κΈ° μ©λ
ArrayList list = new ArrayList( 10 ); // 10 μ΄κΈ° μ©λ
// Object e : λͺ¨λ νμ
맀κ°λ³μ μ¬μ© κ°λ₯
// e == [e]lement == μμ
// 1) list.add()
list.add( 10 ); // int -> μ€ν λ°μ± Integer -> Object 맀κ°λ³μ λ€νμ±
// 10κ° λκ² μ μ₯κ°λ₯
for (int i = 0; i < 10 ; i++) {
list.add( Rnd.getRand(1, 100) );
}
// list.add(index , element );
list.add( 2, 100); // μ½μ
// 2) list.size()
System.out.println( list.size() ); // ν¬κΈ°
// 3) list.get()
System.out.println( list.get(3) );
// foreach λ¬Έ == νμ₯ forλ¬Έ νΈλ¦¬νλ€.
/*
for (Object n : list) {
System.out.printf( "[%d]", n );
}
*/
// 4) list.clear(); = λͺ¨λ μμ μμ
// 5-1) list.remove( index );
// 5-2) list.remove( μμ );
// 6. list.clone()
ArrayList list1 = new ArrayList();
list1.add( new Integer(4) );
list1.add( 5 );
list1.add(2);
list1.add(3);
list1.add(1);
ArrayList li = (ArrayList) list1.clone();
String λ°°μ΄ -> ArrayList
String [] names = { "κΉκ°μ¨", "μ΄μ¬λ―Ό", "νμ μ°", "μν¬μ§", "μ λ―Όμ§"} ;
List l = Arrays.asList( names ); // T...a κ°λ³ μΈμ ( λ¬Έμ )
ArrayList list = new ArrayList(l);
μ λ ¬
list1.sort( new Comparator() {
@Override
public int compare(Object o1, Object o2) {
int a = (int) o1;
int b = (int) o2;
return b - a; // a-b
}
} );
java.util.Iterator(λ°λ³΅μ) μΈν°νμ΄μ€ & Enumerator(μ΄κ±°μ)
// λͺ¨λ μμ(element)λ₯Ό μΆλ ₯. - λ°λ³΅μ(iterator) μ¬μ©
// java.util.Iterator μΈν°νμ΄μ€
Iterator ir = list.iterator(); // iterator() λ©μλ
// boolean hasNext()
while (ir.hasNext()) {
// next()
String name = (String) ir.next();
System.out.println( name );
}
// μ΄κ±°μ
Enumeration en = list.elements();
while (en.hasMoreElements()) {
int n = (int) en.nextElement();
System.out.println( n );
}
// λ°λ³΅μ
Iterator ir = list.iterator();
while (ir.hasNext()) {
int n = (int) ir.next();
System.out.println( n );
}
String μ λ ¬
list.sort( new Comparator() { // μ΅λͺ
(무λͺ
) ν΄λμ€
@Override
public int compare(Object o1, Object o2) {
String s1= (String)o1;
String s2= (String)o2;
// String λ©μλ μ€λͺ
// return s1.compareTo(s2); // 0 μμ μμ
return s2.compareTo(s1);
}
} );
// λλ€ννμ -> λλ€μ°μ°μ
list.sort( (o1,o2) -> ((String)o1).compareTo(((String)o2)) );
// Collections.sort(list); // μ€λ¦μ°¨μ μ λ ¬
// Collections.sort(list , Collections.reverseOrder() ); // λ΄λ¦Όμ°¨μ μ λ ¬
ArrayList & Vector - List μΈν°νμ΄μ€κ΅¬ν
- 100% λμΌν 컬λ μ ν΄λμ€
- μ°¨μ΄μ ) λ©ν° μ€λ λ μμ μ± ( Vector λκΈ°ν μ²λ¦¬ O )
Vector list = new Vector(10);
System.out.println( list.capacity() +" / " + list.size() ); // μ΄κΈ° μ©λ : 10
list.add(1);
list.add(2);
list.add(3);
System.out.println( list.capacity() +" / " + list.size() ); // μ΄κΈ° μ©λ : 10, 3
// list.add(1, 500); // insert μ½μ
// list.set(1, 500); // μμ
// list.setElementAt(500, 1); // μμ
System.out.println( list );
// μ©λμ νμ₯νλ λ©μλ
// list.ensureCapacity( 16 );
list.setSize(5);
// list.firstElement() == list.get(0) == list.elementAt(0)
// list.lastElement() == list.get( list.size() - 1 )
//list.removeElement(list);
//list.removeElementAt(0)
// list.subList(0, 0);
LinkedList
μμ μ μ§ O, μ€λ³΅ νμ© O, λΉμμ°¨μ μΌλ‘ μ½μ /μμ - μ±λ₯ μ°μ
λ¨λ°©ν₯ λ§ν¬λ리μ€νΈ
μλ°©ν₯(λλΈ) "
νν "
λλΈ νν "
κ°λ
Node n1 = new Node();
n1.value = 10;
Node n2 = new Node();
n2.value = 20;
Node n3 = new Node();
n3.value = 30;
Node n4 = new Node();
n4.value = 40;
n1.next = n2;
Node n100 = new Node();
n100.value = 100;
n2.next = n3;
n3.next = n4;
n4.next = null;
Node n5 = new Node();
n5.value = 50;
n4.next = n5;
n5.next = null;
n2.next = null;
Node node = n1;
while( node != null ) {
System.out.println( node.value );
node = node.next;
}
class Node{
Node next; // λ€μ μμ(λ
Έλ)μ μ£Όμκ°μ μ μ₯ν μ°Έμ‘° λ³μ
Node prev; // μ΄μ μμ(λ
Έλ)μ μ£Όμκ°μ μ μ₯ν μ°Έμ‘° λ³μ
int value; // μ μ₯ν κ°
}
// ArrayList, Vector () + 10 μ©λ
LinkedList list = new LinkedList();
list.add(10);
list.add(20);
list.add(30);
list.addFirst(40);
//System.out.println( list.get(2) );
list.getFirst();
list.getLast(); // λ
Έλ(node)
// list.remove();
// λ¬Έμ ) μμ(λ
Έλ)μ κ°μ΄ 20λ³΄λ€ ν° λ
Έλλ λͺ¨λ μ κ±°νμ.
// λ‘μ§
// [ 10, 30]
// 0 1
// μμ λ₯Ό νλλΌλ λ€μ λ
Έλ(μμ)μ μΈλ±μ€κ° λ³κ²½μ΄ λμ§ μλλ‘ νκΈ° μν΄ λ€μ μμλΆν° 체ν¬νκ³ μμ νμ..
for (int i = list.size()-1; i >=0; i--) {
if( (int)list.get(i) > 20 ) list.remove(i);
}
list.removeIf( new Predicate() {
@Override
public boolean test(Object t) {
int n = (int) t; // λ€μ΄μΊμ€ν
+ μΈλ°μ± ( Object -> Integer -> int )
return n > 20 ;
} // μ΅λͺ
ν΄λμ€
} );
//list.removeIf( t -> (int)t > 20 );
//System.out.println( list );
Iterator ir = list.descendingIterator();
while (ir.hasNext()) {
int n = (int) ir.next();
System.out.println( n );
} // while
// list.clear();
// System.out.println(list.isEmpty());
* κ²°λ‘ 1) μμ°¨μ μΌλ‘ μΆκ°/μμ ArrayList > LinkedList μ±λ₯ μ°μ
* κ²°λ‘ 2) λΉμμ°¨μ μΌλ‘ μΆκ°/μμ ArrayList < LinkedList μ±λ₯ μ°μ
Stack & Queue
// stack ꡬ쑰( LIFO ) push() peek() pop()
// queue ꡬ쑰( FIFO) offer() peek() poll()
// isEmpty() / empty()
// size(), remove(), element(), search() λ±λ± λ©μλ
μ€ν ꡬ쑰 : LIFO( Last In First Out )
Stack => Vector => List
add()
push()
pop()
peek()
Stack s = new Stack();
s.push("λ°μλ¦°");
s.push("μ‘°λ―Όκ²½");
s.push("λ
Έμ©μ€");
// μμλ₯Ό μ κ±°νμ§ μκ³ λ§μ§λ§ μμ μ½κΈ°
System.out.println( s.peek() );
// μμλ₯Ό μ κ±°νκ³ λ§μ§λ§ μμ μ½κΈ°
System.out.println( s.pop() );
System.out.println( s.pop() );
System.out.println( s.size() );
while( s.size() != 0) {
System.out.println( s.pop() );
}
while( ! s.empty() ) {
System.out.println( s.pop() );
}
int index = s.search("μ‘°λ―Όκ²½");
System.out.println(index );
ν ꡬ쑰 : FIFO( First In First Out )
Queue q = new LinkedList();
q.offer("μμ’
μ€");
q.offer("ν©μλΉ");
q.offer("κΉκ°μ¨");
System.out.println( q );
// System.out.println( q.poll() );
// System.out.println( q.peek() );
while ( !q.isEmpty()) {
System.out.println( q.poll() );
}
μ€ννμ© : κ΄νΈμ²΄ν¬
Stack s = new Stack();
String expression ;
System.out.print("> ννμ μ
λ ₯ ? ");
expression = new Scanner(System.in).nextLine();
for (int i = 0; i < expression.length(); i++) {
char one = expression.charAt(i);
if( one == '(' ) {
s.push(one); // '(' -> "("
}else if(one == ')' ) {
s.pop();
}
}// for
System.out.println( s.size() );
if ( s.empty() ) {
System.out.println("κ΄νΈκ° μΌμΉνλ€.");
} else {
System.out.println("κ΄νΈκ° μΌμΉνμ§ μλλ€.");
}
μ΅κ·ΌλκΈ