├── Diff web app and dist app.jpg ├── abstrInterference ├── AB.java ├── Abstract2.java ├── Ads.java ├── Ads2.java ├── Defaultmethod.java ├── Interf18.java ├── MultipleInheriStatic.java ├── MultipleInheritance.java └── StaticMethod.java ├── arrayMethods ├── ArrayPrintingDemo.java ├── ArraySortDEmo.java ├── ArrayofStrings.java ├── ArraysDottoString.java ├── BinarySearch.java ├── BinaryString.java ├── ComparatorForArray.java ├── CompareArrays.java ├── ContainsInArray.java ├── CopyOfArray.java ├── LinearSearch.java ├── PairSearch.java ├── StringToArray.java └── commaSeparatedStringToArray.java ├── assertion └── AssertDemo.java ├── bruteForceAlgorithm └── FindPair.java ├── classProperties └── ClassFields.java ├── codility ├── BinaryGap.java ├── CyclicRotation.java ├── CyclicRotation1.java ├── FrogJmp.java ├── FrogJmp2.java ├── FrogRiverOne.java ├── MissingInteger.java ├── MissingInteger1.java ├── OddOccurrencesInArray.java ├── OddOccurrencesInArray1.java ├── PermutationCheck.java ├── PermutationMissingElement.java ├── PermutationMissingElement1.java ├── Practice.java ├── TapeEquilibrium.java └── package-info.java ├── collection ├── AddArray.java ├── ArrayList1.java ├── ArrayToList.java ├── BinarySearch.java ├── ComparatorDemo.java ├── HashMapDemo.java ├── HashSetDemo.java ├── ReadingArrayList.java └── TreeSetDemo.java ├── factorymethods └── UnmodifiableCollection.java ├── generics ├── TestGen.java └── TestGen2.java ├── immutable ├── ImmutableDemo.java └── ImmutableStudent.java ├── immutableVsFinal └── ImmutableVsFinal.java ├── input ├── ArrayInputDemo.java ├── BufferedInput.java ├── ScannerDemo.java ├── ToCheckInput.java └── TwoDArrayInout.java ├── java8 └── ForEach.java ├── practice └── StringDemo.java ├── predicate ├── PredCollDemo.java └── PredicateDemo.java ├── projects ├── ExtractMailIDFrmFile.java └── ExtractMobFrmFile.java ├── regex ├── RegFileNames.java ├── RegJavaIdentifier.java ├── RegMailID.java ├── RegMobNum.java ├── Regex1.java └── RegexDemo.java ├── singleton ├── RuntimeFactoryMethod.java ├── Singleton.java ├── Singleton2.java └── TestSingleton.java ├── stream └── StreamDemoList.java ├── validation ├── Validation.java └── ValidationOld.java ├── vararg ├── HeapPollution.java └── VarArg.java └── wrapper ├── package-info.java └── wrapper.java /Diff web app and dist app.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kendalerishi/Java/2fabdbf7f1aa7d87fc4bbe9c0e107ca201105e79/Diff web app and dist app.jpg -------------------------------------------------------------------------------- /abstrInterference/AB.java: -------------------------------------------------------------------------------- 1 | package abstrInterference; 2 | 3 | abstract class Abstract1 4 | { 5 | Abstract1(){System.out.println("DEF");}; 6 | private static String a1() 7 | {return "Hi a1";} 8 | 9 | abstract void a2(); 10 | /*{ 11 | 12 | } 13 | */ 14 | public static String goa(){System.out.println("Goa1");return "goa";} 15 | 16 | } 17 | 18 | public class AB extends Abstract1 19 | { 20 | AB(){super.goa(); 21 | this.goa(); 22 | } 23 | 24 | public static String goa(){System.out.println("Goa2");return "jik";} 25 | void a2(){} 26 | //public void a3(){ System.out.println("Hji");} 27 | 28 | public static void main(String[] args) 29 | { 30 | AB ab = new AB(); 31 | 32 | //goa(); 33 | //System.out.println(goa()); 34 | 35 | } 36 | } -------------------------------------------------------------------------------- /abstrInterference/Abstract2.java: -------------------------------------------------------------------------------- 1 | package abstrInterference; 2 | 3 | abstract class C 4 | { 5 | abstract int a1(); 6 | abstract int a3(); 7 | void a4() 8 | {} 9 | } 10 | 11 | abstract class A extends C 12 | { 13 | abstract int a2(); 14 | int a1() 15 | { 16 | return 10; 17 | } 18 | 19 | int a3() 20 | { 21 | return 10; 22 | } 23 | 24 | abstract void a4(); 25 | } 26 | 27 | class Abstract2 extends A 28 | { 29 | 30 | int a2() 31 | { 32 | System.out.println("Hi"); 33 | return 0;} 34 | void a4() 35 | {} 36 | 37 | /*void a1() 38 | {}*/ 39 | 40 | public static void main(String[] args) 41 | { 42 | Abstract2 b1 = new Abstract2(); 43 | b1.a3(); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /abstrInterference/Ads.java: -------------------------------------------------------------------------------- 1 | package abstrInterference; 2 | 3 | import java.io.FileReader; 4 | import java.util.*; 5 | 6 | public class Ads 7 | { 8 | public static ArrayList data() 9 | { 10 | int[] array = new int[4]; 11 | String s1=("12, Ram, Eng, 1200"); 12 | String s2=("08, Sita, Test, 800"); 13 | String s3=("11, Shyam, Eng, 1900"); 14 | String s4=("05, Gita, Test, 350"); 15 | ArrayList inputData = new ArrayList(); 16 | inputData.add(s1); 17 | inputData.add(s2); 18 | inputData.add(s3); 19 | inputData.add(s4); 20 | return inputData; 21 | } 22 | 23 | public static void main (String[] args) 24 | { 25 | 26 | processData1(Ads.data()); 27 | 28 | 29 | } 30 | public static Map processData(ArrayList array) 31 | { 32 | 33 | ArrayList al = data(); 34 | List alId = new ArrayList(); 35 | for(int i=0;i retVal = new HashMap(); 48 | return retVal; 49 | } 50 | 51 | 52 | public static void processData1(ArrayList array) 53 | { 54 | 55 | ArrayList al = data(); 56 | List alId = new ArrayList(); 57 | for(int i=0;i alDept = new ArrayList(); 64 | for(int i=0;i alSal = new ArrayList(); 71 | for(int i=0;i hmSal = new HashMap(); 83 | //int MaxSal=Integer.parseInt(alSal.get(0)); 84 | for(int i=0;i retVal = (HashMap)processData(data1()); 11 | for(Map.Entry e: retVal.entrySet()) 12 | System.out.println(e.getKey() + ": " + e.getValue()); 13 | 14 | //processData2(data1()); 15 | } 16 | public static ArrayList data1() 17 | { 18 | int[] array = new int[4]; 19 | String s1=("129, Ram, Eng, 1200"); 20 | String s2=("080, Sita, Test, 800"); 21 | String s3=("12, Shyam, Eng, 1900"); 22 | String s4=("15, Gita, Test, 350"); 23 | ArrayList inputData1 = new ArrayList(); 24 | inputData1.add(s1); 25 | inputData1.add(s2); 26 | inputData1.add(s3); 27 | inputData1.add(s4); 28 | return inputData1; 29 | } 30 | 31 | public static Map processData(ArrayList array) 32 | { 33 | ArrayList al = data1(); 34 | List alAl = new ArrayList(); 35 | 36 | for(int i=0;i alAll = new ArrayList(); 44 | for(int i=0;imaxEngId)) 62 | { 63 | maxEngId=Integer.parseInt(alAll.get(i)); 64 | dept1=alAll.get(i+2); 65 | maxEngSal=Integer.parseInt(alAll.get(i+3)); 66 | } 67 | 68 | if(((alAll.get(i+2)).equals("Test"))&&(Integer.parseInt(alAll.get(i))>maxTestId)) 69 | { 70 | maxTestId=Integer.parseInt(alAll.get(i)); 71 | dept2=alAll.get(i+2); 72 | maxTestSal=Integer.parseInt(alAll.get(i+3)); 73 | } 74 | } 75 | System.out.println("Dept"+"\t"+"Id"+"\t"+"Salary"); 76 | System.out.println(dept1 + "\t" + maxEngId+"\t"+maxEngSal); 77 | System.out.println(dept2 + "\t" + maxTestId+"\t"+maxTestSal); 78 | 79 | 80 | HashMap retVal = new HashMap(); 81 | retVal.put(dept1, maxEngSal); 82 | retVal.put(dept2, maxTestSal); 83 | return retVal; 84 | 85 | } 86 | 87 | public static void processData2(ArrayList array) 88 | { 89 | ArrayList al = data1(); 90 | List alAl = new ArrayList(); 91 | /* This adds last array of ArrayList al to s */ 92 | // String[] s=new String[al.get(0).length()];; 93 | // for(int i=0;i alAll = new ArrayList(); 107 | for(int i=0;imaxEngId)) 128 | { 129 | maxEngId=Integer.parseInt(alAll.get(i)); 130 | dept1=alAll.get(i+2); 131 | maxEngSal=Integer.parseInt(alAll.get(i+3)); 132 | } 133 | 134 | if(((alAll.get(i+2)).equals("Test"))&&(Integer.parseInt(alAll.get(i))>maxTestId)) 135 | { 136 | maxTestId=Integer.parseInt(alAll.get(i)); 137 | dept2=alAll.get(i+2); 138 | maxTestSal=Integer.parseInt(alAll.get(i+3)); 139 | } 140 | } 141 | System.out.println("Dept"+"\t"+"Id"+"\t"+"Salary"); 142 | System.out.println(dept1 + "\t" + maxEngId+"\t"+maxEngSal); 143 | System.out.println(dept2 + "\t" + maxTestId+"\t"+maxTestSal); 144 | //System.out.println(maxEngId); 145 | //System.out.println(maxTestId); 146 | 147 | // int maxEngSal = (int)Integer.parseInt(alAll.get(3)); 148 | // int maxTestSal = (int)Integer.parseInt(alAll.get(3)); 149 | // 150 | // for(int i=2;i(Arrays.asList(name)); 38 | // for(String s:name) 39 | // { 40 | // System.out.println(s); 41 | // } 42 | // for(int s:id) 43 | // { 44 | // System.out.println(s); 45 | // } 46 | // for(String s:city) 47 | // { 48 | // System.out.println(s); 49 | // } 50 | // for(String s:dept) 51 | // { 52 | // System.out.println(s); 53 | // } 54 | // for(int s:sal) 55 | // { 56 | // System.out.println(s); 57 | // } 58 | } 59 | } -------------------------------------------------------------------------------- /arrayMethods/ArraysDottoString.java: -------------------------------------------------------------------------------- 1 | package arrayMethods; 2 | 3 | import java.util.Arrays; 4 | 5 | public class ArraysDottoString { 6 | 7 | public static void main(String[] args) 8 | { 9 | // TODO Auto-generated method stub 10 | int[] a = {1,2,3}; 11 | String s2 = Arrays.toString(a); 12 | System.out.println(s2); 13 | System.out.println(s2.length()); 14 | char[] b = {'a','b','c'}; 15 | String s3= Arrays.toString(b); 16 | System.out.println(s3); 17 | 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /arrayMethods/BinarySearch.java: -------------------------------------------------------------------------------- 1 | package arrayMethods; 2 | 3 | import java.util.Arrays; 4 | 5 | public class BinarySearch 6 | { 7 | public static void main(String[] args) 8 | { 9 | int[] array = {1,2,3,4,6,7}; 10 | int key = 7; 11 | byte[] array1 = {10,20,30,40,60,70}; 12 | byte key1 = 45; 13 | System.out.println(Arrays.binarySearch(array,key)); 14 | System.out.println(Arrays.binarySearch(array1,key1)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /arrayMethods/BinaryString.java: -------------------------------------------------------------------------------- 1 | package arrayMethods; 2 | 3 | public class BinaryString 4 | { 5 | 6 | public static void main(String[] args) 7 | { 8 | int l = 32; 9 | System.out.println(Integer.toBinaryString(l)); 10 | 11 | int m = 15; 12 | System.out.println(Integer.toBinaryString(m)); 13 | 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /arrayMethods/ComparatorForArray.java: -------------------------------------------------------------------------------- 1 | package arrayMethods; 2 | 3 | import java.util.*; 4 | import java.lang.*; 5 | import java.io.*; 6 | 7 | // A class to represent a student. 8 | class Student 9 | { 10 | int rollno; 11 | String name, address; 12 | 13 | // Constructor 14 | public Student(int rollno, String name, 15 | String address) 16 | { 17 | this.rollno = rollno; 18 | this.name = name; 19 | this.address = address; 20 | } 21 | 22 | // Used to print student details in main() 23 | public String toString() 24 | { 25 | return this.rollno + " " 26 | + this.name + " " 27 | + this.address; 28 | } 29 | } 30 | 31 | class Sortbyroll implements Comparator 32 | { 33 | 34 | // Used for sorting in ascending order of 35 | // roll number 36 | public int compare(Student a, Student b) 37 | { 38 | return a.rollno - b.rollno; 39 | } 40 | } 41 | 42 | // Driver class 43 | class ComparatorForArray 44 | { 45 | public static void main(String[] args) 46 | { 47 | Student[] arr = { new Student(111, "bbbb", "london"), 48 | new Student(131, "aaaa", "nyc"), 49 | new Student(121, "cccc", "jaipur") }; 50 | 51 | System.out.println("Unsorted"); 52 | for (int i = 0; i < arr.length; i++) 53 | System.out.println(arr[i]); 54 | 55 | Arrays.sort(arr, new Sortbyroll()); 56 | 57 | System.out.println("\nSorted by rollno"); 58 | for (int i = 0; i < arr.length; i++) 59 | System.out.println(arr[i]); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /arrayMethods/CompareArrays.java: -------------------------------------------------------------------------------- 1 | package arrayMethods; 2 | 3 | import java.util.Arrays; 4 | 5 | public class CompareArrays { 6 | public static void main(String[] args) 7 | { 8 | 9 | // Get the Array 10 | int intArr[] = { 10, 20, 15, 22, 35 }; 11 | 12 | // Get the second Array 13 | int intArr1[] = { 10, 15, 22 }; 14 | 15 | // To compare both arrays 16 | //System.out.println("Integer Arrays on comparison: " 17 | // + Arrays.compareUnsigned(intArr, intArr1)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /arrayMethods/ContainsInArray.java: -------------------------------------------------------------------------------- 1 | package arrayMethods; 2 | 3 | import java.util.Arrays; 4 | 5 | public class ContainsInArray 6 | { 7 | public static void main(String[] args) 8 | { 9 | Integer[] array = {1,2,3,4,6,7}; 10 | int key = 7; 11 | String[] array2= {"ab","cd","ef"}; 12 | String key2 = "ab"; 13 | System.out.println(Arrays.asList(array).contains(key)); 14 | System.out.println(Arrays.asList(array2).contains(key2)); 15 | } 16 | } -------------------------------------------------------------------------------- /arrayMethods/CopyOfArray.java: -------------------------------------------------------------------------------- 1 | package arrayMethods; 2 | 3 | import java.util.Arrays; 4 | 5 | public class CopyOfArray 6 | { 7 | public static void main(String[] args) 8 | { 9 | int[] array1 = {1,2,3,4,5}; 10 | System.out.println(Arrays.copyOf(array1,10)); 11 | System.out.println(Arrays.toString(Arrays.copyOf(array1, 10))); 12 | System.out.println(Arrays.toString(Arrays.copyOf(array1, 3))); 13 | int[] x = Arrays.copyOf(array1, 5); 14 | System.out.println(Arrays.toString(x)); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /arrayMethods/LinearSearch.java: -------------------------------------------------------------------------------- 1 | package arrayMethods; 2 | 3 | public class LinearSearch 4 | { 5 | 6 | public static void main(String[] args) 7 | { 8 | int[] array = {1,2,3,4,6,7}; 9 | int key = 7; 10 | int key1 = 14; 11 | System.out.println(solution(array, key)); 12 | System.out.println(solution(array, key1)); 13 | } 14 | 15 | public static String solution(int[] array, int key) 16 | { 17 | for(int i : array) 18 | { 19 | if(i==key) 20 | return ("Key found at : "+i); 21 | } 22 | return ("Key not found."); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /arrayMethods/PairSearch.java: -------------------------------------------------------------------------------- 1 | package arrayMethods; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.HashSet; 6 | import java.util.Iterator; 7 | import java.util.List; 8 | import java.util.Set; 9 | import java.util.stream.Collectors; 10 | 11 | public class PairSearch { 12 | 13 | public int x; 14 | public int y; 15 | 16 | @Override 17 | public String toString() { 18 | return "[x = " + x + ", y=" + y + "]"; 19 | } 20 | 21 | 22 | public static void main(String[] args) { 23 | // TODO Auto-generated method stub 24 | 25 | int[] a = {4, 9, 0, 34, 7, 22, 3, 10, 1, 5, 12, 55, 89, 32, 53, 62}; 26 | int target = 111; 27 | // Integer[] a1 = 28 | 29 | PairSearch result = new PairSearch(); 30 | Set set = new HashSet<>(); 31 | List al = new ArrayList(Arrays.asList(a)); 32 | Set s1 = new HashSet(Arrays.asList(a)); 33 | for(int i=0;i list = Arrays.stream(a).boxed().collect(Collectors.toList()); 63 | System.out.println(result); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /arrayMethods/StringToArray.java: -------------------------------------------------------------------------------- 1 | package arrayMethods; 2 | 3 | 4 | public class StringToArray 5 | { 6 | 7 | public static void main(String[] args) 8 | { 9 | String str = "Hello World"; 10 | solution(str); 11 | System.out.println(); 12 | solution(str); 13 | } 14 | 15 | public static void solution(String data) 16 | { 17 | char[] resArray = new char[data.length()]; 18 | for(int i=0;i20); 10 | System.out.println(x); 11 | assert(t1.add(10,20)==40); 12 | System.out.println(t1.add(10,20)); 13 | 14 | } 15 | } 16 | 17 | class Test{ 18 | public int add(int a, int b){ 19 | return a+b; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /bruteForceAlgorithm/FindPair.java: -------------------------------------------------------------------------------- 1 | package bruteForceAlgorithm; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | public class FindPair 7 | { 8 | private static class Pair { 9 | public int x; 10 | public int y; 11 | 12 | @Override 13 | public String toString() { 14 | return "[x = " + x + ", y=" + y + "]"; 15 | } 16 | } 17 | public static void main(String[] args) { 18 | 19 | int[] a = {4, 9, 0, 34, 7, 22, 3, 10, 1, 5, 12, 55, 89, 32, 53, 62}; 20 | int target = 111; 21 | Pair result = new Pair(); 22 | Set set = new HashSet(); 23 | 24 | for (int i = 0; i < a.length; i++) { 25 | 26 | if (set.contains(target - a[i])) { 27 | result.x = target - a[i]; 28 | result.y = a[i]; 29 | break; 30 | } 31 | set.add(a[i]); 32 | 33 | } 34 | 35 | System.out.println("pair = [" + result + "]"); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /classProperties/ClassFields.java: -------------------------------------------------------------------------------- 1 | package classProperties; 2 | 3 | import java.lang.reflect.Field; 4 | class Test 5 | { 6 | int age =0; 7 | String name = "Ram"; 8 | // int age; 9 | // String name; 10 | } 11 | 12 | public class ClassFields extends Test 13 | { 14 | public static void main(String[] args) throws ClassNotFoundException , NoSuchFieldException 15 | { 16 | //Test t = new Test(); 17 | System.out.println("Hello"); 18 | Class cls = Class.forName("Test"); 19 | Field field1 = cls.getField("age"); 20 | System.out.println(field1); 21 | Field[] field = cls.getFields(); 22 | for(Field f:field) 23 | { 24 | System.out.println("Hi"); 25 | System.out.println(f); 26 | } 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /codility/BinaryGap.java: -------------------------------------------------------------------------------- 1 | package codility; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.Scanner; 6 | 7 | public class BinaryGap 8 | { 9 | public static void main(String [] args) 10 | { 11 | Scanner sc = new Scanner(System.in); 12 | System.out.println("Enter the number :"); 13 | int n = sc.nextInt(); 14 | int longestBinaryGap = solution(n); 15 | System.out.println("Longest binary gap is : "+longestBinaryGap); 16 | } 17 | 18 | public static int solution(int N) 19 | { 20 | String binaryString = Integer.toBinaryString(N); 21 | 22 | int longestBinaryGap = 0; 23 | List onesList = new ArrayList(); 24 | 25 | for(int i=0; i=Y) 24 | return D/K; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /codility/FrogRiverOne.java: -------------------------------------------------------------------------------- 1 | package codility; 2 | 3 | import java.util.HashSet; 4 | import java.util.Scanner; 5 | 6 | public class FrogRiverOne 7 | { 8 | public static void main(String[] args) 9 | { 10 | Scanner sc = new Scanner((System.in)); 11 | System.out.println("Enter frog distance : "); 12 | int X = sc.nextInt(); 13 | System.out.println("Enter the array size"); 14 | int n = sc.nextInt(); 15 | System.out.println("Enter the array : "); 16 | int A[] = new int[n]; 17 | for (int i=0;i testedSet = new TreeSet(); 26 | Set perfectSet = new TreeSet(); 27 | 28 | for(int i=0; i 1 && A[0] > 1) return 1; 38 | 39 | for(int i=0; i < A.length; i++) 40 | { 41 | if(A[i] > 0 && A[i] < 100001) 42 | { 43 | if(i < A.length-1 && A[i+1]-A[i] == 0) 44 | { 45 | res = A[i] + 1; 46 | } else if(i < A.length-1 && A[i+1]-A[i] == 1) 47 | { 48 | res = A[i+1] + 1; 49 | } else if(i < A.length-1 && A[i+1]-A[i] > 1) 50 | { 51 | return A[i] + 1; 52 | } 53 | } 54 | } 55 | return res; 56 | } 57 | } -------------------------------------------------------------------------------- /codility/OddOccurrencesInArray.java: -------------------------------------------------------------------------------- 1 | package codility; 2 | 3 | import java.util.Scanner; 4 | 5 | public class OddOccurrencesInArray 6 | { 7 | //int num; 8 | 9 | public static void main(String[] args) 10 | { 11 | 12 | Scanner sc = new Scanner((System.in)); 13 | System.out.println("Enter the array size"); 14 | int n; 15 | /* Since we need only odd number of elements in array, if user enters an even number of elements, 16 | we will ask to re-enter again.*/ 17 | while(true) 18 | { 19 | n = sc.nextInt(); 20 | if(n%2!=0) 21 | break; 22 | else 23 | System.out.println("Please enter an odd size"); 24 | continue; 25 | } 26 | System.out.println("Enter the array elements : "); 27 | int A[] = new int[n]; 28 | for (int i=0;i occurrencesMap = new HashMap(); 41 | 42 | for(int i=0; i keySet = occurrencesMap.keySet(); 55 | //System.out.println(keySet); 56 | 57 | for(int currentKey : keySet) 58 | { 59 | int occurrences = occurrencesMap.get(currentKey); 60 | //System.out.println(occurrencesMap.get(currentKey)); 61 | 62 | //if occurs odd number of times, we found the unpaired value - no need to continue checking 63 | if(occurrences % 2 != 0) 64 | return currentKey; 65 | } 66 | 67 | //should never get to here 68 | throw new RuntimeException("It may be an input which does not contain any unpaired value."); 69 | } 70 | } -------------------------------------------------------------------------------- /codility/PermutationCheck.java: -------------------------------------------------------------------------------- 1 | package codility; 2 | 3 | import java.util.HashSet; 4 | import java.util.Scanner; 5 | import java.util.Set; 6 | 7 | public class PermutationCheck 8 | { 9 | 10 | public static void main(String[] args) 11 | { 12 | Scanner sc = new Scanner((System.in)); 13 | System.out.println("Enter the array size"); 14 | int n = sc.nextInt(); 15 | System.out.println("Enter the array : "); 16 | int A[] = new int[n]; 17 | for (int i=0;i testedSet = new HashSet(); 26 | Set perfectSet = new HashSet(); 27 | 28 | for(int i=0; i=0) 23 | System.out.println(y); 24 | else 25 | System.out.println("Invalid Input"); 26 | } 27 | 28 | public static int solution(int x, int[] A){ 29 | 30 | Set perSet = new HashSet(); 31 | Set reqSet = new HashSet(); 32 | for(int i=1;i<=x;i++) 33 | { 34 | reqSet.add(i); 35 | } 36 | 37 | for(int p=0;p set = new HashSet(); 14 | Collections.addAll(set,array); 15 | System.out.println(set); 16 | Iterator ir = set.iterator(); 17 | System.out.println(set); 18 | while(ir.hasNext()) 19 | { 20 | System.out.print(ir.next()); 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /collection/ArrayList1.java: -------------------------------------------------------------------------------- 1 | package collection; 2 | import java.util.ArrayList; 3 | import java.util.Collections; 4 | import java.util.Comparator; 5 | 6 | public class ArrayList1 7 | { 8 | 9 | public static void main(String[] args) 10 | { 11 | ArrayList al = new ArrayList(); 12 | al.add(1); 13 | al.add(4); 14 | al.add(7); 15 | al.add(2); 16 | al.add(3); 17 | ArrayList all = new ArrayList(); 18 | al.add(10); 19 | al.add(20); 20 | ArrayList al1 = new ArrayList(); 21 | al1.add(10); 22 | al1.add(0); 23 | al1.add(0); 24 | al1.add(1); 25 | //al1.addAll(1,al); 26 | //System.out.println(al.removeAll(al1)); 27 | //al.clear(); 28 | //al.retainAll(al1); 29 | // System.out.println(al); 30 | System.out.println(al1); 31 | // System.out.println(al.contains(1)); 32 | // System.out.println(al1.size()); 33 | // //object[] objects = al.toArray(); 34 | // 35 | Integer[] al2 = al.toArray(new Integer[al.size()]); 36 | for(int i=0;i list = Arrays.asList(s1); 15 | System.out.println("This list is : "+ list); 16 | 17 | // we are not creating List object. We are just viewing Array in List format. 18 | s1[2]="Lakhan"; 19 | System.out.println("This list is : "+ list); 20 | list.set(2, "Rajesh"); 21 | System.out.println("This list is : "+ list); 22 | 23 | //ArrayList al = new ArrayList(); 24 | String[] sa = {"ram","sham","babita"}; 25 | ArrayList al=new ArrayList(Arrays.asList(sa)); 26 | System.out.println(al); 27 | // String j = Arrays.toString(sa); 28 | // System.out.println("New string j = "+j); 29 | // System.out.println(j.charAt(10)); 30 | // for(int i=sa.length-1;i>=0;i--) 31 | // { 32 | // System.out.println(sa[i]); 33 | // } 34 | // for(int i=j.length()-1;i>=0;i--) 35 | // { 36 | // System.out.print(j.charAt(i)); 37 | // } 38 | // System.out.println(""); 39 | int[] ar = {1,2,3,4,5}; 40 | int[] arr = {1,2,3,4,5,6}; 41 | System.out.println(Arrays.toString(Arrays.copyOf(ar,15))); 42 | 43 | 44 | 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /collection/BinarySearch.java: -------------------------------------------------------------------------------- 1 | package collection; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | 6 | public class BinarySearch 7 | { 8 | public static void main(String[] args) 9 | { 10 | //Create list 11 | ArrayList arrlist = new ArrayList(); 12 | arrlist.add(10); 13 | arrlist.add(-20); 14 | arrlist.add(30); 15 | arrlist.add(-40); 16 | arrlist.add(50); 17 | //Print List 18 | System.out.println("Provided List are: "+arrlist); 19 | //Search the list for key '-20' 20 | // Collections.sort(arrlist,Collections.reverseOrder()); 21 | // System.out.println(arrlist); 22 | int index = Collections.binarySearch(arrlist, -20, Collections.reverseOrder()); 23 | System.out.println("Index '-20' is available at position: "+index); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /collection/ComparatorDemo.java: -------------------------------------------------------------------------------- 1 | package collection; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | import java.util.Comparator; 6 | import java.util.List; 7 | 8 | public class ComparatorDemo 9 | { 10 | public static void main(String[] args) 11 | { 12 | List al = new ArrayList(); 13 | al.add(10); 14 | al.add(20); 15 | al.add(5); 16 | al.add(40); 17 | al.add(13); 18 | Collections.sort(al); 19 | System.out.println(al); 20 | Collections.sort(al,new MyComparator()); 21 | System.out.println(al); 22 | } 23 | } 24 | 25 | class MyComparator implements Comparator 26 | { 27 | public int compare(Object obj1, Object obj2) 28 | { 29 | Integer i1 = (Integer)obj1; 30 | Integer i2 = (Integer)obj2; 31 | return -i1.compareTo(i2); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /collection/HashMapDemo.java: -------------------------------------------------------------------------------- 1 | package collection; 2 | 3 | import java.util.Collection; 4 | import java.util.HashMap; 5 | import java.util.Iterator; 6 | import java.util.Map; 7 | import java.util.Map.Entry; 8 | //import java.util.*; 9 | import java.util.Set; 10 | import java.lang.*; 11 | 12 | public class HashMapDemo 13 | { 14 | 15 | public static void main(String[] args) 16 | { 17 | // TODO Auto-generated method stub 18 | HashMap hm = new HashMap(); 19 | hm.put(1, "Ram"); 20 | hm.put(2, "Sita"); 21 | hm.put(3, "Shyam"); 22 | hm.put(4, "Geeta"); 23 | 24 | System.out.println(hm); 25 | 26 | hm.put(1, "Rama"); 27 | 28 | System.out.println(hm); 29 | System.out.println(hm.get(3)); 30 | 31 | Set s = hm.keySet(); 32 | System.out.println(s); 33 | 34 | Collection c = hm.values(); 35 | System.out.println(c); 36 | 37 | Set se = hm.entrySet(); 38 | System.out.println(se); 39 | 40 | Iterator itr = se.iterator(); 41 | //Entry me; 42 | while(itr.hasNext()) 43 | { 44 | Map.Entry me = (Map.Entry)itr.next(); 45 | // me = (Entry)itr.next(); 46 | System.out.println(me.getKey() + "........."+me.getValue()); 47 | if(me.getKey().equals("1")) 48 | { 49 | me.setValue("Ram"); 50 | } 51 | } 52 | System.out.println(hm); 53 | } 54 | 55 | } -------------------------------------------------------------------------------- /collection/HashSetDemo.java: -------------------------------------------------------------------------------- 1 | package collection; 2 | 3 | import java.util.Collections; 4 | import java.util.HashSet; 5 | 6 | public class HashSetDemo 7 | { 8 | 9 | public static void main(String[] args) 10 | { 11 | // TODO Auto-generated method stub 12 | HashSet hs = new HashSet(); 13 | // hs.add("B"); 14 | // hs.add(null); 15 | // hs.add("Z"); 16 | // hs.add("A"); 17 | // hs.add(10); 18 | // hs.add("D"); 19 | // hs.add("C"); 20 | // System.out.println(hs); 21 | // System.out.println(hs); 22 | // System.out.println(hs); 23 | hs.add(10); 24 | System.out.println(hs); 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /collection/ReadingArrayList.java: -------------------------------------------------------------------------------- 1 | package collection; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Iterator; 5 | 6 | public class ReadingArrayList 7 | { 8 | 9 | public static void main(String[] args) 10 | { 11 | ArrayList data = new ArrayList(); 12 | data.add(12); 13 | data.add("Ram"); 14 | data.add(25); 15 | data.add(true); 16 | 17 | // Reading arraylist mathod --- 1 18 | System.out.println(data); 19 | 20 | // Reading arraylist mathod --- 2 -- separate elements 21 | for(int i=0;i l1 = new ArrayList(); 13 | l1.add("Ram"); 14 | l1.add("Sita"); 15 | l1.add("Shyam"); 16 | l1.add("Geeta"); 17 | List l2 = Collections.unmodifiableList(l1); 18 | System.out.println(l2); 19 | System.out.println(String.class.getClass()); 20 | System.out.println(Connection.class.getClass()); 21 | System.out.println(ArrayList.class.getClass()); 22 | // code in Java-9 23 | // List l12 = List.of("ab","cd","ef","gh"); 24 | } 25 | public static void main(String[] args) 26 | { 27 | m1(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /generics/TestGen.java: -------------------------------------------------------------------------------- 1 | package generics; 2 | 3 | class MyGen{ 4 | T obj; 5 | static int count; 6 | { 7 | count++; 8 | } 9 | void add(T obj) 10 | { 11 | this.obj=obj; 12 | } 13 | T get(){ 14 | return obj; 15 | } 16 | } 17 | public class TestGen { 18 | 19 | public static void main(String[] args) { 20 | // TODO Auto-generated method stub 21 | 22 | MyGen mg = new MyGen(); 23 | // mg.add(10); 24 | // System.out.println(mg.obj); 25 | // mg.add(20); 26 | // System.out.println(mg.obj); 27 | mg.add(30); 28 | // mg.add("Ram"); 29 | System.out.println(mg.obj); 30 | System.out.println(mg.get()); 31 | MyGen mg1 = new MyGen(); 32 | mg1.add("Ram"); 33 | System.out.println(mg1.obj); 34 | System.out.println("No. of objects created : "+ mg.count); 35 | 36 | 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /generics/TestGen2.java: -------------------------------------------------------------------------------- 1 | package generics; 2 | 3 | 4 | class MyGen1{ 5 | 6 | T obj; 7 | // MyGen1(){ 8 | // this.obj = obj; // not required 9 | // } 10 | MyGen1(T obj){this.obj = obj;} 11 | public T get(){ 12 | return obj; 13 | } 14 | } 15 | 16 | public class TestGen2 { 17 | 18 | public static void main(String[] args) { 19 | // TODO Auto-generated method stub 20 | MyGen1 mg1 = new MyGen1(10); 21 | System.out.println(mg1.get()); 22 | 23 | 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /immutable/ImmutableDemo.java: -------------------------------------------------------------------------------- 1 | package immutable; 2 | 3 | final class ImmutableDemo 4 | { 5 | private final int age; 6 | 7 | ImmutableDemo(int age) 8 | { 9 | this.age=age; 10 | } 11 | 12 | public ImmutableDemo test(int age) 13 | { 14 | if(this.age==age) 15 | return this; 16 | else 17 | return (new ImmutableDemo(age)); 18 | } 19 | 20 | public static void main(String[] args) 21 | { 22 | // TODO Auto-generated method stub 23 | 24 | ImmutableDemo im1 = new ImmutableDemo(10); 25 | ImmutableDemo im2 = new ImmutableDemo(20); 26 | ImmutableDemo im3 = new ImmutableDemo(10); 27 | ImmutableDemo im4 = im1.test(10); 28 | System.out.println(im1==im2); 29 | System.out.println(im1==im3); 30 | System.out.println(im1==im4); 31 | System.out.println(im4.age); 32 | System.out.println(im1.age); 33 | System.out.println(im1==im4); 34 | System.out.println(im4.age); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /immutable/ImmutableStudent.java: -------------------------------------------------------------------------------- 1 | package immutable; 2 | 3 | public final class ImmutableStudent 4 | { 5 | private final int id; 6 | private final String name; 7 | public ImmutableStudent(int id, String name) 8 | { 9 | this.name = name; 10 | this.id = id; 11 | } 12 | public int getId() 13 | { 14 | return id; 15 | } 16 | public String getName() 17 | { 18 | return name; 19 | } 20 | 21 | public static void main(String[] args) 22 | { 23 | ImmutableStudent is1 = new ImmutableStudent(10,"Ram"); 24 | ImmutableStudent is2 = new ImmutableStudent(11,"sita"); 25 | ImmutableStudent is3 = new ImmutableStudent(10,"Ram"); 26 | // is1.id=50; 27 | ImmutableStudent is4 =is1; 28 | System.out.println(is1==is2); 29 | System.out.println(is1==is3); 30 | System.out.println(is1.equals(is2)); 31 | System.out.println(is1.equals(is3)); 32 | System.out.println(is1==is4); 33 | System.out.println(is1.equals(is4)); 34 | 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /immutableVsFinal/ImmutableVsFinal.java: -------------------------------------------------------------------------------- 1 | package immutableVsFinal; 2 | 3 | import java.awt.List; 4 | import java.util.ArrayList; 5 | import java.util.Collections; 6 | 7 | public class ImmutableVsFinal { 8 | 9 | public static void main(String[] args) 10 | { 11 | ArrayList al = new ArrayList(); 12 | al.add(10); 13 | al.add("Raj"); 14 | al.add(20); 15 | al.add("Ravi"); 16 | System.out.println(al); 17 | //ArrayList al1 = Collections.unmodifiableList(al); 18 | 19 | 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /input/ArrayInputDemo.java: -------------------------------------------------------------------------------- 1 | package input; 2 | 3 | import java.util.Scanner; 4 | 5 | public class ArrayInputDemo 6 | { 7 | 8 | public static void main(String[] args) 9 | { 10 | Scanner sc = new Scanner(System.in); 11 | System.out.println("Please enter array size : "); 12 | int s = sc.nextInt(); 13 | int[] age = new int[s]; 14 | System.out.println("Please enter array elements : "); 15 | for(int i=0;i al = new ArrayList(); 11 | al.add(10); 12 | al.add(20); 13 | al.add(30); 14 | al.add(40); 15 | 16 | // for(int i:al) 17 | // { 18 | // System.out.println(i); 19 | // } 20 | al.forEach(i->System.out.println(i)); 21 | al.forEach(System.out::println); 22 | al.forEach(ForEach::doubleIt); 23 | } 24 | 25 | public static void doubleIt(int i) 26 | { 27 | System.out.println(i*i); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /practice/StringDemo.java: -------------------------------------------------------------------------------- 1 | package practice; 2 | 3 | public class StringDemo 4 | { 5 | 6 | public static void main(String[] args) 7 | { 8 | // TODO Auto-generated method stub 9 | String s1="0"; 10 | String s2=" "; 11 | // if(s1.equals("")) 12 | // System.out.println("Cond 1 True"); 13 | // else 14 | // System.out.println("False"); 15 | // if(s1.equals(null)) 16 | // System.out.println("Cond 1 True"); 17 | // else 18 | // System.out.println("False"); 19 | // System.out.println(s1.isEmpty()); 20 | // 21 | // if(s2.equals("")) 22 | // System.out.println("Cond 1 True"); 23 | // else 24 | // System.out.println("False"); 25 | // if(s2.equals(null)) 26 | // System.out.println("Cond 1 True"); 27 | // else 28 | // System.out.println("False"); 29 | // System.out.println(s2.isEmpty()); 30 | if((int)s1.charAt(0)==48) 31 | System.out.println("0 found"); 32 | 33 | 34 | 35 | 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /predicate/PredCollDemo.java: -------------------------------------------------------------------------------- 1 | package predicate; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Scanner; 5 | import java.util.function.Predicate; 6 | 7 | public class PredCollDemo { 8 | 9 | public static void main(String[] args) { 10 | // TODO Auto-generated method stub 11 | 12 | 13 | Scanner sc = new Scanner(System.in); 14 | System.out.println("Enter the size of arraylist: "); 15 | int x = sc.nextInt(); 16 | // ArrayList al = new ArrayList(); 17 | ArrayList al = new ArrayList(); 18 | System.out.println(al.size()); 19 | System.out.println("Enter the arraylist elements: "); 20 | for(int i=0;i p = arl -> arl.isEmpty(); 30 | System.out.println(p.test(al)); 31 | 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /predicate/PredicateDemo.java: -------------------------------------------------------------------------------- 1 | package predicate; 2 | 3 | import java.util.Scanner; 4 | import java.util.function.Predicate; 5 | 6 | public class PredicateDemo { 7 | 8 | public static void main(String[] args) { 9 | // TODO Auto-generated method stub 10 | 11 | Scanner sc = new Scanner(System.in); 12 | System.out.println("Enter the number: "); 13 | int x = sc.nextInt(); 14 | System.out.println("Enter the number to compare : "); 15 | int y = sc.nextInt(); 16 | Predicate P = (I) -> (I>x); 17 | System.out.println(P.test(y)); 18 | 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /projects/ExtractMailIDFrmFile.java: -------------------------------------------------------------------------------- 1 | package projects; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.File; 5 | import java.io.FileReader; 6 | import java.io.IOException; 7 | import java.io.PrintWriter; 8 | import java.util.regex.Matcher; 9 | import java.util.regex.Pattern; 10 | 11 | public class ExtractMailIDFrmFile { 12 | 13 | public static void main(String[] args) throws IOException{ 14 | mobileNo(new File("Data.txt")); 15 | mailID(new File("Data.txt")); 16 | 17 | } 18 | 19 | public static void mobileNo(File f){ 20 | try{ 21 | // File f = new File("Mobile.txt"); 22 | // f.createNewFile(); 23 | // System.out.println(f.exists()); 24 | PrintWriter out = new PrintWriter("Mobile.txt"); 25 | FileReader fr = new FileReader(f); 26 | BufferedReader br = new BufferedReader(fr); 27 | int count=0; 28 | Pattern p = Pattern.compile("(0|91)?[7-9][0-9]{9}"); 29 | String line = br.readLine(); 30 | while(line!=null) 31 | { 32 | Matcher m = p.matcher(line); 33 | while(m.find()) 34 | { 35 | count++; 36 | out.println(m.group()); 37 | // System.out.println(m.group()); 38 | } 39 | line=br.readLine(); 40 | } 41 | // System.out.print("Total mobile numbers found : "+count); 42 | out.print("Total mobile numbers found : "+count); 43 | out.flush(); 44 | br.close(); 45 | } 46 | catch(IOException ie){ 47 | ie.printStackTrace(); 48 | } 49 | } 50 | 51 | public static void mailID(File f){ 52 | try{ 53 | // File f = new File("Mobile.txt"); 54 | // f.createNewFile(); 55 | // System.out.println(f.exists()); 56 | PrintWriter out = new PrintWriter("MailID.txt"); 57 | FileReader fr = new FileReader(f); 58 | BufferedReader br = new BufferedReader(fr); 59 | int count=0; 60 | Pattern p = Pattern.compile("[a-zA-Z][a-zA-Z0-9.-]{4}@[a-zA-Z0-9]+.[a-zA-Z]*{15}"); 61 | String line = br.readLine(); 62 | while(line!=null) 63 | { 64 | Matcher m = p.matcher(line); 65 | while(m.find()) 66 | { 67 | count++; 68 | out.println(m.group()); 69 | // System.out.println(m.group()); 70 | } 71 | line=br.readLine(); 72 | } 73 | // System.out.print("Total mail IDs found : "+count); 74 | out.print("Total mail IDs found : "+count); 75 | out.flush(); 76 | br.close(); 77 | } 78 | catch(IOException ie){ 79 | ie.printStackTrace(); 80 | } 81 | } 82 | } -------------------------------------------------------------------------------- /projects/ExtractMobFrmFile.java: -------------------------------------------------------------------------------- 1 | package projects; 2 | 3 | import java.io.*; 4 | import java.util.regex.*; 5 | 6 | public class ExtractMobFrmFile { 7 | 8 | public static void main(String[] args) { 9 | 10 | try{ 11 | FileReader fr = new FileReader("Data.txt"); 12 | BufferedReader br = new BufferedReader(fr); 13 | File f = new File("Mobile.txt"); 14 | f.createNewFile(); 15 | //System.out.println(f.exists()); 16 | PrintWriter out = new PrintWriter(f); 17 | int count=0; 18 | Pattern p = Pattern.compile("(0|91)?[7-9][0-9]{9}"); 19 | String line = br.readLine(); 20 | while(line!=null) 21 | { 22 | Matcher m = p.matcher(line); 23 | while(m.find()) 24 | { 25 | count++; 26 | out.println(m.group()); 27 | // System.out.println(m.group()); 28 | } 29 | line=br.readLine(); 30 | } 31 | // System.out.print("Total mobile numbers found : "+count); 32 | out.print("Total mobile numbers found : "+count); 33 | out.flush(); 34 | br.close(); 35 | } 36 | catch(IOException ie){ 37 | ie.printStackTrace(); 38 | } 39 | } 40 | 41 | 42 | 43 | } 44 | -------------------------------------------------------------------------------- /regex/RegFileNames.java: -------------------------------------------------------------------------------- 1 | package regex; 2 | 3 | import java.io.File; 4 | import java.util.regex.*; 5 | 6 | public class RegFileNames { 7 | 8 | public static void main(String[] args) { 9 | 10 | File f = new File("F:\\Eclipse\\PRACT"); 11 | txtFinder(f); 12 | 13 | } 14 | 15 | public static void txtFinder(File f){ 16 | int count=0; 17 | Pattern p = Pattern.compile("[a-zA-Z0-9-.$]+.txt"); 18 | String[] listFile = f.list(); 19 | for(String name:listFile) 20 | { 21 | Matcher m = p.matcher(name); 22 | while(m.find()) 23 | { 24 | count++; 25 | System.out.println(name); 26 | } 27 | // if(m.find()&&m.group().equals(name)) 28 | // { 29 | // count++; 30 | // System.out.println(name); 31 | // } 32 | } 33 | System.out.println("Number of text files is : "+count); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /regex/RegJavaIdentifier.java: -------------------------------------------------------------------------------- 1 | package regex; 2 | 3 | import java.util.regex.*; 4 | 5 | public class RegJavaIdentifier { 6 | 7 | public static void main(String[] args) { 8 | validator("Ashok"); 9 | validator("Ashok-"); 10 | validator("=Ashok"); 11 | validator("?Ashok"); 12 | } 13 | 14 | public static void validator(String x){ 15 | 16 | Pattern p = Pattern.compile("[a-zA-Z][a-zA-Z0-9-#]+"); 17 | Matcher m = p.matcher(x); 18 | if(m.find()&&m.group().equals(x)) 19 | { 20 | System.out.println("Valid"); 21 | } 22 | else 23 | { 24 | System.out.println("Invalid"); 25 | } 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /regex/RegMailID.java: -------------------------------------------------------------------------------- 1 | package regex; 2 | 3 | import java.util.regex.Matcher; 4 | import java.util.regex.Pattern; 5 | 6 | public class RegMailID { 7 | 8 | public static void main(String[] args) { 9 | String x = "rishi@gmail.com"; 10 | System.out.println(x.length()); 11 | validator(x); 12 | } 13 | 14 | public static void validator(String x){ 15 | Pattern p = Pattern.compile("[a-zA-Z][a-zA-Z0-9.-]{4..}@[a-zA-Z0-9]+.[a-zA-Z]*{15}"); 16 | Matcher m = p.matcher(x); 17 | if(m.find()&&m.group().equals(x)) 18 | { 19 | System.out.println("Valid"); 20 | } 21 | else 22 | { 23 | System.out.println("Invalid"); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /regex/RegMobNum.java: -------------------------------------------------------------------------------- 1 | package regex; 2 | 3 | import java.util.regex.*; 4 | 5 | public class RegMobNum { 6 | 7 | public static void main(String[] args) { 8 | 9 | validator("919850767571"); 10 | validator("6850767571"); 11 | validator("08507675712"); 12 | } 13 | 14 | public static void validator(String x){ 15 | Pattern p = Pattern.compile("(0|91)?[7-9][0-9]{9}"); 16 | Matcher m = p.matcher(x); 17 | if(m.find()&&m.group().equals(x)) 18 | { 19 | System.out.println("Valid"); 20 | } 21 | else 22 | { 23 | System.out.println("Invalid"); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /regex/Regex1.java: -------------------------------------------------------------------------------- 1 | package regex; 2 | 3 | import java.util.regex.*; 4 | 5 | public class Regex1 { 6 | 7 | public static void main(String[] args) { 8 | // 9 | // String x = "ab"; 10 | // String y = "ababdfabbbab"; 11 | String s1 = "I love India"; 12 | validate("ab", "ababbdaabab"); 13 | 14 | String[] s = s1.split("\\s"); 15 | for(String x:s) 16 | { System.out.println(x);} 17 | } 18 | 19 | public static void validate(String x , String y) { 20 | 21 | Pattern p = Pattern.compile(x); 22 | Matcher m = p.matcher(y); 23 | while(m.find()){ 24 | System.out.println(m.start()+ " " + m.group()); 25 | } 26 | } 27 | 28 | 29 | } 30 | -------------------------------------------------------------------------------- /regex/RegexDemo.java: -------------------------------------------------------------------------------- 1 | package regex; 2 | import java.util.Scanner; 3 | import java.util.regex.*; 4 | 5 | public class RegexDemo 6 | { 7 | 8 | public static void main(String[] args) 9 | { 10 | // TODO Auto-generated method stub 11 | 12 | Scanner sc = new Scanner(System.in); 13 | // System.out.println("Enter the mobile number : "); 14 | // String smob = sc.nextLine(); 15 | // System.out.println("Enter the pattern to find : "); 16 | // String spat = sc.nextLine(); 17 | // Pattern p = Pattern.compile(spat); 18 | // Matcher m = p.matcher(smob); 19 | // Matcher m = p.matcher("a0B9b@Xr#_7kT%"); 20 | // Matcher m = p.matcher("abcdefghijklmnopqrstuvwxyzabcdrfjklmn"); 21 | // 22 | // int count=0; 23 | // System.out.println("start"+"\t"+"ends"+"\t"+"Group"); 24 | // while(m.find()) 25 | // { 26 | // count++; 27 | // System.out.println(m.start() +"\t"+m.end()+"\t"+m.group()); 28 | // } 29 | // System.out.println("No. of occurrence : "+ count); 30 | System.out.println(Pattern.matches("[amn]+", "aa")); 31 | System.out.println(Pattern.matches("[amn]?", "a"));//true (a or m or n comes one time) 32 | System.out.println(Pattern.matches("[amn]?", "aaa"));//false (a comes more than one time) 33 | //System.out.println(p.pattern()); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /singleton/RuntimeFactoryMethod.java: -------------------------------------------------------------------------------- 1 | package singleton; 2 | 3 | import java.util.Date; 4 | 5 | public class RuntimeFactoryMethod 6 | { 7 | public static void main(String[] args) 8 | { 9 | Runtime r = Runtime.getRuntime(); 10 | System.out.println("Total "+ r.totalMemory()); 11 | System.out.println("Free "+r.freeMemory()); 12 | for(int i=0;i<10000;i++) 13 | { 14 | Date d = new Date(); 15 | d=null; 16 | } 17 | System.out.println("New Free 1 "+r.freeMemory()); 18 | r.gc(); 19 | System.out.println("New Free "+r.freeMemory()); 20 | } 21 | 22 | 23 | } 24 | -------------------------------------------------------------------------------- /singleton/Singleton.java: -------------------------------------------------------------------------------- 1 | package singleton; 2 | 3 | public class Singleton 4 | { 5 | private static Singleton s = null; 6 | private Singleton(){}; 7 | 8 | public static Singleton getSingleton() 9 | { 10 | // this is a factory method 11 | if(s==null) 12 | { 13 | s=new Singleton(); 14 | } 15 | return s; 16 | } 17 | 18 | public void output() 19 | { 20 | System.out.println("Hi"); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /singleton/Singleton2.java: -------------------------------------------------------------------------------- 1 | package singleton; 2 | 3 | // Another way of creating singleton class 4 | public class Singleton2 5 | { 6 | private static Singleton2 s2 = new Singleton2(); 7 | private Singleton2() 8 | {} 9 | 10 | public static Singleton2 getSingleton2() 11 | { 12 | return s2; 13 | } 14 | 15 | } -------------------------------------------------------------------------------- /singleton/TestSingleton.java: -------------------------------------------------------------------------------- 1 | package singleton; 2 | 3 | public class TestSingleton 4 | { 5 | public static void main(String[] args) 6 | { 7 | Singleton t = Singleton.getSingleton(); 8 | Singleton t1 = Singleton.getSingleton(); 9 | System.out.println(t.equals(t1)); 10 | System.out.println(t==t1); 11 | t.output(); 12 | t1.output(); 13 | System.out.println(t.hashCode()); 14 | System.out.println(t1.hashCode()); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /stream/StreamDemoList.java: -------------------------------------------------------------------------------- 1 | package stream; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.stream.Collectors; 6 | 7 | public class StreamDemoList { 8 | 9 | public static void main(String[] args) { 10 | // TODO Auto-generated method stub 11 | 12 | ArrayList al = new ArrayList(); 13 | al.add(10); 14 | al.add(20); 15 | al.add(30); 16 | al.add(15); 17 | al.add(25); 18 | al.add(35); 19 | List alOdd = al.stream().filter(I->I%2!=0).collect(Collectors.toList()); // adds only those objects which fulfil condition 20 | System.out.println(alOdd); 21 | al.forEach(x->System.out.println(x)); 22 | List alsquare = al.stream().map(I->I*I).collect(Collectors.toList()); // add all objects after calculation 23 | System.out.println(alsquare); 24 | // List al2Elements = al.stream().flatMap(I->Stream.of(I*I,I*I*I)).collect(Collectors.toList()); 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /validation/Validation.java: -------------------------------------------------------------------------------- 1 | package validation; 2 | 3 | import java.util.function.Predicate; 4 | 5 | public class Validation { 6 | public static void main(String[] args) 7 | { 8 | long l = System.nanoTime(); 9 | System.out.println(); 10 | String data = "rishismile"; 11 | Validation1 v1 = new Validation1(); 12 | System.out.println(v1.usernamevalidator(data)); 13 | System.out.println(System.nanoTime()-l); 14 | } 15 | 16 | public boolean usernamevalidator(String data) 17 | { 18 | Predicate p = data1 -> (data1.length() >=5 && data1.length() <=10); 19 | p.test(data); 20 | // if(!(data.length() >=5 && data.length() <=10)) 21 | // { 22 | // return false; 23 | // } 24 | 25 | for(int i=0;i=65 && asciivalue <=90) && !(asciivalue >=97 && asciivalue <=122)) 32 | { 33 | return false; 34 | } 35 | } 36 | if(!(asciivalue >=65 && asciivalue <=90) && !(asciivalue >=97 && asciivalue <=122) && !(asciivalue >=48 && asciivalue <=57) && (asciivalue!=46) && (asciivalue!=45) && (asciivalue!=95)) 37 | { 38 | return false; 39 | } 40 | } 41 | return true; 42 | } 43 | public boolean emailvalidator(String data) 44 | { 45 | 46 | int atindex = data.indexOf("@"); 47 | int dotindex= data.indexOf("."); 48 | 49 | if(atindex < 0 ) 50 | { 51 | return false; 52 | //System.out.println("@ sign does not exist"); 53 | } 54 | 55 | /*if(dotindex < atindex ) 56 | { 57 | return false; 58 | //System.out.println("dot is before @ sign"); 59 | }*/ 60 | String com = data.substring(dotindex + 1); 61 | 62 | if(!(com.equals("com"))) 63 | { 64 | //System.out.println("string not end with com=="+com); 65 | return false; 66 | } 67 | String username = data.substring(0, atindex); 68 | if(!(username.length() >=5 && username.length() <=10)) 69 | { 70 | return false; 71 | //System.out.println("length in between 5 to 10 =>1"); 72 | } 73 | for(int i=0;i< username.length() ; i++) 74 | { 75 | int asciivalue = username.charAt(i); 76 | if(i==0) 77 | { 78 | if(!(asciivalue >=97 && asciivalue <=122) && !(asciivalue >=65 && asciivalue <=90)) 79 | { 80 | return false; 81 | //System.out.println("first character must be alphabet 1"); 82 | } 83 | } 84 | else 85 | { 86 | if(!(asciivalue >=65 && asciivalue <=90) && !(asciivalue >=97 && asciivalue <=122) && !(asciivalue >=48 && asciivalue <=57) && (asciivalue!=46) && (asciivalue!=45) && (asciivalue!=95)) 87 | { 88 | return false; 89 | //System.out.println("incorrext condition"); 90 | } 91 | } 92 | } 93 | String domainname = data.substring(atindex+1, dotindex); 94 | 95 | if(!(domainname.length() >=5 && domainname.length() <=10)) 96 | { 97 | return false; 98 | //System.out.println("length in between 5 to 10 = >2"); 99 | } 100 | for(int i=0;i< domainname.length() ; i++) 101 | { 102 | int asciivalue = domainname.charAt(i); 103 | 104 | if(i==0) 105 | { 106 | if(!(asciivalue >=97 && asciivalue <=122) && !(asciivalue >=65 && asciivalue <=90)) 107 | { 108 | return false; 109 | //System.out.println("first character must be alphabet 1"); 110 | } 111 | } 112 | else 113 | { 114 | if(!(asciivalue >=65 && asciivalue <=90) && !(asciivalue >=97 && asciivalue <=122) && !(asciivalue >=48 && asciivalue <=57)) 115 | { 116 | return false; 117 | //System.out.println("incorrext condition"); 118 | } 119 | } 120 | } 121 | 122 | return true; 123 | } 124 | public boolean datevalidator(String data) 125 | { 126 | if(!(data.length() ==10)) 127 | { 128 | return false; 129 | } 130 | for(int i=0;i=48 && asciivalue <=57)) 149 | { 150 | //System.out.println("not number"); 151 | return false; 152 | } 153 | 154 | } 155 | return true; 156 | } 157 | public boolean passwordValidation(String password,String confirmPassword) 158 | { 159 | if(password.equals(confirmPassword)) 160 | { 161 | return true; 162 | } 163 | return false; 164 | } 165 | 166 | } 167 | -------------------------------------------------------------------------------- /validation/ValidationOld.java: -------------------------------------------------------------------------------- 1 | package validation; 2 | 3 | public class ValidationOld{ 4 | 5 | public static void main(String[] args) 6 | { 7 | long l = System.nanoTime(); 8 | System.out.println(); 9 | String data = "rishismile"; 10 | Validation1 v1 = new Validation1(); 11 | System.out.println(v1.usernamevalidator(data)); 12 | System.out.println(System.nanoTime()-l); 13 | } 14 | 15 | } 16 | class Validation1 { 17 | public boolean usernamevalidator(String data) 18 | { 19 | if(!(data.length() >=5 && data.length() <=10)) 20 | { 21 | return false; 22 | } 23 | 24 | for(int i=0;i=65 && asciivalue <=90) && !(asciivalue >=97 && asciivalue <=122)) 31 | { 32 | return false; 33 | } 34 | } 35 | if(!(asciivalue >=65 && asciivalue <=90) && !(asciivalue >=97 && asciivalue <=122) && !(asciivalue >=48 && asciivalue <=57) && (asciivalue!=46) && (asciivalue!=45) && (asciivalue!=95)) 36 | { 37 | return false; 38 | } 39 | } 40 | return true; 41 | } 42 | public boolean emailvalidator(String data) 43 | { 44 | 45 | int atindex = data.indexOf("@"); 46 | int dotindex= data.indexOf("."); 47 | 48 | if(atindex < 0 ) 49 | { 50 | return false; 51 | //System.out.println("@ sign does not exist"); 52 | } 53 | 54 | /*if(dotindex < atindex ) 55 | { 56 | return false; 57 | //System.out.println("dot is before @ sign"); 58 | }*/ 59 | String com = data.substring(dotindex + 1); 60 | 61 | if(!(com.equals("com"))) 62 | { 63 | //System.out.println("string not end with com=="+com); 64 | return false; 65 | } 66 | String username = data.substring(0, atindex); 67 | if(!(username.length() >=5 && username.length() <=10)) 68 | { 69 | return false; 70 | //System.out.println("length in between 5 to 10 =>1"); 71 | } 72 | for(int i=0;i< username.length() ; i++) 73 | { 74 | int asciivalue = username.charAt(i); 75 | if(i==0) 76 | { 77 | if(!(asciivalue >=97 && asciivalue <=122) && !(asciivalue >=65 && asciivalue <=90)) 78 | { 79 | return false; 80 | //System.out.println("first character must be alphabet 1"); 81 | } 82 | } 83 | else 84 | { 85 | if(!(asciivalue >=65 && asciivalue <=90) && !(asciivalue >=97 && asciivalue <=122) && !(asciivalue >=48 && asciivalue <=57) && (asciivalue!=46) && (asciivalue!=45) && (asciivalue!=95)) 86 | { 87 | return false; 88 | //System.out.println("incorrext condition"); 89 | } 90 | } 91 | } 92 | String domainname = data.substring(atindex+1, dotindex); 93 | 94 | if(!(domainname.length() >=5 && domainname.length() <=10)) 95 | { 96 | return false; 97 | //System.out.println("length in between 5 to 10 = >2"); 98 | } 99 | for(int i=0;i< domainname.length() ; i++) 100 | { 101 | int asciivalue = domainname.charAt(i); 102 | 103 | if(i==0) 104 | { 105 | if(!(asciivalue >=97 && asciivalue <=122) && !(asciivalue >=65 && asciivalue <=90)) 106 | { 107 | return false; 108 | //System.out.println("first character must be alphabet 1"); 109 | } 110 | } 111 | else 112 | { 113 | if(!(asciivalue >=65 && asciivalue <=90) && !(asciivalue >=97 && asciivalue <=122) && !(asciivalue >=48 && asciivalue <=57)) 114 | { 115 | return false; 116 | //System.out.println("incorrext condition"); 117 | } 118 | } 119 | } 120 | 121 | return true; 122 | } 123 | public boolean datevalidator(String data) 124 | { 125 | if(!(data.length() ==10)) 126 | { 127 | return false; 128 | } 129 | for(int i=0;i=48 && asciivalue <=57)) 148 | { 149 | //System.out.println("not number"); 150 | return false; 151 | } 152 | 153 | } 154 | return true; 155 | } 156 | public boolean passwordValidation(String password,String confirmPassword) 157 | { 158 | if(password.equals(confirmPassword)) 159 | { 160 | return true; 161 | } 162 | return false; 163 | } 164 | 165 | } 166 | 167 | -------------------------------------------------------------------------------- /vararg/HeapPollution.java: -------------------------------------------------------------------------------- 1 | package vararg; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class HeapPollution 6 | { 7 | @SafeVarargs 8 | public static void display(ArrayList...pr) 9 | { 10 | for(ArrayList x:pr) 11 | { 12 | System.out.println(x); 13 | } 14 | } 15 | 16 | public static void main(String[] args) 17 | { 18 | ArrayList al = new ArrayList(); 19 | al.add("Raj"); 20 | al.add("Rama"); 21 | display(al); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /vararg/VarArg.java: -------------------------------------------------------------------------------- 1 | package vararg; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class VarArg 6 | { 7 | public static void m1() 8 | { 9 | Integer i =100; 10 | String s1 = String.valueOf(i); 11 | 12 | } 13 | 14 | public static void sum(int... x) // or sum(int[] x) anyone will work 15 | { 16 | int sum=0; 17 | for(int i=0;i