Skip to main content

Posts

Showing posts from October, 2021

A Guide to Iterator in Java | hasNext(), next(), remove(), forEachRemaining() Methods

What is Iterator in Java? Java Iterator is an Interface that is used to iterate over a collection of java object one by one.  When we want to access collection elements, add/remove or process the elements. In order to do all this processing through a Java program, we should be able to traverse through the collection that we are using. This is where the iterator is used. Java Iterator is a collection framework interface and it is part of the java.util package. In order to use an Iterator, we need to get the iterator object using the iterator() method of the collection interface. Java Iterator supports only Forward Direction Iteration. So it is also know as Uni-Directional Cursor.  Java Iterator consists 4 methods : hasNext() next() remove() forEachRemaining() Syntax : List<String> list = new ArrayList<>(); Iterator<String> iterator = list.iterator(); 1. hasNext() Method in Iterator The hasNext() method is used for checking if there's at least one element left

Java Iterator HackerRank solution with Explanation | Programming Blog

Problem Description : In this problem you need to complete a method func . The method takes an ArrayList as input. In that ArrayList there is one or more integer numbers, then there is a special string "###", after that there are one or more other strings. A sample ArrayList may look like this: element[0]=>42 element[1]=>10 element[2]=>"###" element[3]=>"Hello" element[4]=>"Java"  You have to modify the func method by editing at most 2 lines so that the code only prints the elements after the special string "###". For the sample above the output will be: Hello Java Solution : import java.util.*; public class Main{        static Iterator func(ArrayList mylist) {             Iterator it = mylist.iterator();              while (it.hasNext()) {          Object element = it.next();          if (element instanceof String)             break;         }       return it;    }    @SuppressWarnings({ "unchecked" })  

Prime Checker HackeRank Solution in Java with Explanation

Java solution for Prime Checker HackerRank Problem Problem Description : You are given a class Solution and its main method in the editor. Your task is to create a class Prime . The class Prime should contain a single method checkPrime .  The locked code in the editor will call the checkPrime method with one or more integer arguments. You should write the checkPrime method in such a way that the code prints only the prime numbers . Sample Input : 2 1 3 4 5 Sample Output : 2 2 2 3 2 3 5  Output Format : There will be only four lines of output. Each line contains only prime numbers depending upon the parameters passed to checkPrime in the main method of the class Solution . In case there is no prime number, then a blank line should be printed. Solution : What is prime number? A number that can be divided exactly only by itself and 1, for example 3, 5, 7.... So we have to create new class called Prime and method called checkPrime(). In checkPrime() method we have to use var-args

Can You Access? HackerRank solution in Java | Inner Class in Java

Can You Access HackerRank Solution Problem Description : You are given a class Solution and an inner class Inner.Private . The main method of class Solution takes an integer num as input. The powerof2 in class Inner.Private checks whether a number is a power of 2. You have to call the method powerof2 of the class Inner.Private from the main method of the class Solution . Sample Input and Output : Input : 8 Output : 8 is power of 2 An instance of class: Solution.Inner.Private has been created This problem solution based on Inner class. Learn more about Inner class in Java. Inner classes and its 4 types with example Solution : import java.io.*; import java.lang.reflect.*; import java.util.*; import java.util.regex.*; import java.security.*; public class Solution {     public static void main(String[] args) throws Exception {         DoNotTerminate.forbidExit();            try{             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));             int num

What is Inner Class in Java with examples | Types of inner classes | Nested Class

Inner class and its 4 types with example When a class defined inside another class known as inner class. public class OuterClass {     class InnerClass {              } } When to use Inner Classes? Without existing one type of object, if there is no chance of existing another type of object then we should go for inner classes. example, University consists of several colleges, without existing University there is no chance of colleges hence we have to declare college class inside University class. // Outer Class public class University {     // Inner Class     class College {             } } See another example. Map is group of key value pair and each key value pair is called Entry. Without existing Map object there is no chance of Entry object, hence Interface Entry is defined inside Map Interface. // Outer Interface public interface Map<K, V> {     // Inner Interface      interface Entry<K, V> {             } } Without existing outer class, there is no chance of existing i