Computer Science/Languages

[java] Collection 활용

seungah yoo 2019. 1. 17. 16:17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
jpackage com.util;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
public class CollectionTest {
    public static void main(String[] args) {
        List fruits = new ArrayList<>();
//        fruits.add("apple");
//        fruits.add("orange");
//        fruits.add("banana");
        
        //1.addAll
        Collections.addAll(fruits, "apple","orange","banana");
        
        //2. sort
        Collections.sort(fruits);
        System.out.println(fruits.toString());
        
        //3. search
        Collections.sort(fruits);
        System.out.println(Collections.binarySearch(fruits, "banana"));
        System.out.println(Collections.binarySearch(fruits, "grape"));
    
        //4. copy
        List list = new ArrayList();
        Collections.addAll(list, "one","two","three","four");
        Collections.copy(list, fruits);
        System.out.println(list.toString());
        
        //5. fill
        Collections.fill(list, "filled with dummy data");
        System.out.println(list.toString());
    }    
}
 
cs