Saturday, December 31, 2022

5 Projects You can do to learn TensorFlow in 2024 - Best of Lot

Hello guys, if you want to learn TensorFlow and looking for project ideas then you have come to the right place. Earlier, I have shared best TensorFlow courses and in this article I am going to share 5 projects you can build to learn TensorFlow in 2024. To be honest, Learning artificial intelligence and machine learning is not that easy as everyone thinking like taking raw data and train the algorithm to learn it and then produce an output when you feed it with unknown data. The artificial intelligence is the science of making machine learning as human as well as solving problems that never seen before in a particular situation like what’s known as reinforcement learning.

Tuesday, December 27, 2022

10 Projects You can Build to Learn Golang in 2024

Hello guys, If you want to learn Golang and looking for project ideas to get hands-on practice then you have come to the right place. In the past, I have shared best Golang courses for beginners to learn Golang online and in this article, I am going to share best Golang project ideas for beginners to build and learn Go programming better. From my 20 years of experience in Tech and Programming, I can safely say that there is no better way to learn then actually doing work and building projects. When you go into build mode, your mind work differently as in order to build, actual need arise. For example, when you need to download data from API then you search how you can do that in Golang and then you learn about the classes and tools and that learning remains for long time. That's why I recommend every developer to build projects. 

Thursday, December 22, 2022

Visitor Design Patterns In Java Examples Tutorial

Hello guys, if you want to learn Visitor design pattern in Java then you have come to the right place. Earlier, I have covered many design patterns like Decorator, Strategy, State, Composite, Adapter, Command, Template, Factory, Observer and even few Microservice patterns like SAGA and Database per service and in this article, I will talk about Visitor Design Pattern and how to implement in Java. You will learn things like what is Visitor design pattern, what problem it solves, what are pros and cons of Visitor design pattern, when to use Visitor pattern as well as any alternatives of Visitor Pattern in Java. I will also show you a real world example of Visitor design pattern, but, before we get to the 5 best examples that will teach you all about design patterns in Java, let me tell you a little bit more about what it really is.

Saturday, December 17, 2022

Difference between 32-bit vs 64-bit JVM in Java?

Hello Java Programmers, if you want to learn Java virtual Machine in-depth and wondering what is the difference between a 32-bit and 64-bit JVM and which one should you use and why? then you have come to the right place. Earlier, I have shared the best JVM books and online JVM courses and in this article, I am going to talk about 32-bit vs 64-bit JVM and their pros and cons. This is also a common Java interview question for beginners and intermediate Java programmers. I have tried to answer this question to the point that's why this article is a short but informative one. You will find out what they are, how they are different, how much heap size, and the pros and cons of each of them. 

How to remove duplicate(s) from linked list in Java? Example Tutorial

Hello guys, if you are wondering how to find duplicates in a given linked list in Java then you have come to the right place. In the past, I have explained how to reverse a linked list in Java, find kth element from tail in linked list, find cycle on linked list, and in this article, I will show you how to find duplicate nodes in a given linked list. This is one of the classic linked list coding problem and I have also included this in my list of 30 common linked list problems for programmers. You can further check that list to practice more linked list programs and improve your understanding of linked lists and how to solve those problems. But, before we get into details of finding and printing duplicate nodes from the given linked list let's start with the basics. 

Friday, December 16, 2022

10 Examples Of Mockito + JUnit in Java for Unit Testing

Hello guys, if you are writing unit test in Java then you know how difficult it is to write especially if you are testing a class which is dependent upon other class like HttpClient and you cannot connect to actual server. At those time, a mocking library like Mockito comes to rescue. Given the increased focus on unit testing and code coverage, I have find myself using Mockito more and more along with JUnit in last a couple of years but I haven't written many articles on Mockito yet but that is chaging now. In this article, I am going to share 10 essential Mockito examples which I belive every Java programmer should know. But, before we get to the 10 best examples that will teach you everything there is to know about Mockito in Java, let me tell you a little bit more about what it really is.

Thursday, December 15, 2022

How to convert Java 8 Stream to Array and ArrayList in Java? Example Tutorial

It's relatively easy to convert a Stream to an array in Java 8 by using the toArray() method of java.util.Stream class. By using this method you can convert any type of Stream to a corresponding array like a Stream of Strings can be converted into an array of String, or a Stream of integers can be converted into an array of Integers. The Stream.toArray() method is also overloaded, the one which doesn't take any parameter returns an Object[] which might not be very useful, particularly if you want to convert Stream of T to an array of T.

Array length vs ArrayList Size in Java [Example]

One of the confusing parts in learning Java for a beginner to understand how to find the length of array and ArrayList in Java? The main reason for the confusion is an inconsistent way of calculating the length between two. Calling size() method on arrays and length, or even length() on ArrayList is a common programming error made by beginners. The main reason for the confusion is the special handling of an array in Java.  Java native arrays have built-in length attribute but no size() method while the Java library containers, known as Collection classes like ArrayList<>, Vector<>, etc,  all have a size() method. 

Wednesday, December 14, 2022

How to create an ArrayList from Array in Java? Arrays.asList() Example Tutorial

One of the common problems faced by junior and less experienced Java developers is converting an array to ArrayList e.g. they are getting an array from somewhere in their code and then want to create an ArrayList out of that so that they can add more elements and use other library methods which operate with ArrayList or List. The simplest way to convert an array to ArrayList is by using the Arrays.asList() method, which acts as a bridge between Collection classes and array data structure. This method returns a List that contains elements from an array. 

Monday, December 12, 2022

How to check If two Strings Array are equal in Java? Example Tutorial

Hello guys, if you are wondering how to check if two given String array are equal, I mean they contain same number of elements with same values and looking for solution then you have come to the right place. In the past, I have shared several coding questions on different data structures like linked list, binary tree, string, and even system design and today, we shall be working with arrays, Oh arrays are so pretty! And it’s very simple to learn. Having understood the concept of arrays the goal is to be able to check if two String arrays are equivalent. 

How to find Factorial in Java using Recursion and Iteration - Example Tutorial

Hello guys, if you are looking for a Java program to calculate factorial with and without recursion then you have come to the right place. Factorial is a common programming exercise that is great to learn to code and how to program. When I teach Java to new people, I often start with coding problems like prime numbers, Fibonacci series, and factorial because they help you to develop a coding sense and teach you how to write a program initially. In order to calculate factorial, you just need to know the factorial concepts from Mathematics, and rest I will explain this simple Java programming tutorial. 

Is it Possible to add static or private methods in Java interface?

Can you add a static or private method in an interface in Java? or is it possible to add a private or static method in Java interface? or can you add a non-abstract method on an interface in Java? are a couple of popular Java interview questions which often pop up during telephonic interviews. Well, prior to Java 8, it wasn't possible to add non-abstract methods in Java but nowadays you can add non-abstract static, default, and private methods in the Java interface. The static and default methods were supported as part of interface evolution in Java 8 and you can add private methods on an interface from Java 9 onwards.

10 Examples Of Scanner Class In Java

Hello guys, if you want to learn about Scanner class in Java, one of the most popular class to read input from command prompt or console then you have come to the right place. In this article, I will explain how to read input from command prompt, what is Scanner class and how you can use Scanner class to read various data types like String, int, long, boolean directly from console. You must have heard about famous nextInt(), nextDouble() methods which can read integer and double values from command prompt or console. But, before we get to the 10 best examples that will teach you everything you need to know about scanner class in Java, let me tell you a little bit more about what it really is.

Saturday, December 10, 2022

What is Diamond operator in Java? Example Tutorial

Hello guys, if you are reading Java code and come across closed angle bracket with a diamond like shape and wondering what they are and what they do then you have come to the right place. They are known as Diamond operator in Java and I will explain you what they are and where should you use them in this article. The Diamond operator is a relatively new operator in Java which was first introduced in JDK 7  to improve type inference and reduce boilerplate Java coding. It is denoted with a closed angle bracket that resembles the shape of a diamond (<>) and that's why it's called the Diamond operator. If used correctly it can reduce typing and boilerplate coding in Java and result in much cleaner and readable code especially when you use Generics.  

Parallel Array Sorting in Java - Arrays.parallelSort() Example

There are multiple ways to sort array in Java. For example, you can use Array.sort() method to sort any primitive or object array in Java but Java 8 presents a new feature that is useful to sort array’s elements. It can implemented by using the package “java.util.Arrays” which represents several methods to sort the array. The biggest benefit is that parallel array sorting is that its faster than any other sorting method due to usage of multithreading conception. So multiple threads can break the array into parts and work simultaneously to sort it. 

Difference between Thread vs Process in Java? Example

Thread and Process are two closely related terms in multi-threading and the main difference between Thread and Process in Java is that Threads are part of the process. i.e. one process can spawn multiple Threads. If you run a Java program in UNIX based system e.g. Linux and if that program creates 10 Threads, it still one process and you can find that by using ps -ef | grep identifier command which is one of the most popular use of grep command in UNIX, Where identifier is UNIX the text which can be used as regular expression to find that Java process.

Difference between yield and wait method in Java? Answer

Yield vs wait in Java
The yield and wait methods in Java, though both are related to Threads,  are completely different to each other. The main difference between wait and yield in Java is that wait() is used for flow control and inter-thread communication while yield is used just to relinquish CPU to offer an opportunity to another thread for running. In this Java tutorial, we will what are differences between the wait and yield method in Java and when to use wait() and yield(). What is important for a Java programmer is not only to understand the difference between the wait() and yield() method but also to know the implications of using the yield method. 

10 points about wait(), notify() and notifyAll() in Java Thread?

If you ask me one concept in Java that is so obvious yet most misunderstood, I would say the wait(), notify(), and notifyAll() methods. They are quite obvious because they are one of the three methods of a total of 9 methods from java.lang.Object but if you ask when to use the wait(), notify() and notfiyAll() in Java, not many Java developers can answer with surety. The number will go down dramatically if you ask them to solve the producer-consumer problem using wait() and notify()

Friday, December 9, 2022

How to run Threads in an Order in Java - Thread.Join() Example

Hello Java programmers, if you need to execute multiple threads in a particular order, for example if you have three threads T1, T2 and T3 and we want to execute them in a sequence such that thread 2 starts only when first thread finishes it job and T3 starts after T2, but with multithreading in Java there is no guarantee. Threads are scheduled and allocated CPU by thread scheduler which you cannot control but you can impose such ordering by using Thread.join() method. When you start a thread its not guaranteed that which thread will start first and whether the thread started first will finish first, if your application's logic depends upon a sequence its better to do all those operation on single thread because if all code is confined to one thread it will execute in order they were written provided some JIT optimization.

10 Examples Of Lombok Libarary In Java

Hello guys, if you are working in Java then you may have heard about Lombok, one of the popular Java library which alleviate pain Java developer by removing boiler plate code and provides improve development experience. Yes, Lombok can remove a lot of code like the one you put on equals() and hashCode, getter and setter, toString and constructor and much more. This means you can just create a Java class with fields much like Record of Java SE 17 and you can use it like a fully functional Java class, I mean you can create object using constructor or Builder and you can get and set values using gettter and setter, only difference is they are not visible in code. But then you may be thinking how does it work? Where does it get those getter and setter? why not compiler raise any problem?

Thursday, December 8, 2022

10 Examples Of Ternary Operator In Java

Hello guys, if you are wondering how to use ternary operator in Java then you have come to the right place. Ternary operator is a great operator and you can use ternary operator to replace a simple if-else statement. Since ternary operator allows you to write code in one line, its much more succinct and readable and that's what many Java developer love it, including me. In the last article, I shared 10 example of XOR bitwise operator in Java and in this article, I am going to share 10 example of ternary operator in Java so that you not only know what is ternary operator but also you can use it effectively to write better Java code. But, before we get to the 10 best examples that will teach you everything there is to know about ternary operators in Java, let me tell you a little bit more about what it all really is.

Tuesday, December 6, 2022

10 Examples of XOR Operator In Java

Hello guys, if you are wondering what does the XOR operator do in Java then you have come to the right place. XOR is a bitwise operator in Java, much like AND and OR and its very important because you can perform XOR operation using this operator. If you don't remember, XOR return true if the two values which you are comparing are different like one is true and other is false or vice-versa but it returns false or 0 if both operands are same like both are true or both are false. By using this technique you can solve many coding problems as you can check if the bits you are comparing is same or not. In this article, I will show you different example of XOR operator in Java after that you will understand this operator better. 

Top 35 Java String Interview Questions with Answers for 2 to 5 Years Experienced Programmers

Hello Java Programmers, if you are preparing for a Java developer interview and want to refresh your knowledge about  String class in Java then you have come to the right place. In the past, I have shared 130+ Core Java Interview Questions and 21 String Coding Problems for Interviews and in this article, I am going to share 35 Java String Questions for Interviews. The  String class and concept is a very important class in Java. There is not a single Java program out there which is not use String objects and that's why it's very important from the interview point of view as well. In this article, I am going to share 35 String-based questions from different Java interviews.

Monday, December 5, 2022

How to calculate perimeter and area of square in Java? Example Tutorial

If you are looking for a solution of problem how to calculate perimeter and area of a given Square in Java then you have come at the right place. In this article, I have given step by step solution of this common coding problem. This was actually a homework exercise when I was learning Java program and since I was good at Maths, I know how to calculate Perimeter and Area of Circle and Square but big challenge for me was to convert that knowledge into code. Another big challenge for me was how to take input from user actually that was the mistake I made when I first solved this problem. 

Saturday, December 3, 2022

How to convert float, double, String, Map, List to Set, Integer, Date in Java - Example Tutorial

Hello guys, converting one data type to another, for example String to Integer or String too boolean is a common task in Java and every Java programmer should be familiar with how to convert common data types like String, Integer, Long, Float, Double, Date, List, Map, Set, to each other. In the past, I have shared several tutorial where I have shown you how to carry out such conversion in Java and this article is nothing but a collection of such tutorial so that you can learn all of that knowledge in one place. You can also bookmark this page as I will be adding new data type conversion tutorials into this list as and when I write them, and you can also suggest if you struggle to convert a particular type to another and I will try to cover them here. 

Friday, December 2, 2022

How to solve word break problem in Java using dynamic programming? Example

Problem Statement:
You are given a dictionary of words and the input string. Determine input string can be segmented into a space-separated sequence of given dictionary words.

Note: This question is based on dynamic programming and asked multiple times in top product-based companies.

Inputs:

         Dict = {i, like, am, boy, e, o, dog, cat, g};

        word = "iIikedog"  --------can be segmented into space-separated words--------> i, like, dog




Ask yourself that 'I', 'like', and 'dog' are presented in the dictionary?

Yes, so we can say that the given string can be segmented.

Now let's take another example.

         word = "ilikecatsanddog" --------- can be segmented as-----> i, like, cats, and, dog

                                                                                                              i, like, cat, s, and, dog

We can see that the words 'cats', 'and', 's' are not present in the dictionary so such a string can't be segmented into space-separated words.



Let's try to solve it.....

Like another dynamic programming problem, we will create a matrix and will use previously calculated results to calculate the current result.


Consider the given string as an array like this.

i

l

i

k

e

d

o

g

 

The columns and rows represent the same given string in the matrix.



Now let's try to understand what each cell represents in the matrix.

The cells can have two kinds of values either '0' or '1'. Suppose the cells (3,6) and (3,7) have the value '1' this means the substrings from 3 to 6 and 3 to 7 are present in the given input dictionary.


If you notice the cell (5,3) and the corresponding substring in the given word the direction is reverse and there is no point in considering the reverse computation of the given string. So we will mark all such cells as '0'.

We filled half of the matrix with value '0' so we no need to perform the reverse computations for these cells. This way we can save both memory and time.

Now start filling rest of the cells manually by comparing the row-column pair value against the given dictionary.

So the cell (0, 0) i.e. 'i' is present in the dictionary so matrix[0][0] = 1.

Similarly cell (4, 4) i.e. 'e' is also present in the dictionary so matrix[4][4] = 1.

Now let's take one substring 'ilike' and see the logic to solve this problem.

i

l

i

k

e

d

o

g

0

1

2

3

4

5

6

7


          

          i      l     i     k      e

          0     1    2    3      4


We will try to separate this string into 2 parts in all the possible ways.

Let say we have first 2 parts: 

(0, 0)--------> 'i' and (1, 1)----------> 'l' 

matrix[0][0] && matrix[1][1]

                  1 && 0

So 'il' is not present in the dictionary.

Consider another combination.

(4, 3)--------> 'k' and (4, 4)----------> 'e' 

matrix[4][3] && matrix[4][4]

                  0 && 1



This is false means 'ke' is not present in the dictionary.

So I will keep on taking the substrings from the given word to be space separated and divide them into 2 parts in different ways and will check against the matrix that if both the parts are true i.e. '1' means the substring is present in the dictionary.


So finally we will consider the entire given word and divide it into 2 parts in all the possible ways and we will use the values of the previously computed cells to calculate the value of the marked cell and if the the value is '1' means the given word can be separated into the space separated segments.

Start Implementing it...

Consider below code snippet.

i

l

i

k

e

d

o

g

0

1

2

3

4

5

6

7



       For i = 0 and N = 7


      for(int k = 0; k < N+1; k++){



              if(matrix[i][i+k]) && matrix[k+1][N])

              {

                    return true;

              }

     }


The if condition shows the logic to divide the given word into 2 parts in all possible ways.


Complete Code:

public class Main {

   

    public String wordBreakProblem(String word, Set<String> dict){

            int matrix[][] = new int[word.length()][word.length()];

            

// fill all the cells with '-1'.

            for(int i=0; i < matrix.length; i++){

                for(int j=0; j < matrix[i].length ; j++){

                    matrix[i][j] = -1; 

                }

            }

            

            

//If the substring is present in the dictionary then fill the corresponding cell with non-negative value.

            for(int l = 1; l <= word.length(); l++){

                for(int i=0; i < word.length() -l + 1 ; i++){

                    int j = i + l-1;

                    String str = word.substring(i,j+1);

                    

                    if(dict.contains(str)){

                        matrix[i][j] = i;

                        continue;

                    }

                    

// Filling the value of the corresponding cell for the taken substring using value of the previously calculated cell.

                    for(int k=i+1; k <= j; k++){

                        if(matrix[i][k-1] != -1 && matrix[k][j] != -1){

                            matrix[i][j] = k;

                            break;

                        }

                    }

                }

            }

            if(matrix[0][word.length()-1] == -1){

                return null;

            }

            

            

//Finally segregate the given word into the words available in the dictionary.

            StringBuffer buffer = new StringBuffer();

            int i = 0; int j = word.length() -1;

            while(i < j){

                int k = matrix[i][j];

                if(i == k){

                    buffer.append(word.substring(i, j+1));

                    break;

                }

                buffer.append(word.substring(i,k) + " ");

                i = k;

            }

            

            return buffer.toString();

        }

        public static void main(String args[]){

            Set<String> dictionary = new HashSet<String>();

            dictionary.add("I");

            dictionary.add("like");

            dictionary.add("had");

            dictionary.add("play");

            dictionary.add("to");

            String str = "Ihadliketoplay";

            Main bmw = new Main();

            String result1 = bmw.wordBreakProblem(str, dictionary);

            

            System.out.print(result1);

        }

    }


Output:

I had like to play

Test your understanding...



Q. 1) Given dict = {'li, 'sop', 'tree', 'ding', 'g'} and word = 'sopptreeg'. Is it possible to segment the given string into space separated segments of the given dict. words ?

Ans. 'sop' , 'p', 'tree', 'g'

'so', 'pp', 'tree', 'g'

It looks like the given word can't be separated.

Before you leave...

Knowledge of data structure and algorithms is must to simulate the real world problem in code.

If you want to learn more about this article, drop a comment below and reach out to us to let us know your interest.

If you enjoyed learning the fundamentals of DSA share your knowledge to your fellow programmers and social circle. May be someone out really needs this resource, and you might be helping them out by sharing it.

eeeeellldldkonoioid