ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 컬렉션 프레임 워크
    언어/JAVA 2020. 5. 4. 14:18
    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
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    package test;
    import java.util.*;
     
    public class ArrayList {
     
        public static void main(String[] args) {
            List<String> list = new LinkedList<>();
            
            list.add("b");
            list.add("o");
            list.add("r");
            list.add("a");
            
            
            for(int i=0;i<list.size();i++)
                System.out.println(list.get(i));
            System.out.println();
            
            list.remove(0);
            
            for(int i=0;i<list.size();i++)
                System.out.println(list.get(i));
            System.out.println();
            
            
            for(String s : list)
                System.out.println(s);
            System.out.println();
            
            Iterator<String> lit = list.iterator();
            while(lit.hasNext())
                System.out.println(lit.next());
            System.out.println();
            
        }
     
    }
    /////////////////////////////////////////////
     
    package test;
     
    import java.util.*;
     
    class Car implements Comparable<Car> {
        private int disp;
     
        public Car(int d) {
            disp = d;
        }
     
        @Override
        public String toString() {
            return "" + disp;
        }
     
        @Override
        public int compareTo(Car o) {
            return disp - o.disp;
        }
    }
     
    public class CarSortCollections {
     
        public static void main(String[] args) {
     
            List<Car> list = new LinkedList<>();
            list.add(new Car(1200));
            list.add(new Car(400));
            list.add(new Car(120));
     
            Collections.sort(list);
     
            for (Iterator<Car> itr = list.iterator(); itr.hasNext();)
                System.out.println(itr.next().toString());
        }
    }
     
    /////////////////////////////
    package test;
     
     
    class Person{
        private String name;
        private int age;
        public Person(String name, int age) {
            this.name=name;
            this.age=age;
        }
        public String toString() {
            return name+"("+age+"세)";
        }
        
        @Override
        public int hashCode() {
            return Objects.hash(name,age);
        }
        @Override
        public boolean equals(Object obj) {
            Person comp=(Person)obj;
            if(comp.name.equals(name)&&(comp.age==age))
                return true;
            else
                return false;
        }
        
    }
     
    public class HashEquals {
     
        public static void main(String[] args) {
            HashSet<Person> hset = new HashSet<>();
            hset.add(new Person("k",20));
            hset.add(new Person("J",20));
            hset.add(new Person("M",20));
            hset.add(new Person("k",20));
            
            System.out.println(hset.size());
            System.out.println(hset);
        }
     
    }
    //////////////////////////
    package test;
    import java.util.*;
     
     
    class HashMapColl{
        public static void main(String[] args) {
            HashMap<Integer,String> map = new HashMap<>();
            
            map.put(12,"bora");
            map.put(22,"bowrwea");
            map.put(11,"b2343ra");
            
            System.out.println(map.get(12));
            
            map.remove(22);
            
            System.out.println("11번:"+map.get(11));
            
            Set<Integer> ks =map.keySet();
            for(Iterator<Integer> itr = ks.iterator();itr.hasNext();)
                System.out.println(map.get(itr.next()));
            System.out.println();
        }
     
    }
    ////////////////////////////
    package test;
     
     
    class Num{
        private int num;
        public Num(int n) {num=n;}
        
        @Override
        public String toString() {
            return String.valueOf(num);
        }
        @Override
        public int hashCode() {
            return num%3;
        }
        @Override
        public boolean equals(Object obj) {
            if(num ==((Num)obj).num)
                return true;
            else
                return false;
            
        }
    }
    public class HashSetEquality {
     
        public static void main(String[] args) {
            HashSet<Num> set = new HashSet<>();
            set.add(new Num(7799));
            set.add(new Num(7799));
            set.add(new Num(9955));
            System.out.println(set.size());
            
            for(Num n : set)
                System.out.println(n.toString());
        }
     
    }
     
     
     
    http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
    http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

    '언어 > JAVA' 카테고리의 다른 글

    optional  (0) 2020.05.04
    자료구조  (0) 2020.04.28
    함수적 인터페이스 20200422  (0) 2020.04.25
    제네릭 sort  (0) 2020.04.25
    Vehicle 솔루션 최종  (0) 2020.04.25
Designed by Tistory.