karthikeya Boyini

karthikeya Boyini

1,421 Articles Published

Articles by karthikeya Boyini

Page 65 of 143

Remove all elements from Java LinkedHashSet

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 426 Views

To remove all the elements from LinkedHashSet in Java, use the clear() method.The following is an example to declare LinkedHashSet and add elements to it −LinkedHashSet hashSet = new LinkedHashSet(); hashSet.add(10); hashSet.add(20); hashSet.add(30); hashSet.add(40); hashSet.add(50); hashSet.add(60);Use the clear() method to remove all elements −hashSet.clear();The following is an example −Exampleimport java.util.LinkedHashSet; public class Demo { public static void main(String[] args) { LinkedHashSet hashSet = new LinkedHashSet(); hashSet.add(10); hashSet.add(20); hashSet.add(30); ...

Read More

Get last entry from NavigableMap in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 238 Views

To display the last entry from NavigableMap in Java, use the lastEntry() method.Let us first create NavigableMap.NavigableMap n = new TreeMap(); n.put("A", 498); n.put("B", 389); n.put("C", 868); n.put("D", 988); n.put("E", 686); n.put("F", 888); n.put("G", 999); n.put("H", 444); n.put("I", 555); n.put("J", 666);Get the last entry now.n.lastEntry()The following is an example to get the last entry from NavigableMap.Exampleimport java.util.*; public class Demo { public static void main(String[] args) { NavigableMap n = new TreeMap(); n.put("A", 498); n.put("B", 389); ...

Read More

Java Program to generate custom random number -1 or 1

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 288 Views

To generate custom random number 1 or -1, you need to use nextBoolean(). At first take a loop and create a Random object on each iteration −for (int i = 0; i < 5; i++) {    Random rand = new Random(); }Now, use nextBoolean() to generate 1 on TRUE condition, ekse -1 −for (int i = 0; i < 5; i++) {    Random rand = new Random();    if (rand.nextBoolean())       System.out.println(1);    else       System.out.println(-1); }Exampleimport java.util.Random; public class Demo {    public static void main(String[] args) {       for (int ...

Read More

C++ Program to Construct an Expression Tree for a Postfix Expression

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 3K+ Views

An expression tree is basically a binary tree which is used to represent expressions. In expression tree, nodes correspond to the operator and each leaf node corresponds to the operand. This is a C++ program to construct an expression tree for a postfix Expression in inorder, preorder and postorder traversals.AlgorithmBegin    Function r() has a character variable as parameter.       If the characters are + or - or * or / then          Return will be -1       If the characters are from A to Z then          Return will ...

Read More

How to use headSet() method of Java NavigableSet Class

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 159 Views

The headset() method returns elements up to a limit defined as a parameter.First, create a NavigableSet and add elements −NavigableSet set = new TreeSet(); set.add(10); set.add(25); set.add(40); set.add(55); set.add(70); set.add(85); set.add(100);Now, use the headset() method −set.headSet(55));The following is an example −Exampleimport java.util.NavigableSet; import java.util.TreeSet; public class Demo {    public static void main(String[] args) {       NavigableSet set = new TreeSet();       set.add(10);       set.add(25);       set.add(40);       set.add(55);       set.add(70);       set.add(85);       set.add(100);       System.out.println("Returned Value = " + set.headSet(55)); ...

Read More

Copy all elements of Java LinkedHashSet to an Object Array

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 248 Views

First, create a LinkedHashSet and add elements −LinkedHashSet l = new LinkedHashSet(); l.add(new String("1")); l.add(new String("2")); l.add(new String("3")); l.add(new String("4")); l.add(new String("5")); l.add(new String("6")); l.add(new String("7"));Now, copy it to an object array like this −// copying Object[] arr = l.toArray();The following is an example to copy all elements of a LinkedHashSet to an object array −Exampleimport java.util.*; public class Demo { public static void main(String[] args) { LinkedHashSet l = new LinkedHashSet(); l.add(new String("1")); l.add(new String("2")); ...

Read More

C++ Program to Find Lowest Common Ancestor in a Binary Search Tree

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 587 Views

A binary tree which has at most two children, specified as left child and right child. This is a C++ Program to find the lowest common ancestor in a Binary Tree.AlgorithmBegin Create a structure n to declare data d, a left child pointer l and a right child pointer r.    Create a function to create newnode. Call a function LCA() to Find lowest common ancestor in a binary tree:    Assume node n1 and n2 present in the tree.    If root is null, then return.       If root is not null there are two cases.   ...

Read More

Create a new ArrayList from another collection in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 471 Views

An ArrayList can be created from another collection using the java.util.Arrays.asList() method. A program that demonstrates this is given as follows −Exampleimport java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Demo {    public static void main(String args[]) throws Exception {       String str[] = { "John", "Macy", "Peter", "Susan", "Lucy" };       List aList = new ArrayList(Arrays.asList(str));       System.out.println("The ArrayList elements are: " + aList);    } }OutputThe ArrayList elements are: [John, Macy, Peter, Susan, Lucy]Now let us understand the above program.The string array str[] is defined. Then an ArrayList is created using the ...

Read More

How to populate a 2d array with random alphabetic values from a range in Java?

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 1K+ Views

To populate a 2d array with random alphabets, use the Random class. Let us first declare a 2d array −char arr[][] = new char[3][3];Now, in a nested for loop, use the Random class object to get random values on the basis of switch case. Here, our range is 3 i.e. set of 3 alphabets at once −Random randNum = new Random(); for (int i = 0; i < 3; i++) {    for (int j = 0; j < 3; j++) {       int x = randNum.nextInt(3);       switch (x) {          case ...

Read More

Java Program to sort a subset of array elements

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 385 Views

Let us first create a string array −String[] strArr = new String[] { "r", "p", "v", "y", "s", "q" };Now, use Arrays.sort() to get the subset. Use the following to sort only from the index range 2 to 6.Arrays.sort(strArr, 2, 6);Exampleimport java.util.Arrays; public class Demo {    public static void main(String[] args) {       String[] strArr = new String[] { "r", "p", "v", "y", "s", "q" };       Arrays.sort(strArr, 2, 6);       System.out.println("Sorted subset of array elements from index 2 to 6...");       for (int a = 0; a < strArr.length; a++) ...

Read More
Showing 641–650 of 1,421 articles
« Prev 1 63 64 65 66 67 143 Next »
Advertisements