Skip to content

Commit 7516a04

Browse files
committed
Core Java
1 parent 499bdd2 commit 7516a04

File tree

8 files changed

+134
-10
lines changed

8 files changed

+134
-10
lines changed

Collections/src/com/cdac/collections/bean/Student.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.cdac.collections.bean;
22

3-
public class Student implements Comparable<Student>{
3+
public class Student implements Comparable<Student>,Cloneable{
44

55
private int rollNo;
66
private String name;
@@ -53,4 +53,9 @@ public int compareTo(Student o) {
5353
//DECREASING ORDER BY ROLL NO
5454
// return o.rollNo - this.rollNo ;
5555
}
56+
57+
@Override
58+
public Object clone() throws CloneNotSupportedException {
59+
return super.clone();
60+
}
5661
}

Collections/src/com/cdac/collections/collectionsutils/CollectionsDemo.java

+24-6
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,41 @@ public static void main(String[] args) {
1515
list.add("One");
1616
list.add("Two");
1717
list.add("One");
18+
System.out.println("Initial List");
19+
System.out.println(list);
1820

1921
Set<String> set = new HashSet<>();
2022
set.add("Three");
2123
set.add("Four");
2224
set.add("One");
23-
25+
System.out.println("Initial Set");
2426
System.out.println(set);
27+
28+
//Adding all list elements to Set Collection
2529
set.addAll(list);
30+
System.out.println("Adding list to set collection");
2631
System.out.println(set);
2732

33+
//Adding array values to List Collection
34+
String strArray[] = {"ABC","acb"};
35+
Collections.addAll(list, strArray);
36+
System.out.println("Adding Array values to List collection ");
37+
System.out.println(list);
38+
39+
//sorting the List Collection
2840
Collections.sort(list);
29-
System.out.println("Sort : "+list);
41+
System.out.println("Sorting the List : ");
42+
System.out.println(list);
43+
44+
//sorting the list with comparator - reverse order
3045
Collections.sort(list, Collections.reverseOrder());
31-
System.out.println("Reverse Order : "+list);
46+
System.out.println("Reverse Order : ");
47+
System.out.println(list);
3248

3349
String max = Collections.max(list);
34-
System.out.println("Max : "+max);
50+
System.out.println("Max Element in the List : "+max);
3551
String min = Collections.min(list);
36-
System.out.println("Min : "+min);
52+
System.out.println("Min Element in the List : "+min);
3753

3854
List<Integer> iList = new ArrayList<>();
3955
iList.add(10);
@@ -69,5 +85,7 @@ public static void main(String[] args) {
6985

7086
Map<Integer, String> map = new HashMap<>();
7187
map = Collections.synchronizedMap(map);
88+
89+
7290
}
73-
}
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.cdac.collections.threadsafe;
2+
3+
import java.util.ArrayList;
4+
5+
public class EmployeeRunnable implements Runnable{
6+
7+
ArrayList<String> names;
8+
9+
public EmployeeRunnable(ArrayList<String> list) {
10+
this.names = list;
11+
}
12+
13+
@Override
14+
public void run() {
15+
for(int i=0;i<100;i++)
16+
names.add("Employee :: "+i);
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.cdac.collections.threadsafe;
2+
3+
import java.util.ArrayList;
4+
5+
import com.cdac.collections.bean.Student;
6+
7+
public class MainProgram {
8+
public static void main(String[] args) throws CloneNotSupportedException {
9+
ArrayList<String> list = new ArrayList<>();
10+
EmployeeRunnable employeeRunnable = new EmployeeRunnable(list);
11+
StudentRunnable studentRunnable = new StudentRunnable(list);
12+
13+
Thread t1 = new Thread(employeeRunnable);
14+
Thread t2 = new Thread(studentRunnable);
15+
16+
t1.start();
17+
t2.start();
18+
19+
20+
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.cdac.collections.threadsafe;
2+
3+
import com.cdac.collections.bean.Student;
4+
5+
public class ShallowCopy_DeepCopy {
6+
public static void main(String[] args) throws CloneNotSupportedException {
7+
//Shallow copy
8+
Student s1 = new Student(101, "Atul", 80);
9+
Student s2 = s1;
10+
s2.setMarks(85);
11+
System.out.println("Shallow Copy");
12+
System.out.println(s2.getMarks()+" "+s1.getMarks());
13+
14+
Student s3 = (Student) s1.clone();
15+
s3.setMarks(90);
16+
System.out.println("Deep Copy");
17+
System.out.println(s3.getMarks() +" "+s1.getMarks());
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.cdac.collections.threadsafe;
2+
3+
import java.util.ArrayList;
4+
5+
public class StudentRunnable implements Runnable{
6+
7+
ArrayList<String> names;
8+
9+
public StudentRunnable(ArrayList<String> list) {
10+
this.names = list;
11+
}
12+
@Override
13+
public void run() {
14+
for(int i=100;i<200;i++)
15+
names.add("Student :: "+i);
16+
}
17+
18+
}

FileIO/src/com/cdac/fileio/bytestream/CharacterStream.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
import java.io.FileReader;
55
import java.io.FileWriter;
66
import java.io.IOException;
7+
import java.nio.CharBuffer;
78

89
public class CharacterStream {
910

1011
public static void main(String[] args) throws IOException {
11-
String content = "Hi Good morning!";
12+
String content = "Hi -1 Good morning!";
1213
writeToFile(content);
1314
readFromFile();
1415
}
@@ -35,11 +36,14 @@ public static void readFromFile(){
3536
FileReader fileReader = null;
3637
try {
3738
fileReader = new FileReader(filePath);
38-
int i;
39+
/*int i;
3940
while ( (i = fileReader.read()) != -1){
4041
char ch = (char) i;
4142
System.out.print(ch);
42-
}
43+
}*/
44+
CharBuffer buffer = null;
45+
fileReader.read(buffer);
46+
System.out.println(buffer.toString());
4347
} catch (FileNotFoundException e) {
4448
e.printStackTrace();
4549
} catch (IOException e) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.cdac.fileio.bytestream;
2+
3+
import java.io.FileReader;
4+
import java.io.FileWriter;
5+
import java.io.IOException;
6+
import java.io.InputStreamReader;
7+
import java.io.Reader;
8+
9+
public class TestErroStream {
10+
public static void main(String[] args) throws IOException {
11+
12+
int b = System.in.read();
13+
14+
for(int i=0;i<5;i++){
15+
System.out.println("Hello");
16+
System.out.flush();
17+
System.err.println("Error");
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)