Search results for: 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

class Test { public static void main(String[] args) { String str = “alegru”; char c = str.charAt(2); System.out.println(c); } } Output: e class Test { public static void main(String[] args) { String str = “alegru”; int index = str.indexOf(‘l’); System.out.println(index); } } Output: 1 class Test { public static void main(String[] args) { String str…

Read More Working With Strings in Java – Part 1

Java Strings are one of the most commonly used data types in Java programming, and understanding how to work with them effectively is essential for any Java developer. In this tutorial, I will cover everything you need to know about Java Strings, from the basics of creating and manipulating Strings to advanced concepts like regular…

Read More Mastering Java String: A Comprehensive Tutorial

import java.util.Arrays; class Test { public static void main(String[] args) { String[] names = {“John”, “Steve”, “Melissa”, “Maria”}; System.out.println(Arrays.toString(names)); } } Output: [John, Steve, Melissa, Maria]   copyOf(type [] a, int n) Returns a new copy of the array, which consists of the first n of its elements. import java.util.Arrays; class Test { public static…

Read More Arrays class in Java

class Test { int[] a; int b[]; int []c; } class Test { int[] a = new int[100]; int b[] = new int[]{1, 2, 3}; int[] c = {1, 2, 3}; } class Test { public static void main(String[] args) { // initialize the array int[] a = new int[3]; // add elements to array…

Read More Java Arrays