├── .gitignore ├── .idea ├── .gitignore ├── inspectionProfiles │ └── Project_Default.xml ├── jpa-buddy.xml ├── misc.xml ├── modules.xml ├── uiDesigner.xml └── vcs.xml ├── README.md ├── java8InterviewCQ.iml └── src └── com └── rohit └── Sample.java /.gitignore: -------------------------------------------------------------------------------- 1 | ### IntelliJ IDEA ### 2 | out/ 3 | !**/src/main/**/out/ 4 | !**/src/test/**/out/ 5 | 6 | ### Eclipse ### 7 | .apt_generated 8 | .classpath 9 | .factorypath 10 | .project 11 | .settings 12 | .springBeans 13 | .sts4-cache 14 | bin/ 15 | !**/src/main/**/bin/ 16 | !**/src/test/**/bin/ 17 | 18 | ### NetBeans ### 19 | /nbproject/private/ 20 | /nbbuild/ 21 | /dist/ 22 | /nbdist/ 23 | /.nb-gradle/ 24 | 25 | ### VS Code ### 26 | .vscode/ 27 | 28 | ### Mac OS ### 29 | .DS_Store -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | # Editor-based HTTP Client requests 5 | /httpRequests/ 6 | # Datasource local storage ignored files 7 | /dataSources/ 8 | /dataSources.local.xml 9 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 218 | -------------------------------------------------------------------------------- /.idea/jpa-buddy.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java 8 Interview Sample Coding Questions [Solution Improvement in progress] 2 | 3 | This repository contains sample Java 8 coding questions that can be used for interview preparation. Each question focuses on a specific programming concept or problem-solving technique using Java 8 features. 4 | 5 | ## List of Questions 6 | 7 | 1. **Separate odd and even numbers in a list of integers** 8 | 9 | Given a list of integers, write a Java 8 program to separate the odd and even numbers into two separate lists. 10 | 11 | 2. **Remove duplicate elements from a list using Java 8 streams** 12 | 13 | Write a Java 8 program to remove duplicate elements from a list using the stream API and lambda expressions. 14 | 15 | 3. **Find the frequency of each character in a string using Java 8 streams** 16 | 17 | Write a Java 8 program to find the frequency of each character in a given string using the stream API and collectors. 18 | 19 | 4. **Find the frequency of each element in an array or a list** 20 | 21 | Write a Java 8 program to find the frequency of each element in an array or a list using streams and collectors. 22 | 23 | 5. **Sort a given list of decimals in reverse order** 24 | 25 | Write a Java 8 program to sort a given list of decimal numbers in reverse order. 26 | 27 | 6. **Join a list of strings with '[' as prefix, ']' as suffix, and ',' as delimiter** 28 | 29 | Given a list of strings, write a Java 8 program to join the strings with '[' as a prefix, ']' as a suffix, and ',' as a delimiter. 30 | 31 | 7. **Print the numbers from a given list of integers that are multiples of 5** 32 | 33 | Write a Java 8 program to print the numbers from a given list of integers that are multiples of 5. 34 | 35 | 8. **Find the maximum and minimum of a list of integers** 36 | 37 | Given a list of integers, write a Java 8 program to find the maximum and minimum numbers in the list. 38 | 39 | 9. **Merge two unsorted arrays into a single sorted array using Java 8 streams** 40 | 41 | Write a Java 8 program to merge two unsorted arrays into a single sorted array using the stream API. 42 | 43 | 10. **Merge two unsorted arrays into a single sorted array without duplicates** 44 | 45 | Write a Java 8 program to merge two unsorted arrays into a single sorted array without duplicates. 46 | 11. **Get the three maximum and three minimum numbers from a given list of integers** 47 | 48 | Write a Java 8 program to get the three maximum and three minimum numbers from a given list of integers. 49 | 50 | 12. **Check if two strings are anagrams or not using Java 8 streams** 51 | 52 | Write a Java 8 program to check if two strings are anagrams or not using the stream API and lambda expressions. 53 | 54 | 13. **Find the sum of all digits of a number in Java 8** 55 | 56 | Write a Java 8 program to find the sum of all digits of a given number. 57 | 58 | 14. **Find the second largest number in an integer array** 59 | 60 | Write a Java 8 program to find the second largest number in an integer array. 61 | 62 | 15. **Sort a list of strings according to the increasing order of their length** 63 | 64 | Write a Java 8 program to sort a given list of strings according to the increasing order of their length. 65 | 66 | 16. **Find the sum and average of all elements in an integer array** 67 | 68 | Write a Java 8 program to find the sum and average of all elements in an integer array. 69 | 70 | 17. **Find the common elements between two arrays** 71 | 72 | Write a Java 8 program to find the common elements between two arrays using streams. 73 | 74 | 18. **Reverse each word of a string using Java 8 streams** 75 | 76 | Write a Java 8 program to reverse each word of a given string using the stream API and lambda expressions. 77 | 78 | 19. **Find the sum of the first 10 natural numbers** 79 | 80 | Write a Java 8 program to find the sum of the first 10 natural numbers using streams. 81 | 82 | 20. **Reverse an integer array** 83 | 84 | Write a Java 8 program to reverse an integer array. 85 | 86 | 21. **Print the first 10 even numbers** 87 | 88 | Write a Java 8 program to print the first 10 even numbers. 89 | 90 | 22. **Find the most repeated element in an array** 91 | 92 | Write a Java 8 program to find the most repeated element in an array. 93 | 94 | 23. **Check if a string is a palindrome using Java 8 streams** 95 | 96 | Write a Java 8 program to check if a given string is a palindrome using the stream API and lambda expressions. 97 | 98 | 24. **Find strings in a list that start with a number** 99 | 100 | Given a list of strings, write a Java 8 program to find the strings that start with a number. 101 | 102 | 25. **Extract duplicate elements from an array** 103 | 104 | Write a Java 8 program to extract duplicate elements from an array. 105 | 106 | 26. **Print duplicate characters in a string** 107 | 108 | Write a Java 8 program to print the duplicate characters in a string. 109 | 110 | 27. **Find the first repeated character in a string** 111 | 112 | Write a Java 8 program to find the first repeated character in a string. 113 | 114 | 28. **Find the first non-repeated character in a string** 115 | 116 | Write a Java 8 program to find the first non-repeated character in a string. 117 | 118 | 29. **Generate the Fibonacci series** 119 | 120 | Write a Java 8 program to generate the Fibonacci series. 121 | 122 | 30. **Print the first 10 odd numbers** 123 | 124 | Write a Java 8 program to print the first 10 odd numbers. 125 | 126 | 31. **Get the last element of an array** 127 | 128 | Write a Java 8 program to get the last element of an array. 129 | 130 | 32. **Calculate the age of a person in years** 131 | 132 | Write a Java 8 program to calculate the age of a person in years given their birthday. 133 | -------------------------------------------------------------------------------- /java8InterviewCQ.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/com/rohit/Sample.java: -------------------------------------------------------------------------------- 1 | package com.rohit; 2 | 3 | import java.time.LocalDate; 4 | import java.time.Period; 5 | import java.util.*; 6 | import java.util.function.Function; 7 | import java.util.stream.IntStream; 8 | import java.util.stream.Stream; 9 | 10 | import static java.util.Comparator.reverseOrder; 11 | import static java.util.List.of; 12 | import static java.util.stream.Collectors.*; 13 | 14 | public class Sample { 15 | public static void main(String[] args) { 16 | 17 | /** 18 | * Separate odd and even numbers in a list of integers. 19 | * 20 | * Given a list of integers, write a Java 8 program to separate 21 | * the odd and even numbers into two separate lists. 22 | */ 23 | 24 | separationOfEvenOddNumberInMap(); 25 | separationOfEvenOddNumberInList(); 26 | 27 | /** 28 | * Remove duplicate elements from a list using Java 8 streams 29 | * 30 | * Write a Java 8 program to remove duplicate elements from a list 31 | * using the stream API and lambda expressions. 32 | */ 33 | 34 | removeDuplicateFromList(); 35 | 36 | 37 | /** 38 | * Find the frequency of each character in a string using Java 8 streams 39 | * 40 | * Write a Java 8 program to find the frequency of each character in 41 | * a given string using the stream API and collectors. 42 | */ 43 | characterFrequency(); 44 | 45 | /** 46 | * Find the frequency of each element in an array or a list 47 | * 48 | * Write a Java 8 program to find the frequency of 49 | * each element in an array or a list using streams and collectors. 50 | */ 51 | 52 | wordFrequency(); 53 | 54 | /** 55 | * Sort a given list of decimals in reverse order 56 | * 57 | * Write a Java 8 program to sort a given list of decimal numbers in reverse order. 58 | */ 59 | 60 | reverseSortedList(); 61 | 62 | /** 63 | * Join a list of strings with '[' as prefix, ']' as suffix, and ',' as delimiter 64 | * 65 | * Given a list of strings, write a Java 8 program to join the strings 66 | * with '[' as a prefix, ']' as a suffix, and ',' as a delimiter. 67 | */ 68 | 69 | joinListOfStrings(); 70 | 71 | /** 72 | * Print the numbers from a given list of integers that are multiples of 5 73 | * 74 | * Write a Java 8 program to print the numbers from a given list of integers that are multiples of 5. 75 | */ 76 | 77 | multipleOf5(); 78 | 79 | /** 80 | * Find the maximum and minimum of a list of integers 81 | * Given a list of integers, write a Java 8 program to find the maximum and minimum numbers in the list. 82 | */ 83 | 84 | minMaxFromList(); 85 | 86 | /** 87 | * Merge two unsorted arrays into a single sorted array using Java 8 streams 88 | * Write a Java 8 program to merge two unsorted arrays into a single-sorted array using the stream API. 89 | */ 90 | mergeUnsortedArrayIntoSorted(); 91 | 92 | /** 93 | * Merge two unsorted arrays into a single sorted array without duplicates 94 | * Write a Java 8 program to merge two unsorted arrays into a single-sorted array without duplicates. 95 | */ 96 | mergeUnsortedArrayIntoSortedWithoutDuplicate(); 97 | 98 | /** 99 | * Get the three maximum and three minimum numbers from a given list of integers 100 | * 101 | * Write a Java 8 program to get the three maximum and three minimum numbers from a given list of integers. 102 | */ 103 | 104 | min3max3(); 105 | 106 | /** 107 | * Check if two strings are anagrams or not using Java 8 streams 108 | * Write a Java 8 program to check if two strings are anagrams or not using the stream API and lambda expressions. 109 | */ 110 | 111 | isAnagram(); 112 | isAnagram2(); 113 | 114 | /** 115 | * Find the sum of all digits of a number in Java 8 116 | * 117 | * Write a Java 8 program to find the sum of all digits of a given number. 118 | * 119 | */ 120 | sumOf(); 121 | /** 122 | * Find the second-largest number in an integer array 123 | * 124 | * Write a Java 8 program to find the second-largest number in an integer array. 125 | */ 126 | secondLargestNumberFromList(); 127 | 128 | 129 | /** 130 | * Sort a list of strings according to the increasing order of their length 131 | * 132 | * Write a Java 8 program to sort a given list of strings according to the increasing order of their length. 133 | */ 134 | sortByLengthOfList(); 135 | /** 136 | * Find the sum and average of all elements in an integer array 137 | * 138 | * Write a Java 8 program to find the sum and average of all elements in an integer array. 139 | */ 140 | 141 | calculateAndSumAndAverage(); 142 | /** 143 | * Find the common elements between two arrays 144 | * 145 | * Write a Java 8 program to find the common elements between two arrays using streams. 146 | */ 147 | commonElements(); 148 | /** 149 | * Reverse each word of a string using Java 8 streams 150 | * 151 | * Write a Java 8 program to reverse each word of a given string using the stream API and lambda expressions 152 | */ 153 | reverseEachWord(); 154 | 155 | /** 156 | * Find the sum of the first 10 natural numbers 157 | * 158 | * Write a Java 8 program to find the sum of the first 10 natural numbers using streams. 159 | */ 160 | 161 | int sumOf10NaturalNumber = IntStream.rangeClosed(1, 10) 162 | .sum(); 163 | System.out.println(sumOf10NaturalNumber); 164 | 165 | /** 166 | * Reverse an integer array 167 | * 168 | * Write a Java 8 program to reverse an integer array. 169 | */ 170 | reversedArray(); 171 | 172 | /** 173 | * Find the most repeated element in an array 174 | * 175 | * Write a Java 8 program to find the most repeated element in an array. 176 | */ 177 | 178 | mostRepeatedElement(); 179 | 180 | /** 181 | * Check if a string is a palindrome using Java 8 streams 182 | * 183 | * Write a Java 8 program to check if a given string is a palindrome using the stream API and lambda expressions. 184 | */ 185 | 186 | checkIsTheStringPalindrome(); 187 | 188 | /** 189 | * Find strings in a list that start with a number 190 | * 191 | * Given a list of strings, write a Java 8 program to find the strings that start with a number. 192 | */ 193 | 194 | stringsStartsWithNumber(); 195 | 196 | /** 197 | * Extract duplicate elements from an array 198 | * 199 | * Write a Java 8 program to extract duplicate elements from an array. 200 | */ 201 | 202 | extractDuplicateElements(); 203 | 204 | /** 205 | * Print duplicate characters in a string 206 | * Write a Java 8 program to print the duplicate characters in a string. 207 | */ 208 | 209 | duplicateCharactersInString(); 210 | 211 | /** 212 | * Find the first repeated character in a string 213 | * Write a Java 8 program to find the first repeated character in a string. 214 | */ 215 | firstRepeatedCharacter(); 216 | 217 | /** 218 | * Find the first non-repeated character in a string 219 | * 220 | * Write a Java 8 program to find the first non-repeated character in a string. 221 | */ 222 | firstNonRepeatingCharacter(); 223 | 224 | /** 225 | * Generate the Fibonacci series 226 | * 227 | * Write a Java 8 program to generate the Fibonacci series. 228 | */ 229 | generateFibonacciSeries(); 230 | 231 | /** 232 | * Print the first 10 odd numbers 233 | * 234 | * Write a Java 8 program to print the first 10 odd numbers. 235 | */ 236 | firstTenOddNumbers(); 237 | 238 | /** 239 | * Get the last element of an array 240 | * 241 | * Write a Java 8 program to get the last element of an array. 242 | */ 243 | 244 | lastElementInTheArray(); 245 | 246 | /** 247 | * Calculate the age of a person in years 248 | * 249 | * Write a Java 8 program to calculate the age of a person in years given their birthday. 250 | */ 251 | calculatePersonAgeInYear(); 252 | } 253 | 254 | private static void calculatePersonAgeInYear() { 255 | LocalDate birthDate = LocalDate.of(1998, 8, 17); 256 | LocalDate currentDate = LocalDate.now(); 257 | int age = Period.between(birthDate, currentDate).getYears(); 258 | System.out.println("Age of the person is: " + age); 259 | } 260 | 261 | private static void lastElementInTheArray() { 262 | int[] intArray = {0,1,2,3,4,5}; 263 | Integer lastElementInTheArray = Arrays.stream(intArray) 264 | .boxed() 265 | .reduce((first, second) -> second).orElse(-1); 266 | System.out.println("\nlast elements in the array " + lastElementInTheArray); 267 | } 268 | 269 | private static void firstTenOddNumbers() { 270 | Stream.iterate(1,i->i+2) 271 | .limit(10) 272 | .forEach(System.out::print); 273 | } 274 | 275 | private static void generateFibonacciSeries() { 276 | 277 | Stream.iterate(new int[]{0,1},t->new int[]{ t[1], t[0]+ t[1] }) 278 | .limit(10) 279 | .map(t->t[0]) 280 | .forEach(System.out::print); 281 | 282 | Function> intArraytoListOFInt = array -> Arrays.stream(array).boxed() 283 | .collect(toList()); 284 | List collect = Stream.iterate(new int[]{0, 1}, t -> new int[]{t[1], t[0] + t[1]}) 285 | .limit(10) 286 | .map(intArraytoListOFInt) 287 | .flatMap(List::stream) 288 | .distinct() 289 | .collect(toList()); 290 | System.out.println(collect); 291 | } 292 | 293 | private static void firstNonRepeatingCharacter() { 294 | String tempStr = "rohitrohi"; 295 | System.out.println (Arrays.stream (tempStr.split ("")) 296 | .filter (str -> tempStr.indexOf (str) == tempStr.lastIndexOf (str)) 297 | .findFirst () 298 | .orElse ("")); 299 | 300 | } 301 | 302 | private static void checkIsTheStringPalindrome() { 303 | String str = "momd"; 304 | String temp = str.replaceAll("\\s+", "").toLowerCase(); 305 | System.out.println("is palindrome string " +IntStream.range(0, temp.length() / 2) 306 | .noneMatch(i -> temp.charAt(i) != temp.charAt(temp.length() - i - 1))); 307 | } 308 | 309 | private static void firstRepeatedCharacter() { 310 | String word = "rohttoh"; 311 | System.out.println (Arrays.stream (word.split ("")) 312 | .filter (str -> word.indexOf (str) != word.lastIndexOf (str)) 313 | .findFirst ().orElse ("")); 314 | } 315 | 316 | private static void duplicateCharactersInString() { 317 | String word = "rohttoh"; 318 | System.out.println ("original String " + word); 319 | 320 | 321 | System.out.println (Arrays.stream (word.split ("")) 322 | .filter (str -> word.indexOf (str) != word.lastIndexOf (str)) 323 | .map (str -> str.charAt (0)) 324 | .collect (toList ())); 325 | } 326 | 327 | private static void extractDuplicateElements() { 328 | List duplicateElements = of(1, 2,2,2,3, 3, 4, 5,1,1,56, 7, 8, 9, 10); 329 | 330 | System.out.println("maxed Elements " + duplicateElements); 331 | 332 | List extractDuplicateElements = duplicateElements.stream() 333 | .filter(element -> duplicateElements.indexOf(element) 334 | != duplicateElements.lastIndexOf(element)) 335 | .distinct() 336 | .collect(toList()); 337 | 338 | 339 | System.out.println("extract duplicates elements from " + extractDuplicateElements); 340 | } 341 | 342 | private static void stringsStartsWithNumber() { 343 | String [] words= {"rohit","foo","nemo","target1","12Target","2robot"}; 344 | 345 | System.out.println("original Strings " + Arrays.toString(words)); 346 | 347 | List stringStartNumber = Arrays.stream(words) 348 | .filter(word -> Character.isDigit(word.charAt(0))) 349 | .toList(); 350 | System.out.println("strings started with a number " + stringStartNumber); 351 | } 352 | 353 | private static void mostRepeatedElement() { 354 | int [] elements = {2,3,1,4,4,1,4,333,3,333,2,2,2,5,222}; 355 | 356 | System.out.println("original Array" + Arrays.toString(elements)); 357 | Function, Integer> maxValuesKey = integerLongMap -> 358 | integerLongMap.entrySet() 359 | .stream() 360 | .max(Map.Entry.comparingByValue()) 361 | .map(Map.Entry::getKey) 362 | .orElse(Integer.MAX_VALUE); 363 | 364 | Integer maxDuplicateValue = Arrays.stream(elements) 365 | .boxed() 366 | .collect(collectingAndThen(groupingBy(Function.identity(), 367 | counting()), maxValuesKey)); 368 | 369 | System.out.println("max duplicate value in the array "+maxDuplicateValue); 370 | } 371 | 372 | private static void reversedArray() { 373 | int [] numberArray ={1,2,3,4,5,6,7,8,9,10}; 374 | System.out.println("original array" + Arrays.toString(numberArray)); 375 | int[] reversedArray = IntStream.rangeClosed(1, numberArray.length) 376 | .map(number -> numberArray[numberArray.length - number]) 377 | .toArray(); 378 | System.out.println("reversedArray Array" + Arrays.toString(reversedArray)); 379 | } 380 | 381 | private static void reverseEachWord() { 382 | String stmt = "java is OOP language"; 383 | String reverseEachWord = Arrays.stream(stmt.split(" ")) 384 | .map(word -> new StringBuffer(word).reverse()) 385 | .collect(joining(" ")); 386 | System.out.println(reverseEachWord); 387 | } 388 | 389 | private static void commonElements() { 390 | List oneToTen = of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); 391 | List twoToTen = of(2, 3, 4, 5, 6, 7, 8, 9, 10); 392 | List commonElements = oneToTen.stream() 393 | .filter(twoToTen::contains) 394 | .toList(); 395 | System.out.println(commonElements); 396 | } 397 | 398 | private static void calculateAndSumAndAverage() { 399 | List oneToTen = of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); 400 | 401 | IntSummaryStatistics summaryStatistics = oneToTen.stream() 402 | .collect(summarizingInt(Integer::intValue)); 403 | System.out.println(summaryStatistics.getSum()); 404 | System.out.println(summaryStatistics.getAverage()); 405 | } 406 | 407 | private static void sortByLengthOfList() { 408 | List names = Arrays.asList("rohit", "urmila", "rohit", "urmila", "ram", "sham", "sita", "gita"); 409 | names.stream() 410 | .sorted(Comparator.comparingInt(String::length)) 411 | .forEach(System.out::println); 412 | } 413 | 414 | private static void secondLargestNumberFromList() { 415 | List oneToTen = of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); 416 | Integer secondLarge = oneToTen.stream() 417 | .sorted(reverseOrder()) 418 | .skip(1) 419 | .findFirst() 420 | .orElse(Integer.MAX_VALUE); 421 | 422 | System.out.println(secondLarge); 423 | } 424 | 425 | private static void sumOf() { 426 | List oneToTen = of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); 427 | System.out.println(oneToTen.stream() 428 | .mapToInt(Integer::intValue) 429 | .sum()); 430 | } 431 | 432 | private static boolean isAnagram2() { 433 | 434 | String string1 = "listen"; 435 | String string2 = "silent"; 436 | 437 | String join1 = Arrays.stream(string1.split("")) 438 | .sorted() 439 | .collect(joining("")); 440 | String join2 = Arrays.stream(string2.split("")) 441 | .sorted() 442 | .collect(joining("")); 443 | return join1.equals(join2); 444 | 445 | } 446 | 447 | private static void isAnagram() { 448 | char[] splitIt = "listen".toCharArray(); 449 | char[] splitIt2 = "silent".toCharArray(); 450 | 451 | Arrays.sort(splitIt); 452 | Arrays.sort(splitIt2); 453 | if (Arrays.equals(splitIt,splitIt2)) { 454 | System.out.println("is Anagram"); 455 | }else{ 456 | System.out.println("is not anagram"); 457 | } 458 | 459 | } 460 | 461 | private static void min3max3() { 462 | List randomNumbers = of(12, 32, 2, 4, 777, 5, 32, 890, 422, 44, 99, 43); 463 | List min3 = randomNumbers.stream() 464 | .sorted(Integer::compare) 465 | .limit(3) 466 | .collect(toList()); 467 | 468 | List max3 = randomNumbers.stream() 469 | .sorted((x, y) -> Integer.compare(y, x)) 470 | .limit(3) 471 | .collect(toList()); 472 | System.out.println(min3); 473 | System.out.println(max3); 474 | } 475 | 476 | private static void mergeUnsortedArrayIntoSortedWithoutDuplicate() { 477 | int [] randomNumbers ={12, 32, 2, 4, 777, 5, 32, 890, 422, 44, 99, 43}; 478 | int [] randomNumber2 = {4, 32, 2, 5, 6, 78, 98, 53, 90}; 479 | 480 | System.out.println(Arrays.toString(IntStream.concat(Arrays.stream(randomNumbers), Arrays.stream(randomNumber2)) 481 | .distinct() 482 | .toArray())); 483 | } 484 | 485 | private static void mergeUnsortedArrayIntoSorted() { 486 | int [] randomNumbers ={12, 32, 2, 4, 777, 5, 32, 890, 422, 44, 99, 43}; 487 | int [] randomNumber2 = {4, 3, 2, 5, 6, 78, 98, 53, 90}; 488 | 489 | int[] sortedArrayByMergingTwoArray = IntStream.concat(Arrays.stream(randomNumbers), 490 | Arrays.stream(randomNumber2)).sorted().toArray(); 491 | System.out.println(Arrays.toString(sortedArrayByMergingTwoArray)); 492 | } 493 | 494 | private static void minMaxFromList() { 495 | List randomNumbers = of(12, 32, 2, 4, 777, 5, 32, 890, 422, 44, 99, 43); 496 | Integer maxNumber = randomNumbers.stream() 497 | .max(Integer::compareTo) 498 | .orElse(Integer.MAX_VALUE); 499 | Integer minValue = randomNumbers.stream() 500 | .min(Integer::compareTo) 501 | .orElse(Integer.MIN_VALUE); 502 | System.out.println(maxNumber); 503 | System.out.println(minValue); 504 | IntSummaryStatistics summaryStatistics = randomNumbers.stream() 505 | .collect(summarizingInt(Integer::intValue)); 506 | System.out.println(summaryStatistics.getMax()); 507 | System.out.println(summaryStatistics.getMin()); 508 | System.out.println(summaryStatistics.getCount()); 509 | System.out.println(summaryStatistics.getSum()); 510 | System.out.println(summaryStatistics.getAverage()); 511 | } 512 | 513 | private static void multipleOf5() { 514 | List randomNumbers = of(12, 32, 2, 4, 777, 5, 32, 890, 422, 44, 99, 43); 515 | List multipleOf5 = randomNumbers.stream() 516 | .filter(n -> n % 5 == 0) 517 | .collect(toList()); 518 | System.out.println(multipleOf5); 519 | } 520 | 521 | private static void joinListOfStrings() { 522 | List languageList = of("java", "c++", "c", "C sharp", "python", "kotlin", "scala"); 523 | String joinWithPrefixSuffixAndDelimiter = languageList 524 | .stream() 525 | .collect(joining(",", "[", "]")); 526 | System.out.println(joinWithPrefixSuffixAndDelimiter); 527 | } 528 | 529 | private static void reverseSortedList() { 530 | List randomNumbers = of(12, 32, 2, 4, 777, 5, 32, 890, 422, 44, 99, 43); 531 | //this is throwing unsupportedOprerationException because we are 532 | // tries to sort ImmutableList that is not allowed . 533 | // randomNumbers.sort(Comparator.reverseOrder()); 534 | System.out.println(randomNumbers); 535 | List sortInReverse = randomNumbers.stream() 536 | .sorted((x, y) -> Integer.compare(y, x)) // reverse sort 537 | .collect(toList()); 538 | System.out.println(sortInReverse); 539 | 540 | List sortListReverse = randomNumbers.stream() 541 | .sorted(reverseOrder()) 542 | .collect(toList()); 543 | System.out.println(sortListReverse); 544 | 545 | } 546 | 547 | private static void wordFrequency() { 548 | List names = Arrays.asList("rohit", "urmila", "rohit", "urmila", "ram", "sham", "sita", "gita"); 549 | Map frequencyWords = names.stream() 550 | .collect(groupingBy(Function.identity(), counting())); 551 | System.out.println(frequencyWords); 552 | } 553 | 554 | private static void characterFrequency() { 555 | String name = "rohitroh"; 556 | Map characterFrequency = Arrays.stream(name.split("")) 557 | .collect(groupingBy(Function.identity(), counting())); 558 | System.out.println(characterFrequency); 559 | 560 | 561 | Map collected = name.chars() 562 | .mapToObj(ch -> (char) ch) 563 | .collect(groupingBy(Function.identity(), counting())); 564 | System.out.println(collected); 565 | 566 | Map countCharacter = Arrays.stream(name.split("")) 567 | .collect(groupingBy(Function.identity(), 568 | collectingAndThen(counting(), Long::intValue))); 569 | System.out.println(countCharacter); 570 | } 571 | 572 | private static void removeDuplicateFromList() { 573 | List oneToTen = of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); 574 | List removeDuplicate = oneToTen.stream() 575 | .distinct() 576 | .collect(toList()); 577 | 578 | System.out.println(removeDuplicate); 579 | Set removeDuplicateWithoutOrder 580 | = oneToTen.stream() 581 | .collect(toSet()); 582 | System.out.println(removeDuplicateWithoutOrder); 583 | 584 | List uniqueElement = oneToTen 585 | .stream() 586 | .filter(number -> oneToTen.indexOf(number) 587 | == oneToTen.lastIndexOf(number)) 588 | .collect(toList()); 589 | System.out.println(uniqueElement); 590 | } 591 | 592 | private static void separationOfEvenOddNumberInList() { 593 | List oneToTen = of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); 594 | 595 | Collection> evenOddList = oneToTen.stream() 596 | .collect(collectingAndThen(partitioningBy(i -> i % 2 == 0), 597 | Map::values)); 598 | 599 | System.out.println(evenOddList); 600 | } 601 | 602 | private static void separationOfEvenOddNumberInMap() { 603 | List oneToTen = of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); 604 | 605 | Map> evenAddOddSeparation = oneToTen.stream() 606 | .collect(partitioningBy(i -> i % 2 == 0)); 607 | 608 | System.out.println(evenAddOddSeparation); 609 | } 610 | } 611 | --------------------------------------------------------------------------------