Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Collections/.project
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
<filteredResources>
<filter>
<id>1647954731126</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import java.util.Map;
import java.util.Set;

public class CollectionsDemo {
public class CollectionsDemo
{
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("One");
Expand Down Expand Up @@ -51,14 +51,14 @@ public static void main(String[] args) {
String min = Collections.min(list);
System.out.println("Min Element in the List : "+min);

List<Integer> iList = new ArrayList<>();
iList.add(10);
iList.add(20);
iList.add(40);
iList.add(30);
List<Integer> indexList = new ArrayList<>(); //iList name is not understadable can be converted to List 2 or IndexList
indexList.add(10);
indexList.add(20);
indexList.add(40);
indexList.add(30);

//Binary Search in List Collection
int index = Collections.binarySearch(iList, 190);
int index = Collections.binarySearch(indexList, 190);
System.out.println("Binary Search of 190 index is : "+index);

//Copy one list to another list
Expand All @@ -71,36 +71,60 @@ public static void main(String[] args) {
newList.add(14);
newList.add(15);
//newList should have minimum size as iList
Collections.copy(newList, iList);
Collections.copy(newList, indexList);
System.out.println("Copying the list from another list : "+newList);

//Creating Immutable Collection
Set<String> emptySet = Collections.emptySet();
List<String> emptyList = Collections.emptyList();
Map<String, String> emptyMap = Collections.emptyMap();
System.out.println("Creating immutable collection (list, set, map)");
System.out.println("Empty Set : "+emptySet.size());
//emptySet.add("Try"); //not allowed its immutable

//replacing all the elements with the new value
Collections.replaceAll(iList, 10, 100);
System.out.println("Replacing all the 10 in the list with 100 :");
System.out.println(iList);

//shuffling the list
Collections.shuffle(iList);
System.out.println("Shuffle the Elements in the List : ");
System.out.println(iList);


methodCalling(indexList); //Calling all methods from one place

//Creating singleton Collection Set, List, Map
Set<String> singletonSet = Collections.singleton("Java");
System.out.println("Creating singleton Collection : ");
System.out.println(singletonSet);
// singletonSet.add("Hello"); //not supported its immutable
}

static void methodCalling(List<Integer> indexList)
{
immutableCollection(); // Extracted Method
replaceAndShuffle(indexList); // Extracted Method
singletonCollection(); // Extracted Method
synchronizedCollection(); // Extracted Method
}
static void immutableCollection()
{
//Creating Immutable Collection
Set<String> emptySet = Collections.emptySet();
List<String> emptyList = Collections.emptyList();
Map<String, String> emptyMap = Collections.emptyMap();
System.out.println("Creating immutable collection (list, set, map)");
System.out.println("Empty Set : "+emptySet.size());
//emptySet.add("Try"); //not allowed its immutable
}

static void replaceAndShuffle(List<Integer> indexList)
{
//replacing all the elements with the new value
Collections.replaceAll(indexList, 10, 100);
System.out.println("Replacing all the 10 in the list with 100 :");
System.out.println(indexList);

//shuffling the list
Collections.shuffle(indexList);
System.out.println("Shuffle the Elements in the List : ");
System.out.println(indexList);
}

static void singletonCollection()
{
//Creating singleton Collection Set, List, Map
Set<String> singletonSet = Collections.singleton("Java");
//singletonSet.add("Hello"); //not supported its immutable
System.out.println("Creating singleton Collection : ");
System.out.println(singletonSet);
}

//Creating synchronized Collection List, Set, Map
Map<Integer, String> map = new HashMap<>();
map = Collections.synchronizedMap(map);
System.out.println("Creating synchronizing Collection : ");
static void synchronizedCollection()
{
//Creating synchronized Collection List, Set, Map
Map<Integer, String> map = new HashMap<>();
map = Collections.synchronizedMap(map);
System.out.println("Creating synchronizing Collection : ");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

import java.util.ArrayList;

public class EmployeeRunnable implements Runnable{
public class EmployeeRunnable
{

ArrayList<String> names;

public EmployeeRunnable(ArrayList<String> list) {
this.names = list;
}

@Override
public void run() {
for(int i=0;i<100;i++)
names.add("Employee :: "+i);
}
}

}
//run method moved to MainProgram.java
69 changes: 56 additions & 13 deletions Collections/src/com/cdac/collections/threadsafe/MainProgram.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,65 @@
package com.cdac.collections.threadsafe;

import java.util.ArrayList;
// import com.cdac.collections.bean.Student;
// Applying move method run to main program, decreases the complexity of tread running
public class MainProgram implements Runnable
{
public static void main(String[] args) throws CloneNotSupportedException {

import com.cdac.collections.bean.Student;

public class MainProgram {
public static void main(String[] args) throws CloneNotSupportedException {
ArrayList<String> list = new ArrayList<>();
EmployeeRunnable employeeRunnable = new EmployeeRunnable(list);
StudentRunnable studentRunnable = new StudentRunnable(list);

Thread t1 = new Thread(employeeRunnable);
Thread t2 = new Thread(studentRunnable);
Thread t1 = new Thread();
Thread t2 = new Thread();

t1.start();
t2.start();
ArrayList<String> list = new ArrayList<>();
EmployeeRunnable employeeRunnable = new EmployeeRunnable(list);
StudentRunnable studentRunnable = new StudentRunnable(list);



}
public void run()
{
ArrayList<String> Employeenames = null;
ArrayList<String> Studentnames = null;
for(int i=0;i<100;i++)
{
Employeenames.add("Employee :: "+i);
}
for(int j =100; j<200;j++)
{
Studentnames.add("Student :: "+j);
}

}
}

}
// class EmployeeRunnable implements Runnable
// {

// ArrayList<String> names;

// public EmployeeRunnable(ArrayList<String> list) {
// this.names = list;
// }

// @Override
// public void run() {
// for(int i=0;i<100;i++)
// names.add("Employee :: "+i);
// }
// }

// class StudentRunnable implements Runnable
// {

// ArrayList<String> names;

// public StudentRunnable(ArrayList<String> list) {
// this.names = list;
// }
// @Override
// public void run() {
// for(int i=100;i<200;i++)
// names.add("Student :: "+i);
// }

Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@

import java.util.ArrayList;

public class StudentRunnable implements Runnable{

public class StudentRunnable
{
ArrayList<String> names;

public StudentRunnable(ArrayList<String> list) {
public StudentRunnable(ArrayList<String> list)
{
this.names = list;
}
@Override
public void run() {
for(int i=100;i<200;i++)
names.add("Student :: "+i);
}

}
}
//run method moved to MainProgram.java
11 changes: 11 additions & 0 deletions EncryptionDecryption/.project
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
<filteredResources>
<filter>
<id>1647954731142</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
1 change: 0 additions & 1 deletion EncryptionDecryption/bin/.gitignore

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file added EncryptionDecryption/bin/snippet/Snippet.class
Binary file not shown.
26 changes: 17 additions & 9 deletions EncryptionDecryption/src/com/cdac/encrypt/aes/AESEncryption.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,33 @@
*/
public class AESEncryption {

public static void main(String[] args) {
final String strToEncrypt = "My text to encrypt";
public static void main(String[] args)
{
final String strToEncrypt = "My text to encrypt";
final String strPssword = "f2fc2007-b24b-4ab5-b62f-8dba873d0341";
AESEncryption.setKey(strPssword);
AESEncryption.encrypt(strToEncrypt.trim());
encryptdecrypt.setKey(strPssword);
//encryptdecrypt is called from main funcation
encryptdecrypt.encrypt(strToEncrypt.trim());
System.out.println("String to Encrypt: " + strToEncrypt);
System.out.println("Encrypted: " + AESEncryption.getEncryptedString());
final String strToDecrypt = AESEncryption.getEncryptedString();
AESEncryption.decrypt(strToDecrypt.trim());
System.out.println("Encrypted: " + encryptdecrypt.getEncryptedString());
final String strToDecrypt = encryptdecrypt.getEncryptedString();
encryptdecrypt.decrypt(strToDecrypt.trim());
System.out.println("String To Decrypt : " + strToDecrypt);
System.out.println("Decrypted : " + AESEncryption.getDecryptedString());
System.out.println("Decrypted : " + encryptdecrypt.getDecryptedString());
}
}

//class encryptdecrypt increases the readability of the code. So, it is extracted to another new class.
//funcationality remains the same besides having refactoring
class encryptdecrypt
{
private static SecretKeySpec secretKey;
private static byte[] key;
private static String decryptedString;
private static String encryptedString;

public static void setKey(String myKey) {
public static void setKey(String myKey)
{
MessageDigest sha = null;
try {
key = myKey.getBytes("UTF-8");
Expand Down
6 changes: 4 additions & 2 deletions EncryptionDecryption/src/snippet/Snippet.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package snippet;

public class Snippet {
Divisors are
public class Snippet
{
//Divisors are
}

//changed snippet.java as code is giving error
11 changes: 11 additions & 0 deletions ExceptionHandling/.project
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
<filteredResources>
<filter>
<id>1647954731166</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
11 changes: 11 additions & 0 deletions FileIO/.project
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
<filteredResources>
<filter>
<id>1647954731181</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
11 changes: 11 additions & 0 deletions Generics/.project
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
<filteredResources>
<filter>
<id>1647954731191</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
Loading