java

import java.util.*; public class Test { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); System.out.println(numbers.stream().mapToInt(Integer::intValue).average().getAsDouble()); } } Output: 5.5   Here, we used the mapToInt(Integer::intValue) to convert the Stream<Integer> which is the result of the stream() method, to IntStream. import java.util.*; public class…

Read More Find the Average of Numbers in Java

public class Test { public static void main(String[] args) { int[] array = new int[]{7, 1, 9, 2, 5}; Arrays.sort(array); Arrays.stream(array).forEach(System.out::print); } } Output: 1 2 5 7 9   Note: values of type int will be automatically converted to the corresponding Wrapper class. And since the Integer Wrapper class implements Comparable, we can sort…

Read More Sort an Array in Java

import java.util.*; public class Test { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(Arrays.asList(7, 12, 5, 28, 97, 14, 2, 7, 8, 12, 19, 22)); Collections.sort(numbers); System.out.println(numbers); } } Output: [2, 5, 7, 7, 8, 12, 12, 14, 19, 22, 28, 97] Using the sort() method from ArrayList class public class Test…

Read More Sort a List in Java

class Test { public static void main(String[] args) { Map<Integer, String> map = new HashMap<>(); map.put(1, “Java”); map.put(7, “Python”); map.put(10, “Ruby”); map.put(3, “Kotlin”); for (Map.Entry<Integer, String> entry : map.entrySet()) { System.out.println(“Key = ” + entry.getKey() + “, Value = ” + entry.getValue()); } } } Output: Key = 1, Value = Java Key = 3,…

Read More Iterate over a Map in Java

public class Test { public static void main(String[] args) { int num = 8; int num2 = 9; if(num % 2 == 0) { System.out.println(“num is an even number!”); } else { System.out.println(“num is odd number!”); } if(num2 % 2 == 0) { System.out.println(“num2 is an even number!”); } else { System.out.println(“num2 is odd number!”);…

Read More Check if the Number is Even or Odd in Java

public class Test { public static void main(String[] args) { String str = “racecar”; System.out.println(str.equals(new StringBuilder(str).reverse().toString())); } } Output: true   We can do the same with the StringBuffer class since it also has the reverse() method. Using the for loop public class Test { public static void main(String[] args) { String str = “racecar”;…

Read More Check if String is Palindrome in Java

public class Test { public static void main(String[] args) { String str1 = “Learn Java Programming with alegrucoding.com”; String str2 = “Learn Java”; String str3 = “Simple Java Programming”; // check if str1 contains str2 if (str1.contains(str2)) { System.out.println(“str2 is present in str1”); } else { System.out.println(“str2 is not present in str1”); } // check…

Read More Check if a String Contains a Substring in Java

public class Test { public static void main(String[] args) { // create an object of Random class Random random = new Random(); // Generate random integer 0 to 100 int randomInteger1 = random.nextInt(101); // Generate random integer 0 to 10000 int randomInteger2 = random.nextInt(10001); // Generate random double value double randomDouble = random.nextDouble(); // Generate…

Read More Generate a Random Number in Java

public class Test { public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asList(7, 19, 14, 1, 12, 22, 18, 3, 28, 5)); System.out.println(“Maximum value: ” + Collections.max(list)); System.out.println(“Minimum value: ” + Collections.min(list)); } } Output: Maximum value: 28 Minimum value: 1 Finding the maximum and minimum values in List using the Java 8…

Read More Find Max and Min Values of a List in Java

Output: Java   Methods from the StringUtils class that we can use to get a substring from String in Java: import org.apache.commons.lang3.StringUtils; public class Test { public static void main(String[] args) { String str = “Hello, my name is Tom, and I live in USA”; String substringBefore = StringUtils.substringBefore(str, “,”); String substringAfter = StringUtils.substringAfter(str, “,”); String substringBetween = StringUtils.substringBetween(str,…

Read More Get Substring from String in Java

public class GenericClass <T> GenericClass<Type> obj = new GenericClass<Type>(); List<String> list = new ArrayList<>(); Map<Integer, String> map = new HashMap<>(); Set<String> set = new HashSet<>(); public class Test <T> { private T data; public Test(T data) { this.data = data; } public static void main(String[] args) { Test<Integer> test1 = new Test<>(10); Test<String> test2 =…

Read More Generics in Java with Examples