Learnitweb

Author: Editorial Team

  • Java 8 Runnable example using Lambda expression

    Functional interface acts as a type for lambda expression and can be used to invoke lambda expression. java.lang.Runnable is a functional interface as it has only one abstract method, public void run(). We’ll see an example to create Thread without lambda and then with the use of lambda expression. Output

  • Compare two HashMap in Java

    equals() method in the HashMap compares two maps by key-value pairs. This method returns true if two maps represent the same mappings. Since HashMap is not an ordered collection, the order of keys in both maps may be different. Compare HashMaps by keys and values Output Compare two hashmaps for same keys If we want…

  • How to write and use a lambda expression in Java 8?

    Lambda expression is an anonymous function. A lambda expression We’ll learn to convert normal method in to a lambda expression with the help of a simple “Hello World” example. Since a lambda expression does not have a name, modifier and return type, it’s equivalent lambda expression is: If lambda expression has only one executable statement,…

  • Java legal identifiers, keywords and code conventions

    All the Java components classes, variables, and methods need names. These names are called identifier. Legal identifiers must be composed of only Unicode characters, numbers, currency symbols, and connecting characters (such as underscores). Few examples of legal identifiers:int _x;int $x;int x2c;int this_is_a_valid_identifier; Few examples of illegal identifiers:int .x;int x.c;int x&;int x-c;int 9x; Java code conventions…

  • Java program to convert String to boolean

    java.lang.Boolean class provides two methods to convert String to boolean. public static boolean parseBoolean(String s) This method parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string “true”. public static Boolean valueOf(String s) This method returns…

  • Generics in Java – An introduction

    Generics is one of the most important features introduced in Java 5. Generics enables classes and interfaces to be parameters when defining classes, interfaces and methods. What the above statement means is an interface or class may be declared to take one or more type parameters, which are written in angle brackets and should be…

  • Java Functional Interface

    An interface with only single abstract method is called functional interface. A functional interface can contain default and static methods which do have an implementation in addition to single unimplemented method. Functional interface acts as a type for lambda expression and can be used to invoke lambda expression There are some predefined Functional interfaces in…

  • Write a java program to find common elements between two arrays?

    Method 1: Iterative method In this method, we iterate both the given arrays and compare each element of one array with elements of other array. If the two compared elements are found equal then they add element to a HashSet. The method works well even for arrays which contain duplicate elements. Output Method 2: Using retainAll()…

  • How to Convert a Map to a List in Java?

    A Map maps keys to values. Map provides following two methods to return keys and values of a map: public Collection<V> values() This method returns a Collection view of the values contained in this map. public Set keySet() This method returns a Set view of the keys contained in this map Output

  • Difference between DOM and SAX parser in Java

    DOM XML parser in Java DOM stands for Document Object Model and represents an XML document in tree format, each element representing tree branches. The XML file will be loaded as a whole and all its contents will be built as an in-memory representation of the tree the document represents. Parsing XML file using DOM…