Posts

How to get all the dependencies/dependency tree of a maven project (direct dependencies and indirect dependencies)?

Image
Introduction:         If you want to get the list of direct and indirect dependencies in a maven project, you can use the command mvn dependency:tree How to Execute: You can run mvn dependency:tree on the command line like below. You should be in the same folder where pom.xml exists.        What is the use?        Knowing the list of all the dependencies in a maven project is helpful if you want to exclude some indirect dependencies.  For example, you have added a dependency in your project which internally fetches a log back dependency with version 1.2. But if you want to use the log back dependency with version 1.3, then you need to find out which dependency caused this and you have to exclude this. Later you can add the log back dependency with version 1.3.

In Detail:Collectors toList() method in Java 8

Introduction:          Collectors.toList() method is used to transform the java stream into a List of values. This process is called a reduction. The Collectors class implements various useful reduction operations such as accumulating the stream elements into collections etc.  In a simple layman terms, Collectors.toList() method is used to convert a stream of values to a List of values. Collectors.toList() actually returns a Collector object which accumulates/combines the input elements into a new List.  Now let us see how to implement Collectors.toList() with an example. Code Exmaple package test; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class CollectorsToList { public static void main(String[] args) { // create a sample list List&ltInteger&gt aList = new ArrayList&lt&gt(); // add few elements to the array list aList.add(1); aList.add(2); aList.add(3); a...

What is Meta tag in HTML and what does <meta charset="utf-8"> do?

Image
1. Introduction What is Metadata?         Metadata is data that describes data. It can be data about HTML, such as author, and import keywords that describe the document. The metadata can be added to an HTML document using the &ltmeta&gt element. There are a lot of different types of elements that can be added to your HTML document. Out of these &ltmeta charset="utf-8"&gt is also one type. Where should we place &ltmeta&gt tag inside HTML document?      The &ltmeta&gt must be placed inside the &lthead&gt tag. The job of the HTML head tag is to contain metadata about the document. The head of an HTML document is the part that is not displayed in the web browser when the page is loaded. It contains information such as the page &lttitle&gt, links to CSS, links to custom favicons, and other metadata. 2. What does &ltmeta charset="utf-8"&gt do? This element simply specifies the document's...

How to Convert Java 8 IntStream to a List

Image
1. Overview                     In this tutorial, we are going to convert an IntStream to List and to an array. IntStream is a stream of primitive int values and a List is an ordered collection that holds generic values. To convert a stream to the collection, we can call collect() method of Stream class, but this works when the streams hold non-primitive types. Calling collect() method on primitive streams give a compilation error "collect() in IntStream cannot be applied to". 2. Solutions 2.1. Call boxed() method before collecting to a List        IntStream has a method boxed() that returns a Stream consisting of the elements each boxed to an Integer. After getting a boxed stream, we can call the collect() method to convert into a List. Code: package streams; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; public class IntStreamTest { public stati...

Topics to Prepare for Java Interview

Companies looking for Java developers who have experience in following topics 1. Core Java 2. Java 8 3. Multithreading (must) 4. Spring 5. Spring Boot (Micro Services) 6. Hibernate 7. XML 8. Concurrency 9. Algorithms 10. Data structures 11. Design patterns 12. Object-Oriented Design 13. Architecture patterns 14. MVC Design 15. RESTful web services 16. Maven 17. MongoDB 18. Git 19. JUnit & Mockito 20. System Design Links to refer: Design patterns https://www.youtube.com/watch?v=EYOKqb2Mf7k&list=PLmCsXDGbJHdjZiLC2HjXrgw8-E4O3MXdN Singleton Implement Singleton using Eager Initialisation Implement Singleton using Lazy Initialisation What is the Double-Checked Locking? When Double-Checked Locking fails? How can you fix Double-Checked Locking fail? Ans: use Volatile Factory  Abstract Factory - Factory of Factories Spring Spring core Spring transactions Spring JDBC Spring MVC CoreJava & Java8 OOP...

What is Group Id && Artifact Id in a maven project

Image
Artifact is a file, usually a jar. So Artifact Id is the name of the jar. Each artifact id has a group ID (usually a reversed domain name like com.example.foo) for example, in the below dependencies, you can observer that, all artifacts are belong to same groupId. Here groupId is org.slf4j & artifacts have different ids.

Different ways to create an object In Java

Below are the different ways to create an object in java. 1. using new keyword 2. using newInstance() method of class Class 3. using newInstance() method of class Constructor 4. using clone() method 5. using deserialization Using new keyword: 1. Employee emp = new Employee(); Using newInstance() method of class Class: 1. Employee emp2 = Class.forName("Employee").newInstance(); 2. Employee emp3 = Employee.class.newInstance(); Using newInstance() method of class Constructor: 1. Constructor empConstructor = Employee.class.getConstructor();  Employee emp4 = empConstructor.newInstance() Using clone() method: 1. Calling clone() method, actually creates a new object and copies all the contents of old object into it. Creating an object using clone method doesn't invoke any constructor. 2. To use clone() method on an object, we need to implement Cloneable interface and define clone() method in it. Employee emp5 = (Employee) emp4.clone(); Using...

ConcurrentHashMap in Java

ConcurrentHashMap is concrete implementation of ConcurrentMap. Both are from java.util.concurrent package. Functionality: 1. The functionality is similar to normal HashMap with better concurrency. 2. ConcurrentHashMap is thread safe. 3. It doesn't lock the Map while you are reading from it. 4. It doesn't lock the entire Map when writing to it, it only locks the part of the Map that is being written to, internally. Difference between HahsMap && ConcurrentHashMap: HashMap ConcurrentHashMap Not Thread Safe Thread Safe Allows 1 null Key Will Not allow NULL Values

Benifits Of Hibernate ORM

The Benifits of ORM (Object Relational Mapping) is as follows. 1. It reduces the development time/cost. 2. It speeds up the development. 3. It provides us with portability. Hibernate supports multiple databases. so there is no need to write database-specific code. This is a useful feature, generally all databases have their own syntax made up of Data Definition Language (DDL) or Data Manipulation Language (DML) statements. If we used JDBC for querying & persisting data, we would need to rewrite a database-specific code as our database is changed. In this regard hibernate gets rid of developer's headache by handling this issue. How hibernate handles different databases syntaxes? 1. With the help of dialect hibernate achieves this type of functionality. The implementation of dialect class is provided by the database provider to inform hibernate about the syntax of their particular database. Benifits of Hibernate ORM: 1. Code reusablity. 2. Transaction Management....

Error: Could not find or load mian class org.gradle.wrapper.GradleWrapperMain

Image
When i was trying to build a project for eclipse, i ran the below command gradlew eclipse and i got below error. I searched in internet and found some solutions and finally the below solution is working for me. I executed below command and this fixed my issue. 'git add -f gradle/wrapper/gradle-wrapper.jar' There are other solutions but this only worked for me.

GitHub: How to pull changes from online git repository to your local

Image
  1. If you are working on some open source project which is hosted on online github repository and you want to get the files modified by other peers. The command to get changes to your local (computer) project folder, you have to execute "git pull" command. 2. Remember, you have to in the project directory of before executing any git commands.  3. The output of "git pull" looks like below.Here some one added "HelloWorld.java" to online repository and git pulled that file to my local repository.

GIT: How to add a file to local repostiory

Image
1. If you have created a new file in the project, the file will not be added to Git repository until you explicitly tell the Git. 2. When ever you create a new file, or made changes to a file, Git keeps track of changes to the file. 3. "git status" command lets you know, what are the changes added to the project. 4. The result of "git status" command looks like below. I added a file HelloWorld.txt to my HelloWorld project.  5. By using "git add" command you can add the file to Git repository. The output of "git add" as follows. 6. After executing "git add", execute "git status", you can see the message, branch is uptodate & changes to be committed, with the list of files to commit. 7. You can commit the changes to repository, by executing "git commit" command. 8. executing "git commit" won't push files to online git repository, it keeps files in your local repository. 9. If you want...