Java

public int compareTo(Object obj) class Test { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add(“Python”); list.add(“Java”); list.add(“JavaScript”); list.add(“Ruby”); list.add(“Kotlin”); System.out.println(“Before sorting:”); System.out.println(list); // sorting the list Collections.sort(list); System.out.println(“After sorting:”); System.out.println(list); } } Output: Before sorting: [Python, Java, JavaScript, Ruby, Kotlin] After sorting: [Java, JavaScript, Kotlin, Python, Ruby] Sorting the array of…

Read More Comparable in Java

Queue queue1 = new LinkedList(); Queue queue2 = new PriorityQueue(); class Test { public static void main(String[] args) { Queue<String> queue = new LinkedList(); queue.offer(“Steve”); queue.offer(“Megan”); queue.offer(“Ryan”); queue.offer(“Melissa”); System.out.println(queue); } } Output: [Steve, Megan, Ryan, Melissa]   We can also use the add(Object o) method to add elements to the Queue. In this example, we will…

Read More Queue in Java

HashMap<K, V> map = new HashMap<>(); Map<Integer, String> map = new HashMap<>(); 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”); System.out.println(map); } } Output: {1=Java, 3=Kotlin, 7=Python, 10=Ruby} class Test { public static void main(String[] args) { Map<Integer, String> map…

Read More Map in Java

HashSet<String> set = new HashSet<>(); Set<String> set = new HashSet<>(); class Test { public static void main(String[] args) { Set<String> set = new HashSet<>(); set.add(“Java”); set.add(“Kotlin”); set.add(“Python”); set.add(“Ruby”); System.out.println(set); } } Output: [Java, Ruby, Kotlin, Python] class Test { public static void main(String[] args) { Set<String> set = new HashSet<>(); set.add(“Kotlin”); set.add(“Java”); set.add(“Ruby”); set.add(“Python”); Set<String>…

Read More Set in Java

LinkedList<String> list = new LinkedList<>(); List<Integer> list = new LinkedList<>(); class Test { public static void main(String[] args) { List<String> list = new LinkedList<>(); list.add(“Steve”); list.add(“Megan”); list.add(“Melissa”); list.add(“Ryan”); System.out.println(list); } } Output: [Steve, Megan, Melissa, Ryan] class Test { public static void main(String[] args) { List<String> list = new LinkedList<>(); list.add(“Steve”); list.add(“Megan”); list.add(“Melissa”); list.add(“Ryan”); List<String>…

Read More LinkedList in Java

ArrayList<String> list = new ArrayList<>(); List<Integer> list = new ArrayList<>(); class Test { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add(“Steve”); list.add(“Megan”); list.add(“Melissa”); list.add(“Ryan”); System.out.println(list); } } Output: [Steve, Megan, Melissa, Ryan]   Using the addAll() method, we can add all the elements of an existing list to the newly created…

Read More ArrayList in Java

Iterator<T> iterator()   Method Description   public boolean add(E e) It is used to insert an element in this collection.   public boolean addAll(Collection<? extends E> c) It is used to insert the specified collection elements in the invoking collection.   public boolean remove(Object element) It is used to delete an element from the collection.…

Read More Java Collections

In Java, there are three main types of loops: while loops, do-while loops, and for loops. These loop structures are similar in many ways, but they differ in some key aspects that make them useful for different scenarios. This tutorial will introduce you to the concepts and syntax behind Java while and do-while loops. You…

Read More ‘while’ and ‘do-while’ Loops: The Secret to Efficient Java Programming

class Test { public static void main(String[] args) { String str = “ALEGRUTECHBLOG”; String strInLowerCase = str.toLowerCase(); System.out.println(strInLowerCase); } } Output: alegrutechblog   toUpperCase() Converts all of the characters in this String to the upper case using the rules of the default locale.   class Test { public static void main(String[] args) { String str…

Read More Working With Strings in Java – Part 2