Tag: Questions

  • Java – How do I efficiently iterate over each entry in a Java Map?

    Java – How do I efficiently iterate over each entry in a Java Map? The Java Map interface, java.util.Map, represents a mapping between a key and a value. More specifically, a Java Map can store pairs of keys and values. Each key is linked to a specific value. Once stored in a Map, you can later look up the value using just…

  • Java – What is difference between CrudRepository and JpaRepository interfaces in Spring Data JPA?

    Java – What is difference between CrudRepository and JpaRepository interfaces in Spring Data JPA? JpaRepository extends PagingAndSortingRepository which in turn extends CrudRepository. Their main functions are: CrudRepository mainly provides CRUD functions. PagingAndSortingRepository provides methods to do pagination and sorting records. JpaRepository provides some JPA-related methods such as flushing the persistence context and deleting records in a batch. Because of the inheritance mentioned…

  • Java – What’s the difference between @Component, @Repository & @Service annotations in Spring?

    Java – What’s the difference between @Component, @Repository & @Service annotations in Spring?   From Spring Documentation: The @Repository annotation is a marker for any class that fulfils the role or stereotype of a repository (also known as Data Access Object or DAO). Among the uses of this marker is the automatic translation of exceptions, as described in Exception…

  • Java – Program To Calculate Distance Between Two Points

    Java – Program To Calculate Distance Between Two Points   The distance between two points formula derived from the Pythagorean Theorem. To find the distance between two points (x1,y1) and (x2,y2), all that you need to do is use the coordinates of these ordered pairs and apply the formula pictured below: Taking input through scanner…

  • Java – How to read / convert an InputStream into a String in Java?

    Java – How to read / convert an InputStream into a String in Java? Ways to convert an InputStream to a String: Using IOUtils.toString (Apache Utils) String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8); Using CharStreams (Guava) String result = CharStreams.toString(new InputStreamReader( inputStream, Charsets.UTF_8)); Using Scanner (JDK) Scanner s = new Scanner(inputStream).useDelimiter(“\\A”); String result = s.hasNext() ? s.next() : “”; Using Stream API (Java 8). Warning: This…

  • Java – How to parse JSON in Java

    Java – How to parse JSON in Java Lets assume you have a class Person with just a name. Google GSON (Maven) Great JSON serialisation / de-serialisation of objects. If you want to get a single attribute out you can do it easily with the Google library as well: Org.JSON (Maven) If you don’t need object de-serialisation but to simply…

  • Java – How to compare strings in Java?

    Java – How to compare strings in Java? == tests for reference equality (whether they are the same object). .equals() tests for value equality (whether they are logically “equal”). Consequently, if you want to test whether two strings have the same value you will probably want to use Objects.equals(). // These two have the same value new String(“test”).equals(“test”)…

  • Java – What is a NullPointerException, and how do I fix it?

    What is a NullPointerException (NPE), and how do I fix it? A good place to start is the JavaDocs. They have this covered: Thrown when an application attempts to use null in a case where an object is required. These include: Calling the instance method of a null object. Accessing or modifying the field of a…

  • Java – Get real file extension

    How to get real file extension in java? One of the option by getting MIME type as : mimeType= URLConnection.guessContentTypeFromName(file.getName()); Below has the advantage of going beyond mere use of file extension, and takes a peek at content InputStream is = new BufferedInputStream(new FileInputStream(file)); mimeType = URLConnection.guessContentTypeFromStream(is); //…close stream Using Java 7 import java.io.IOException; import…