Skip to main content

Posts

Showing posts from February, 2022

Set interface in Java with examples | HashSet, LinkedHashSet, TreeSet Class | SortedSet Interface

Guide to Set Interface in Java with SortedSet, HashSet, LinkedHashSet and TreeSet Set Interface (java.util.set) Set is part of  Collection Framework . Set directly extends Collection interface and uses its capability. Set is unordered data structure and contains only unique values means does not contains duplicate values. Following are child classes and interfaces of Set interface : HashSet Class LinkedHashSet Class SortedSet Interface TreeSet Class HashSet Class : HashSet contains unordered elements and can not contains duplicate elements. How to create new HashSet? Set set = new HashSet() Or HashSet hashSet = new HashSet() Lets see example of HashSet : Example 1 : HashSet class demo import java.util.HashSet; import java.util.Set; public class HashSetDemo {     public static void main(String[] args) {         Set<String> set = new HashSet<>() {{             add("Java");               add("Java");             add("Python");             add(&quo

How to Sort object property with null values using Java 8 Lambda expression

Sort custom object property with null values using Java 8 lambda expression What is lambda expression? The expression through which we can represent an anonymous function. learn more about lambda expression : Lambda Expression in Java 8 with examples   Lets see how we can sort custom object property without and with lambda expression. Example 1 : Sort custom object without lambda expression using Collections.sort() Programming.java public class Programming {     private int id;     private String name;          public Programming(int id, String name) {         this.id = id;         this.name = name;     }     public int getId() {         return id;     }     public void setId(int id) {         this.id = id;     }     public String getName() {         return name;     }     public void setName(String name) {         this.name = name;     }     @Override     public String toString() {         return "Programming [id = " + id + ", name = " + name + "]";     }

What is List interface in Java | Guide to ArrayList, LinkedList and Vector Class

List interface in Java with ArrayList, LinkedList and Vector Class    List Interface (java.util.List) : List is an ordered collection. It is also known as sequence. List is child interface of Collection framework . List comes under java.util package. We can access any element by their index and search for element also in the List. List maintains ordered elements and duplicate also. Methods of List Interface : add() addAll() clear() contains() containsAll() equals() get() hashCode() indexOf() isEmpty() iterator() lastIndexOf() listIterator() remove() removeAll() retainAll() set() size() subList() toArray() For use functionality of List interface we can use following classes : ArrayList LinkedList Vector Stack ArrayList Class : We can say, ArrayList is better version of Array. It is resizable array. means as simple array, we does not have to specify size of array. The Java ArrayList class can store a group of many objects. We can not store primitives (int, double, float, etc) in ArrayLis

How to Divide and compute Modulo of large number | How to compute mod of a big number?

Print modulo of BigInteger in Java | Convert large String to BigInteger in Java Sometimes, as developer we need to divide large number that is represented as String. For divide and get modulo we require value as number (int, long or float). But int, long have some limitations. So we can use BigInteger class for storing large numbers. BigInteger class is used for the mathematical operation which involves very big integer calculations that are outside the limit of all available primitive data types. Example : Check Odd and Even for large number using BigInteger class public class BigIntegerDemo {     public static void main(String[] args) {                  Scanner sc = new Scanner(System.in);         System.out.println("Enter large number");         BigInteger number = sc.nextBigInteger();          // Getting modulo of entered number         BigInteger ans = number.mod(new BigInteger("2"));          // Check modulo is 0 or not and print based on that         if