Samual Sam

Samual Sam

1,507 Articles Published

Articles by Samual Sam

Page 11 of 151

Convert an ArrayList to HashSet in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 1K+ Views

To convert ArrayList to HashSet, firstly create an ArrayList −List l = new ArrayList();Add elements to the ArrayList −l.add("Accent"); l.add("Speech"); l.add("Diction"); l.add("Tone"); l.add("Pronunciation");Now convert the ArrayList to HashSet −Set s = new HashSet(l);The following is an example to convert an ArrayList to HashSet in Java.Exampleimport java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.HashSet; public class Main { public static void main(String[] args) { List l = new ArrayList(); l.add("Accent"); l.add("Speech"); l.add("Diction"); ...

Read More

Instant get() method in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 268 Views

The value of the required ChronoField for an Instant can be obtained using the get() method in the Instant class in Java. This method requires a single parameter i.e. the ChronoField and it returns the value of the ChronoField that was passed as a parameter.A program that demonstrates this is given as follows −Exampleimport java.time.*; import java.time.temporal.ChronoField; import java.time.temporal.ValueRange; public class Demo {    public static void main(String[] args) {       Instant i = Instant.now();       int micro = i.get(ChronoField.MICRO_OF_SECOND);       System.out.println("The current Instant is: " + i);       System.out.println("The MICRO_OF_SECOND Field ...

Read More

How to convert Integer array list to integer array in Java?

Samual Sam
Samual Sam
Updated on 11-Mar-2026 2K+ Views

To convert integer array list to integer array is not a tedious task. First, create an integer array list and add some elements to it −ArrayList < Integer > arrList = new ArrayList < Integer > (); arrList.add(100); arrList.add(200); arrList.add(300); arrList.add(400); arrList.add(500);Now, assign each value of the integer array list to integer array. We used size() to get the size of the integer array list and placed the same size to the newly created integer array −final int[] arr = new int[arrList.size()]; int index = 0; for (final Integer value: arrList) {    arr[index++] = value; }Exampleimport java.util.ArrayList; public class ...

Read More

Create NavigableMap from TreeMap in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 240 Views

To create NavigableMap from TreeMap. The following is the declaration −NavigableMap n = new TreeMap();Now, add some elements to the NavigableMap created above −n.put("A", 888); n.put("B", 999); n.put("C", 444); n.put("D", 555); n.put("E", 666); n.put("F", 888); n.put("G", 999); n.put("H", 444); n.put("I", 555); n.put("J", 666);The following is an example to create NavigableMap from TreeMap and display it −Exampleimport java.util.*; public class Demo { public static void main(String[] args) { NavigableMap n = new TreeMap(); n.put("A", 888); n.put("B", 999); ...

Read More

Get the first element from a Sorted Set in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 638 Views

To create a Sorted Set, firstly create a Set.Set s = new HashSet();Add elements to the above set.int a[] = {77, 23, 4, 66, 99, 112, 45, 56, 39, 89}; Set s = new HashSet(); try { for(int i = 0; i < 5; i++) { s.add(a[i]); }After that, use TreeSet class to sort.TreeSet sorted = new TreeSet(s);Get the first element, using the first() method −System.out.println("First element of the sorted set = "+ (Integer)sorted.first());The following is the code to get the first element from a Sorted Set ...

Read More

C++ Program to Check if a Binary Tree is a BST

Samual Sam
Samual Sam
Updated on 11-Mar-2026 1K+ Views

Binary Search Tree is a binary tree data structure in which we have 3 properties −The left subtree of a binary search tree of a node contains only nodes with keys lesser than the node’s key.The right subtree of a binary search tree node contains only nodes with keys greater than the node’s key.The left and right of a subtree each must also be a binary search tree.AlgorithmBegin    function BSTUtill()       If node is equals to NULL then          Return 1.       If data of node is less than minimum or greater ...

Read More

Insert an element to List using ListIterator in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 346 Views

Let us first create an ArrayList −ArrayList < Integer > arrList = new ArrayList < Integer > (); arrList.add(100); arrList.add(200); arrList.add(300); arrList.add(400); arrList.add(500);Now, create a ListIterator from the above ArrayList and insert more elements −ListIterator < Integer > iterator = arrList.listIterator(); iterator.add(1000); iterator.add(2000); iterator.add(3000);Exampleimport java.util.ArrayList; import java.util.ListIterator; public class Demo {    public static void main(String[] args) {       ArrayListarrList = new ArrayList();       arrList.add(100);       arrList.add(200);       arrList.add(300);       arrList.add(400);       arrList.add(500);       arrList.add(600);       arrList.add(700);       arrList.add(800);       ListIteratoriterator = arrList.listIterator();       iterator.add(1000);       iterator.add(2000);       iterator.add(3000);       for (Integer i: arrList) {          System.out.println(i);       }    } }output1000 2000 3000 100 200 300 400 500 600 700 800

Read More

Generate 10 random four-digit numbers in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 5K+ Views

To generated random integer, use the Random class with nextInt. At first, create a Random object −Random rand = new Random();The Random above is a random number generator. Now, pick the random numbers one by one. We want 10 random four-digit numbers, therefore loop it until i = 1 to 10 −for (int i = 1; i

Read More

C++ Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree

Samual Sam
Samual Sam
Updated on 11-Mar-2026 281 Views

A binary tree is a tree data structure in which each node has at most two children, which are defined as left child and right child.AlgorithmBegin    function identical():        Take two nodes r1 and r2 as parameter.       If r1 and r2 is NULL then          Return true.       If r1 or r2 is NULL then          Return false.       Return (r1->d is equal to r2->d and          Call function Identical(r1->l, r2->l) and          Call functions Identical(r1->r, r2->r) ); ...

Read More

How to generate a random BigInteger value in Java?

Samual Sam
Samual Sam
Updated on 11-Mar-2026 3K+ Views

To generate random BigInteger in Java, let us first set a min and max value −BigInteger maxLimit = new BigInteger("5000000000000"); BigInteger minLimit = new BigInteger("25000000000");Now, subtract the min and max −BigInteger bigInteger = maxLimit.subtract(minLimit); Declare a Random object and find the length of the maxLimit: Random randNum = new Random(); int len = maxLimit.bitLength();Now, set a new B integer with the length and the random object created above.Exampleimport java.math.BigInteger; import java.util.Random; public class Demo {    public static void main(String[] args) {       BigInteger maxLimit = new BigInteger("5000000000000");       BigInteger minLimit = new BigInteger("25000000000");     ...

Read More
Showing 101–110 of 1,507 articles
« Prev 1 9 10 11 12 13 151 Next »
Advertisements