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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295 |
------------------------------------------------------------------------------------------
package chap07;
public class AnnotionClass extends AnnotionSuperClass {
public static void main(String[] args) {
AnnotionClass a = new AnnotionClass();
}
@Override
public void print() {
// TODO Auto-generated method stub
}
@Override
public int add(int a, int b) {
// TODO Auto-generated method stub
return 0;
}
@Override
public String add(String a, String b) {
// TODO Auto-generated method stub
return null;
}
}
abstract class AnnotionSuperClass {
public abstract void print();
public abstract int add(int a, int b);
public abstract String add(String a, String b);
}
------------------------------------------------------------------------------------------
package chap07;
import java.io.Serializable;
public class Box1<T extends Number> {
private T t;
public void add(T t) {
this.t = t;
}
public T get() {
return t;
}
public <U extends String & Serializable> void inspect(U u) {
System.out.println("T:" + t.getClass().getName());
System.out.println("U:" + u.getClass().getName());
}
public static void main(String[] args) {
Box1<Integer> integerBox = new Box1<Integer>();
integerBox.add(new Integer(10));
integerBox.inspect("some text");
}
}
------------------------------------------------------------------------------------------
package chap07;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class CalendarDate {
public static void main(String[] args) {
String week = "일월화수목금토";
GregorianCalendar gc = new GregorianCalendar();
String half = (gc.get(Calendar.AM_PM) == 0) ? "오전" : "오후";
String today = gc.get(Calendar.YEAR) + "년"
+ (gc.get(Calendar.MONTH) + 1) + "월"
+ gc.get(Calendar.DATE) + "일"
+ week.charAt((gc.get(Calendar.DAY_OF_WEEK) - 1)) + "요일"
+ half + gc.get(Calendar.HOUR) + "시"
+ gc.get(Calendar.MINUTE) + "분"
+ gc.get(Calendar.SECOND) + "."
+ gc.get(Calendar.MILLISECOND) + "초";
System.out.println("현재시간 :" + today);
Date date = gc.getTime();
String str
= DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(date);
System.out.println(str);
}
}
------------------------------------------------------------------------------------------
package chap07;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class CallBackTest {
public static void main(String[] args) {
Timer t = new Timer(10, evernt -> System.out.println("beep"));
t.start();
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
Timer t1 = new Timer(10, new MyActionCommand());
t1.start();
}
}
class MyActionCommand implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("beep1");
}
}
------------------------------------------------------------------------------------------
package chap07;
public class ClassName {
public static void main(String[] args) {
System.out.println("This class name is:" + ClassName.class.getName());
ClassName cn = new ClassName();
cn.className(args);
Object obj = args;
cn.className(obj);
cn.className("test");
}
void className(Object obj) {
System.out.println("The class of" + obj + "is" + obj.getClass().getName());
}
}
------------------------------------------------------------------------------------------
package chap07;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class ComplexCollection {
public static void main(String[] args) {
Prof p = new Prof("홍길동");// 생성자 Prof "홍길동"
p.addSubject("자바개요");
p.addSubject("자바기본문법");
p.addSubject("자바객체");
Object obj[] = p.getSubject();
List<String> li = new ArrayList<String>();
int cnt = 0;
for (Object ob : obj) {
li.add(++cnt + "강." + (String) ob);
}
p.addLecture("자바", li);
print(p);
System.out.println(p.listLecture());
p = new Prof("성춘향");
p.addSubject("HTML개요");
p.addSubject("HTML기본태그");
p.addSubject("JavaScript");
obj = p.getSubject();
li = new ArrayList<String>();
cnt = 0;
for (Object ob : obj) {
li.add(++cnt + "강." + (String) ob);
}
p.addLecture("HTML", li);
System.out.println(p.listLecture());
print(p);
}
public static void print(Prof prof) {
System.out.println("==================");
System.out.printf("교수명: %s%n", prof.getName());
System.out.println("------------------");
Object key = prof.getLectureKey().toArray()[0];
System.out.printf("개설과목명: %s%n", key);
System.out.println("------------------");
System.out.printf("과목 내용:%n");
System.out.println("------------------");
List<String> lec = prof.listLecture().get(key);
for (String st : lec)
System.out.printf("%s%n", st);
System.out.println("==================");
}
}
class Prof {
private List<String> subject;// subject는 List구조 String타입이다.
private String name;// name은 String타입이다.
private Map<String, List<String>> lecture;// lecture는 Map구조이다.
public String getName() {
return name;
}
public Prof(String name) {// 생성자 Prof(String name)
this.name = name;
subject = new ArrayList<String>();// ArrayList객체 생성
lecture = new HashMap<String, List<String>>();// HashMap객체 생성
}
public Set getLectureKey() {
return lecture.keySet();
}
public List<String> getLecture(Object key) {
return lecture.get(key);
}
public Object[] getSubject() {
return subject.toArray();
}
public int getSize() {
return subject.size();
}
public void addSubject(String name) {// Subject 과목을 하나하나 추가하는 것
subject.add(name);
}
public void addLecture(String key, List<String> lst) {// Lecture을 하나하나 추가하는
// 것
lecture.put(key, lst);
}
public Map<String, List<String>> listLecture() {
return lecture;
}
}
------------------------------------------------------------------------------------------
package chap07;
import java.util.Date;
public class DataClass {
public static void main(String[] args) {
Date start = new Date();
long cnt = 0L;
for (long i = 0; i < 1000000000; i++) {
// String st=new String("대한민국"+i);
// System.out.println(".");
// cnt+=i;
// st+=st;
StringBuffer st = new StringBuffer("대한민국" + i);
}
System.out.println();
Date end = new Date();
System.out.println(end.getTime() - start.getTime());
System.out.println(cnt);
}
}
------------------------------------------------------------------------------------------
package chap07;
public class EnumClass {
public static void main(String[] args) {
Day d = Day.mn;
String day[] = { "월요일", "화요일", "수요일",
"목요일", "금요일", "토요일", "일요일" };
int a[] = { 1, 2, 3, 4, 5 };
for (int d1 : a) {
System.out.println(d1);
}
for (String d1 : day) {
System.out.println(d1);
}
for (Day d1 : Day.values()) {
System.out.println(d1.ordinal());
System.out.println(d1.getName());
System.out.println(d1);
}
}
}
enum Day {
mn("월요일"), tu("화요일"), we("수요일"), te("목요일"),
fr("금요일"), sa("토요일"), su("일요일");
private final String name;
private Day(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
enum Val {
mn(1), tu(2);
private final int i;
private Val(int i) {
this.i = i;
}
public int getVal() {
return i;
}
}
------------------------------------------------------------------------------------------
package chap07;
import java.util.ArrayList;
public class GenericClass {
public static void main(String[] args) {
ArrayList al = new ArrayList();
ArrayList<Integer> al1 = new ArrayList();
ArrayList<Integer> al2 = new ArrayList<Integer>();
ArrayList al3 = new ArrayList<String>();
ArrayList<String> al4 = new ArrayList<String>();
al.add("사과");
al.add(new Integer("10"));
al1.add(new Integer("10"));
// al1.add("사과");//String 타입 안됨 Integer 타입만 가능
// al2.add("사과");
al2.add(new Integer("10"));
al3.add("사과");
al3.add(new Integer("10"));
al4.add("사과");
// al4.add(new Integer("10"));//String 타입만 가능
System.out.println(al);
System.out.println(al1);
System.out.println(al2);
System.out.println(al3);
System.out.println(al4);
for (int i = 0; i < al.size(); i++) {
Object ob = al.get(i);
String s = null;
Integer i1 = null;
if (ob instanceof String)
s = (String) ob;
if (ob instanceof Integer)
i1 = (Integer) ob;
if (s != null)
System.out.println(s);
if (i1 != null)
System.out.println(i1);
}
for (int i = 0; i < al1.size(); i++) {
Integer i1 = al1.get(i);
System.out.println(i1);
}
}
}
------------------------------------------------------------------------------------------
package chap07;
import java.util.ArrayList;
public class GenericTyoeClass {
public static void main(String[] args) {
MyVector<String> my = new MyVector<String>();
my.add("딸기");
my.add("바나나");
my.addElement(new String("딸기"));
my.addElement(new String("바나나"));
ArrayList<String> al = my.getElement();
String s = my.get();
System.out.println(s);
for (String ss : al)
System.out.println(ss);
MyVector<Integer> my1 = new MyVector<Integer>();
my1.addElement(new Integer("5"));
my1.addElement(new Integer("15"));
ArrayList<Integer> al1 = my1.getElement();
for (Integer ss1 : al1)
System.out.println(ss1);
}
}
class MyVector<T> {
private T t;
private ArrayList<T> al;
public MyVector() {
al = new ArrayList<T>();
}
public void add(T t) {
this.t = t;
}
public void addElement(T t) {
this.al.add(t);
}
public T get() {
return t;
}
public ArrayList<T> getElement() {
return al;
}
}
------------------------------------------------------------------------------------------
package chap07;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.Vector;
public class HashSetClass {
public static void main(String[] args) {
// List랑 Set의 차이를 설명하는 것이다.
Set<Integer> s1, s2;
List<Integer> v1; // Vector는 List의 타입이다.
s1 = new HashSet<Integer>();// 생성자
v1 = new Vector<Integer>();// 생성자
s2 = new LinkedHashSet<Integer>();// 생성자
s1.add(new Integer(1));// 값 넣기
s1.add(new Integer(2));// 값 넣기
s1.add(new Integer(3));// 값 넣기
s1.add(new Integer(1));// 값 넣기
s2.addAll(s1);// 모든 값 넣기
v1.addAll(s1);// 모든 값 넣기
s2.add(new Integer(1));// Set의 경우 중복된 값을 넣을 수 없으나
v1.add(new Integer(1));// Vector의 경우 List와 같아서 중복된 값을 넣을 수 있다.
System.out.println(s1);
System.out.println(s2);
System.out.println(v1);
s1.stream().forEach(System.out::println);
// s1안에 있는 요소를 하나하나 씩 출력을 할 떄 쓰는 문장.
s2.stream().forEach(System.out::println);
v1.stream().forEach(System.out::println);
System.out.println(v1.get(0));
// Vector는 List형태라서 get()으로 값을 가져올 수 있지만
// System.out.println(s1.get(0));
//Set는 가져올 수가 없다. 그 이유는 Set은 Index가 없기 때문이다.
System.out.println(s1.contains(v1.get(0)));
// 이 문장은 s1에 v1의[0]의 값이 있는 지를 물어보는 것이다.
s1.parallelStream().filter(a -> a >= 2).forEach(System.out::println);
// 람다식을 사용하여 a가 a>=2일 경우만 출력하는 문장.
for (Integer i : s1) {
System.out.println(i);
} // 배열 처럼 출력을 할 때 사용 하는 문장이며 현재 Set은 배열 형태가 아니다
// 배열 처럼 출력하기 위해 이렇게 적었다.
Object[] obj = s1.toArray();// 객체 배열로 바뀜
System.out.println("-----------------------------");
for (Object i : obj) {// 배열 : 요소
System.out.println((Integer) i);
System.out.println(i);
// Warpper 클래스 특징상 데이터 타입 변환을 안해도 되기 때문에 (Integer)을 생략해도 된다.
}
System.out.println("-----------------------------");
Iterator itt = s1.iterator();
for (; itt.hasNext();) {
System.out.println(itt.next());
}
}
}
------------------------------------------------------------------------------------------
package chap07;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
public class HashTableClass {
public static void main(String[] args) {
Map<String, Integer> m1, m2;
// Map은 Key값이 중복이 되면 안되고 Warrper 클래스를 사용해야 한다.
// Set는 집합이며 중복되는 값을 가지지 않는다.
m1 = new Hashtable<String, Integer>();
m2 = new HashMap<String, Integer>();
m1.put("1", new Integer("100"));// put("키값", 안에 있는 것은 값)
m1.put("2", new Integer("200"));
m1.put("3", new Integer("300"));
m2.putAll(m1);
m1.put("4", new Integer("400"));
m2.put("4", new Integer("400"));
m1.put("1", new Integer("500"));// 키값이 "1"인 주소에 500을 덮어쓴다.
m2.put("1", new Integer("500"));
m1.put("홍길동", new Integer("600"));
m2.put("성춘향", new Integer("700"));
System.out.println(m1);
System.out.println(m2);
System.out.println("-----------------");
System.out.println(m2.get("1"));
// Map에 있는 값을 꺼낼 경우네는 키값을 꺼내야 한다.
System.out.println(m2.get("3"));
System.out.println("-----------------");
Set<String> s1 = m1.keySet();// KeySet을 통해서 Key값을 가져올 수가 있다.
s1.stream().forEach(System.out::println);
System.out.println("--------------parallelStream-----------------");
s1.parallelStream().forEach(System.out::println);
System.out.println("-----------------");
for (String s : s1) {
System.out.printf("%s 키에 대한 값은 : %s%n", s, m1.get(s));
}
System.out.println("-----------------");
System.out.println(m1.containsValue("500"));
System.out.println(m1.containsValue("100"));
System.out.println(m1.containsValue(new Integer("500")));
System.out.println(m1.containsValue(new Integer("100")));
System.out.println("-----------------");
System.out.println(m1.containsKey("5"));
System.out.println(m1.containsKey("6"));
System.out.println(m1.containsKey("4"));
System.out.println("------Iterator--------");
Iterator<String> it = m1.keySet().iterator();
while (it.hasNext()) {
// iterator를 썼기 때문에 .hasNext()를 쓸 수가 있다.
Object ob = it.next();
System.out.printf("%s=%s%n", ob, m1.get(ob));
}
System.out.println("-----------------");
Vector<Map> v1 = new Vector<Map>();
// List 구조를 <Map>이라는 구조에 개체를 담는다는 뜻이다.
Map<String, String> m3 = new HashMap<String, String>();
m3.put("성명", "홍길동");
v1.add(m3);
m3 = new HashMap<String, String>();
m3.put("성명", "성춘향");
v1.add(m3);
m3 = new HashMap<String, String>();
m3.put("성명", "이몽룡");
v1.add(m3);
for (Map m : v1) {
System.out.println(m.get("성명"));
} // 자료구조는 데이터를 메모리 구조에 담는 것이다.
}
}
------------------------------------------------------------------------------------------
package chap07;
public class IntegerClass {
public static void main(String[] args) {
Integer i1, i2, i3;
i1 = new Integer(10);
i2 = new Integer("10");
i3 = new Integer(i1);
System.out.printf("%d,%d,%d %n", i1, i2, i3);
i1 = 10;
i2 = 20;
i3 = 30;
System.out.printf("%d,%d,%d %n", i1, i2, i3);
System.out.printf("%s,%s,%s %n", i1.equals(i1), i2.equals(i2), i3.equals(i3));
byte b1 = i1.byteValue();
short s1 = i1.shortValue();
System.out.printf("b1=%d, s1=%d", b1, s1);
int a = Integer.parseInt("123");
int b = i3.valueOf("123");
System.out.printf("a=%d, b=%d", a, b);
String binary, hex;
binary = Integer.toBinaryString(5);
hex = Integer.toHexString(10);
System.out.printf("binary=%s, hex=%s binary길이= %d%n", binary, hex, binary.length());
// byte bt1 = Byte.valueOf(binary);
byte bt1 = Byte.valueOf("7");
byte bt2 = Byte.valueOf("127");
// bt2 = Byte.valueOf(hex);
System.out.printf("bt1=%d, bt2=%d %n", bt1, bt2);
}
}
------------------------------------------------------------------------------------------
package chap07;
public class Lamdaclass {
public static void main(String[] args) {
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 10; i++)
System.out.println(i);
}
}).start();
new Thread(() -> {
for (int i = 0; i < 10; i++)
System.out.println("test");
}).start();
// () -> {
// System.out.println("람다식 테스트");
// }
// () -> {
// return 0.14;
// }
// (String s) -> {
// System.out.println(s);
// }
}
}
------------------------------------------------------------------------------------------
package chap07;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
public class LamdaStream {
public static void main(String[] args) {
List<Integer> ar = Arrays.asList(1, 2, 3, 4, 5);
System.out.println(ar);
ar.forEach(System.out::println);
ar.stream().forEach(System.out::println);
List ar2 = Arrays.asList("?���?", "?���?", "?��?��", "?���?");
System.out.println(ar2);
Arrays.asList(1, 2, 3, 4, 5).forEach(System.out::println);
ar2.stream().forEach(System.out::println);
System.out.println("--------");
ar2.parallelStream().forEach(System.out::println);
// map ?��?�� Lamda ?��?��?��(Expression)
ar2.stream().map(str -> str + " ?��?��?��").forEach(System.out::println);
System.out.println("--------");
// ar.stream().map(a -> a+a);
ar.stream().map(i -> i + "번째").forEach(System.out::println);
// ar2.parallelStream().
// Limit ?��?�� Lamda ?��?��?��(Expression)
ar.stream().limit(1).forEach(System.out::println);
ar2.stream().limit(3).forEach(System.out::println);
// ar2.add(new String("바나?��"));
// ar.add(10);
ar.stream().forEach(System.out::print);
System.out.println("-----------");
ar2.stream().forEach(System.out::println);
System.out.println("-----------");
// Filter ?��?�� Lamda ?��?��?��(Expression)
ar2.stream().filter(str -> "?���?" == str).forEach(System.out::println);
System.out.println("-----------");
ar.stream().filter(a -> a >= 2).forEach(System.out::println);
System.out.println("-----------");
ar.parallelStream().filter(a -> a >= 2).forEach(System.out::println);
System.out.println("-----------");
List<String> al = new ArrayList();
List<String> ar3 = new ArrayList();
al.add("바나?��");
al.add("?���?");
ar3.add("바나?��");
ar3.add("?���?");
System.out.println(al);
al.stream().forEach(System.out::println);
System.out.println("-----------");
al.stream().filter(s -> s == "?���?").forEach(System.out::println);
System.out.println("-----------");
al.addAll(ar3);
System.out.println(al);
ar2 = Arrays.asList(Arrays.asList(1, 2, 3),
Arrays.asList(4, 5, 6),
Arrays.asList(7, 8, 9));
System.out.println(ar2);
ar2.stream().forEach(System.out::println);
Arrays.asList(Arrays.asList(1, 2, 3),
Arrays.asList(4, 5, 6),
Arrays.asList(7, 8, 9)).stream()
.flatMap(s -> s.stream()).forEach(System.out::print);
System.out.println("-----------");
// reduce ?��?�� Lamda ?��?��?��(Expression)
System.out.println(Arrays.asList(1, 2, 3).stream().reduce((a, b) -> a + b).get());
System.out.println("-----------");
System.out.println(al.stream().reduce((a, b) -> a + b).get());
System.out.println("-----------");
// Collection ?��?�� Lamda ?��?��?��(Expression)
System.out.println(al.stream().collect(Collectors.toList()));
Iterator it = al.stream().iterator();
for (int i = 0; it.hasNext(); i++) {
it.next();
System.out.println(i);
System.out.println(it.toString());
}
System.out.println("--------");
System.out.println(it);
}
}
------------------------------------------------------------------------------------------
package chap07;
public class LamdaTest {
public static void main(String[] args) {
Func add = (int a, int b) -> a + b;
System.out.println(add);
System.out.println(add.calc(10, 20));
}
}
interface Func {
public int calc(int a, int b);
}
------------------------------------------------------------------------------------------
package chap07;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
public class ListDemo {
static final int N = 10000;
static List<Integer> values;
static {
Integer vals[] = new Integer[N];
Random rn = new Random();
for (int i = 0, currval = 0; i < N; i++) {
vals[i] = new Integer(currval);
currval += rn.nextInt(100) + 1;
}
values = Arrays.asList(vals);
}
static long timeList(List<Integer> list) {
long start = System.currentTimeMillis();
for (int i = 0; i < N; i++) {
int index = Collections.binarySearch(list, values.get(i));
if (index != i)
System.out.println("*** error ***|n");
}
return (System.currentTimeMillis() - start);
}
public static void main(String[] args) {
System.out.println("time for ArrayList =" + timeList(new ArrayList<Integer>(values)));
System.out.println("time for LinkedList =" + timeList(new LinkedList<Integer>(values)));
}
}
------------------------------------------------------------------------------------------
package chap07;
public class MathClass {
public static void main(String[] args) {
// 1) y=x^2+2x+1 double x,y
double x = 3, y = 0;
y = Math.pow(x, 2) + 2 * x + 1;
y = Math.round(y * 10) / 10;
System.out.printf("x=%f일때 y=%f %n", x, y);
// 2) r(반지름) 원의 둘레를 구하기 double r
double r = 5;
y = 2 * Math.PI * r;
y = (int) (Math.round(y * 10)) / 10;
System.out.printf("반지름r=%f일때 y=%f %n", r, y);
double c = 3.124, d = 3.524;
System.out.printf("c=%f, d=%f %n", c, d);
c = Math.floor(c);
d = Math.floor(d);
System.out.printf("c=%f, d=%f %n", c, d);
c = 3.164;
d = 3.527;
c = Math.round(c);
d = Math.round(d);
System.out.printf("c=%f, d=%f %n", c, d);
c = 3.164;
d = 3.527;
c = Math.round(c * 10) / 10.0;
d = Math.round(d * 100) / 100.0;
System.out.println("c= " + c);
System.out.println("d= " + d);
c = 3.164;
d = 3.527;
c = Math.ceil(c);
d = Math.ceil(d);
System.out.printf("c=%f, d=%f %n", c, d);
System.out.printf("c=%f, d=%f 작은값:%f 큰값:%f %n",
c, d, Math.min(c, d), Math.max(c, d));
for (double k = 0; k < 1; k += 0.01) {
System.out.printf("x=%f => sin(x)=%f%n", k, Math.sin(k));
}
for (double k = 0; k < 1; k += 0.01) {
System.out.printf("x=%f => cos(x)=%f%n", k, Math.cos(k));
}
}
}
------------------------------------------------------------------------------------------
package chap07;
public class ObjectClass {
public static void main(String[] args) {
Object1 ob1;
Object2 ob2;
ob1 = new Object1();
ob2 = new Object2();
System.out.println(ob1);
System.out.println(ob2);
System.out.println(ob1.toString());
String s1 = "대한민국";
boolean b = ob1.equals(ob2);
boolean b1 = ob1.equals(ob1);
boolean b2 = "대한민국".equals("대한민국");
System.out.printf("b=%s,b1=%s,b2=%s %n", b, b1, b2);
// b2='a.equals'('a');
b2 = 'a' == 'a';
// b2=5.equals(5);
b2 = 5 == 5;
b2 = b1 == b1;
b2 = s1.equals("대한민국1");
System.out.printf("b=%s,b1=%s,b2=%s %n", b, b1, b2);
Class c1, c2;
c1 = ob1.getClass();
c2 = ob2.getClass();
System.out.printf("c1=%s, c2=%s %n", c1.getName(), c2.getName());
System.out.println(ob1 instanceof Object1);
System.out.println(ob1 instanceof Object);
// System.out.println(c1 instanceof Object1);
System.out.println(c1 instanceof Object);
}
}
class Object1 extends Object {
public String toString() {
return "Object 객체";
}
}
class Object2 {
}
------------------------------------------------------------------------------------------
package chap07;
import java.util.Random;
public class RandomClass {
public static void main(String[] args) {
Random rn = new Random();
for (int i = 0; i < 10; i++) {
int r = rn.nextInt() + 1;
System.out.println(r);
}
for (int i = 0; i < 10; i++) {
int r = rn.nextInt(10 - 3 + 1) + 3;// nextInt공식 (max - min +1)+min
System.out.println(r);
}
for (int i = 0; i < 10; i++) {
int r = rn.nextInt(3);// 2-0+1 + 0
System.out.println(r);
}
}
}
------------------------------------------------------------------------------------------
package chap07;
public class Sorting {
public static void main(String[] args) {
String st[] = { "이몽룡", "성춘향", "홍길동", "강감찬" };
for (String s : st)
System.out.println(s);
st = sort(st);
System.out.println("----------------------");
for (String s : st)
System.out.println(s);
}
public static String[] sort(String st[]) {
for (int i = 0; i < st.length - 1; i++) {
// System.out.println("----------------------");
// System.out.println("i보기 : " + i);
for (int j = i + 1; j < st.length; j++) {
// System.out.println("j보기 : " + j);
if (st[i].compareTo(st[j]) > 0) {
String s = st[i];
// System.out.println("방 st[i] : " + st[i]);
st[i] = st[j];
// System.out.println("방 st[j] : " + st[j]);
st[j] = s;
// System.out.println("방 s : " + s);
}
}
}
return st;
}
}
------------------------------------------------------------------------------------------
package chap07;
public class StringBufferClass {
public static void main(String[] args) {
StringBuffer sb1, sb2, sb3;
String s1, s2, s3;
s1 = "사과";
s2 = s1;
s3 = s2;
// sb1="딸기";
sb1 = new StringBuffer("사과");
sb2 = new StringBuffer("딸기");
sb3 = new StringBuffer("바나나");
System.out.printf("s1=%s, s2=%s, s3=%s%n", s1, s2, s3);
System.out.printf("sb1=%s, sb2=%s, sb3=%s%n", sb1, sb2, sb3);
System.out.printf("sb1=%s, sb2=%s, sb3=%s%n",
sb1.toString(), sb2.toString(), sb3.toString());
s1.concat(" 한 상자");
s2 = s1.concat(" 한 상자");
sb1.append(" 한 상자");// append는 뒤에 추가가되는데
sb2.append(" 한 상자");
System.out.printf("s1=%s, s2=%s, s3=%s%n", s1, s2, s3);
System.out.printf("sb1=%s, sb2=%s, sb3=%s%n", sb1, sb2, sb3);
sb3.insert(0, " 한 상자");// insert는 앞에 추가가 된다.
sb3.insert(sb3.length(), " 한 상자");
System.out.printf("sb1=%s, sb2=%s, sb3=%s%n", sb1, sb2, sb3);
sb3.delete(0, 4);
System.out.printf("sb1=%s, sb2=%s, sb3=%s%n", sb1, sb2, sb3);
sb3.delete(0, 1);
System.out.printf("sb1=%s, sb2=%s, sb3=%s%n", sb1, sb2, sb3);
sb1.reverse();
sb2.reverse();
System.out.printf("sb1=%s, sb2=%s, sb3=%s%n", sb1, sb2, sb3);
sb1.reverse();
sb2.reverse();
sb1.replace(0, 2, "수박");
sb2.replace(0, 2, "토마토");
sb3.replace(0, 3, "참외");
System.out.printf("sb1=%s, sb2=%s, sb3=%s%n", sb1, sb2, sb3);
sb1.deleteCharAt(1);
System.out.printf("sb1=%s, sb2=%s, sb3=%s%n", sb1, sb2, sb3);
System.out.printf("sb1=%s, sb2=%s, sb3=%s%n", sb1, sb2, sb3);
for (int i = 0; i < sb1.length(); i++) {
System.out.println(sb1.charAt(i));
}
for (int i = 0; i < sb1.length(); i++) {
System.out.println(sb1.indexOf("한"));
}
System.out.println(sb2);
s2 = deleteString(sb2, "토");
System.out.println(sb2);
}
public static String deleteString(StringBuffer sb, String del) {
int k;
for (int i = 0; i < sb.length(); i++) {
if ((k = sb.indexOf(del)) >= 0) {
sb.deleteCharAt(k);
}
}
return sb.toString();
}
}
------------------------------------------------------------------------------------------
package chap07;
public class StringClass {
String s1, s2, s3, s4;
public static void main(String[] args) {
StringClass a = new StringClass();
String st1, st2, st3, st4;
st1 = st2 = st3 = st4 = null;
System.out.printf("%s, %s, %s, %s %n", st1, st2, st3, st4);
System.out.printf("%s, %s, %s, %s %n", a.s1, a.s2, a.s3, a.s4);
st1 = st2 = st3 = st4 = "우리나라";
a.s1 = a.s2 = a.s3 = a.s4 = "우리나라";
System.out.printf("%s, %s, %s, %s %n", st1, st2, st3, st4);
System.out.printf("%s, %s, %s, %s %n", a.s1, a.s2, a.s3, a.s4);
st1 = new String("우리나라");
System.out.println(st1.equals(st2));
System.out.println(st3.equals(st2));
System.out.println(st1 == st2);
System.out.println(st3 == st2);
char ch[] = { '우', '리', '나', '라' };
st2 = new String(ch, 2, 2);
st2 = new String(ch, 0, ch.length);
System.out.printf("%s, %s, %s, %s %n", st1, st2, st3, st4);
System.out.println(st1.equals(st2));
System.out.println(st1.equals(st3));
a.s1 = "A";
a.s2 = "a";
System.out.println(a.s1.compareTo(a.s2));
// 앞을 기준으로 뒤에꺼가 작은면 양수 반대로는 음수.
System.out.println(a.s2.compareTo(a.s1));
System.out.println(a.s1.compareTo(a.s1));
a.s1 = "Aa";
a.s2 = "Ab";
System.out.println(a.s1.compareTo(a.s2));
System.out.println(a.s2.compareTo(a.s1));
System.out.println(st1.compareTo(st2));
st2 = st1.concat("대한민국");
System.out.println(st2.concat("대한민국"));
System.out.println(st1);
System.out.println(st2);
System.out.printf("%s:길이=%d,%s:길이=%d%n", st1, st1.length(), st2, st2.length());
st1 = st1.trim();
st2 = st2.trim();
System.out.printf("%s:길이=%d,%s:길이=%d%n", st1, st1.length(), st2, st2.length());
}
}
------------------------------------------------------------------------------------------
package chap07;
enum Grade2 {
grade1("1학년"), grade2("2학년"), grade3("3학년"), grade4("4학년");
private final String name;
private Grade2(String name) {
this.name = name;
}
public String getNmae() {
return name;
}
}
public class Student {
public String name;
public Grade2 grade;
public static void main(String[] args) {
for (Grade2 grade : Grade2.values()) {
System.out.println(grade);
System.out.println(grade.ordinal());
System.out.println(grade.getNmae());
}
}
}
------------------------------------------------------------------------------------------
package chap07;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Inherited
@Documented
@Retention(RetentionPolicy.RUNTIME)
// @Retention(RetentionPolicy.SOURCE)
// @Retention(RetentionPolicy.CLASS)
@Target({ ElementType.PACKAGE, ElementType.CONSTRUCTOR, ElementType.FIELD,
ElementType.LOCAL_VARIABLE, ElementType.PARAMETER, ElementType.TYPE_PARAMETER,
ElementType.METHOD, ElementType.ANNOTATION_TYPE, ElementType.TYPE,
ElementType.TYPE_USE, })
public @interface UserAnnotation {
public enum Grade {
GRADE1, GRADE2, GRADE3, GRADE4,
}
Grade grade()
default Grade.GRADE1;
String[] value() default { "대한", "민국" };
int print() default 1;
}
------------------------------------------------------------------------------------------
package chap07;
import java.lang.reflect.Field;
import chap07.UserAnnotation.Grade;
public class UserAnnotationClass {
@UserAnnotation(value = { "독립", "만세" })
String[] st;
@UserAnnotation(grade = Grade.GRADE1)
Grade grade;
@UserAnnotation(print = 10)
int a;
public static void main(String[] args) throws Exception {
UserAnnotationClass uac = new UserAnnotationClass();
UserAnnotation ua = null;
Field[] fields = uac.getClass().getDeclaredFields();
System.out.println(fields.length);
ua = fields[0].getAnnotation(UserAnnotation.class);
System.out.println(ua.value().length);
System.out.printf("%s, %s%n", ua.value()[0], ua.value()[1]);
ua = fields[1].getAnnotation(UserAnnotation.class);
System.out.println(ua.grade());
ua = fields[2].getAnnotation(UserAnnotation.class);
System.out.println(ua.print());
}
}
------------------------------------------------------------------------------------------
package chap07;
import java.util.List;
import java.util.Random;
import java.util.Vector;
public class VectorClass {
public static void main(String[] args) {
Vector<NumberSet> v1, v2, v3;
List<NumberSet> li1, li2;
li1 = new Vector();
li2 = new Vector(3);
v1 = new Vector<NumberSet>();
v2 = new Vector<NumberSet>(3);
System.out.printf("v1(size):%d, v2(size):%d%n", v1.size(), v2.size());
System.out.printf("li1(size):%d, li2(size):%d%n", li1.size(), li2.size());
v1.add(new NumberSet());
for (int i = 0; i < 10; i++) {
v2.addElement(new NumberSet());
} // Vector에서 add 로 추가 하나 addElement를 추가하나 결과는 같다.
System.out.printf("v1(size):%d, v2(size):%d%n", v1.size(), v2.size());
for (int i = 0; i < v2.size(); i++) {
System.out.printf("v1(0):%d, v2(%d):%d%n", ((NumberSet) v1.get(0)).getNum(), i,
((NumberSet) v2.get(i)).getNum());// v1, v2 : 난수값
System.out.printf("v1(0):%d, v2(%d):%d%n", v1.get(0).getNum(), i, v2.get(i).getNum());
// v1, v2 : 난수값
if (v2.contains(v1.get(0))) {
System.out.printf("v1(0)=v2(%d) : %s%n", i, v1.get(i).equals(v2.get(i)));
} else {
System.out.printf("v2(%d):%d%n", i, v2.get(i).getNum());// 난수 값
}
if (v2.indexOf(v1.get(0)) >= 0) {
System.out.printf("indexOf : v2(%d) : %d%n", i, v2.get(i).getNum());
}
}
li1 = v1;
li1.addAll(v2);
System.out.println(li1);
System.out.printf("v1:size: %d. li1:size: %d%n", v1.size(), li1.size());
li1.remove(0);
li1.remove(v2.get(0));
li1.removeAll(v2);
System.out.printf("v1:size: %d. li1:size: %d%n", v1.size(), li1.size());
System.out.printf("v2(%d):%d%n", 0, v2.get(0).getNum());
System.out.println(v2);
v2.set(2, new NumberSet(7));
v2.set(5, new NumberSet(9));// NumberSet는 값을 정해준다는 뜻
System.out.println(v2);
}
}
class NumberSet {
private int num;
public String toString() {
return String.valueOf(num);
}
public NumberSet() {
num = new Random().nextInt(3);
}
public NumberSet(int num) {
super();
this.num = num;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
------------------------------------------------------------------------------------------
package chap07;
public class WrapperClass {
public static void main(String[] args) {
Boolean b;
b = new Boolean(false);
Byte b1;
b1 = new Byte("5");
Short s1;
s1 = new Short("5");
Character c1;
c1 = 'A';
Integer i1;
i1 = 7;
Long l1;
l1 = 8l;
Float f1;
f1 = 3.4f;
Double d1;
d1 = 5.12;
boolean b2;
b2 = b.booleanValue();
byte b3;
b3 = b1.byteValue();
short s2;
s2 = s1.shortValue();
char c2;
c2 = c1.charValue();
int i2;
i2 = i1.intValue();
long l2;
l2 = l1.longValue();
float f2;
f2 = f1.floatValue();
double d2;
d2 = d1.doubleValue();
System.out.printf("byte 범위%d ~%d %n", Byte.MIN_VALUE, Byte.MAX_VALUE);
System.out.printf("short 범위%d ~%d %n", Short.MIN_VALUE, Short.MAX_VALUE);
System.out.printf("char 범위%s ~%s %n", Character.MIN_VALUE, Character.MAX_VALUE);
System.out.printf("int 범위%d ~%d %n", Integer.MIN_VALUE, Integer.MAX_VALUE);
System.out.printf("long 범위%d ~%d %n", Long.MIN_VALUE, Long.MAX_VALUE);
System.out.printf("float 범위%f ~%f %n", Float.MIN_VALUE, Float.MAX_VALUE);
System.out.printf("double 범위%f ~%f %n", Double.MIN_VALUE, Double.MAX_VALUE);
double db = Double.compare(3.14, 3.15);
double db1 = Double.sum(3.14, 3.15);
System.out.printf("%f, %f => %f %n", 3.14, 3.15, db);
System.out.printf("%f, %f => %f %n", 3.14, 3.15, db1);
}
}
------------------------------------------------------------------------------------------ |
cs |
'Legend 개발자 > Java' 카테고리의 다른 글
Read Code(Chap.09) (0) | 2017.07.20 |
---|---|
Read Code(Chap.08) (0) | 2017.07.20 |
Read Code(Chap.06) (0) | 2017.07.20 |
Read Code(Chap.05) (0) | 2017.07.20 |
Read Code(Chap.04) (0) | 2017.07.20 |