Traverse Through a HashSet in Java

AmitDiwan
Updated on 23-Sep-2019 12:33:48

806 Views

HashSet extends AbstractSet and implements the Set interface. It creates a collection that uses a hash table for storage.A hash table stores information by using a mechanism called hashing. In hashing, the informational content of a key is used to determine a unique value, called its hash code.To traverse through a HashSet, you can use the Iterator in Java. At first, create a HashSet with string values −HashSet hashSet = new HashSet(); hashSet.add("Jack"); hashSet.add("Tom"); hashSet.add("David"); hashSet.add("John"); hashSet.add("Steve");Now, traverse using Iterator −Iterator iterator = hashSet.iterator(); while(iterator.hasNext()){    System.out.println(iterator.next()); }Let us see a simple example wherein we have some HashSet elements and we ... Read More

Java Signature getInstance Method with Examples

AmitDiwan
Updated on 23-Sep-2019 12:31:24

738 Views

A signature object that can implement the required signature algorithm can be obtained using the method getInstance() in the class java.security.Signature.Let us now see an example −Exampleimport java.security.*; import java.util.*; public class Main {    public static void main(String[] argv) {       try {          Signature signature = Signature.getInstance("SHA256withRSA");          String str = signature.toString();          System.out.println(str);       } catch (NoSuchAlgorithmException e) {          System.out.println("Error!!! NoSuchAlgorithmException");       }    } }OutputSignature object: SHA256withRSALet us now see another example −Exampleimport java.security.*; import java.util.*; public ... Read More

Java Signature getAlgorithm() Method with Examples

AmitDiwan
Updated on 23-Sep-2019 12:29:24

237 Views

The name of the algorithm for the signature object can be obtained using the method getAlgorithm() in the class java.security.Signature.Let us now see an example −Exampleimport java.security.*; import java.util.*; public class Main {    public static void main(String[] argv) {       try {          Signature signature = Signature.getInstance("SHA256withRSA");          String algorithm = signature.getAlgorithm();          System.out.println("The Algorithm = " + algorithm);       } catch (NoSuchAlgorithmException e) {          System.out.println("Error!! NoSuchAlgorithmException");       }    } }OutputThe Algorithm = SHA256withRSALet us now see another example ... Read More

Log Functions in Java

AmitDiwan
Updated on 23-Sep-2019 12:27:32

372 Views

The log functions in Java are part of java.lang.Math. The functions include log, log10, log1p. Let us see an example of each of these log functions −static double log(double a)The java.lang.Math.log(double a) returns the natural logarithm (base e) of a double value. Let us see an example −Exampleimport java.io.*; public class Main {    public static void main(String args[]) {       // get two double numbers       double x = 60984.1;       double y = -497.99;       // get the natural logarithm for x       System.out.println("Math.log(" + x + ")=" ... Read More

Trigonometric Functions in Java

AmitDiwan
Updated on 23-Sep-2019 12:23:14

2K+ Views

The java.lang.Math class contains methods for trigonometric operations like cos(), sin(), tan(), tanh(), cosh(), atan(), etc.Let us work around some of these trigonometric functions in Java −static double asin(double a)The java.lang.Math.asin(double a) returns the arc sine of an angle, in the range of -pi/2 through pi/2.Let us now see an example −Exampleimport java.util.*; public class Main {    public static void main(String args[]) {       // get a variable x which is equal to PI/2       double x = Math.PI / 2;       // convert x to radians       x = Math.toRadians(x); ... Read More

C Program for Armstrong Numbers

Sunidhi Bansal
Updated on 23-Sep-2019 12:00:35

677 Views

We are given a task where we have to check a number n entered by the user, whether it’s Armstrong or not.An Armstrong number is when the sum of all the digits power by the number of digits or we can say order of digits n, is same as the digit.So below is a simple representation how to find the Armstrong number −Formula −wxyz…. = pow(w, n) +pow(x, n) + pow(y, n) + pow(z, n) + …..AlgorithmSTART Step 1-> Declare a function to find the value after power operation on the number    int power(int a, int b)     ... Read More

Calculate Area of Square in C++

Sunidhi Bansal
Updated on 23-Sep-2019 11:53:37

2K+ Views

We are given with a side of a rectangle and our task is to print the area of the square from that side.Square is 2-D plain figure which have 4 sides and forms 4 angles of 90degree each and all the sides are of equal shape. In other words we can say that the square is a form of rectangle with equal sides.Given below is representation of a square −The Area of square is Side x SideExampleInput: 6 Output: 36 As the side is 6 so the output is 6*6=36 Input: 12 Output: 144AlgorithmSTART    Step 1-> Declare a function ... Read More

C++ Program for Area of Square After N-th Fold

Sunidhi Bansal
Updated on 23-Sep-2019 11:50:02

210 Views

Given a side of a square and the number of fold, we have to find the Area of square after number of folds.A square is a 2-D shape like rectangle where all the sides are equal. And it’s all angles are equal to 90 degrees.While folding a square we −Fold the square from top left side of the triangle to the bottom of the right side forming a triangle.The second fold will be folding from up to downside.The third fold is folding again from left to right.And likewise we follow the above steps.ExamplesInput: side = 23, fold = 4 Output: ... Read More

C Program for Area and Perimeter of Rectangle

Sunidhi Bansal
Updated on 23-Sep-2019 11:47:18

3K+ Views

Given a length and breadth of a rectangle we have to find its area and Perimeter.Rectangle is 2-D figure containing four sides and four angles of 90 degree each. All the sides of rectangle are not equal only the opposite sides of a rectangle are equal. The diagonals in a rectangle also are of the same length.Below is a diagrammatic representation of rectangle.Here A represents the breadth and B represents length of the rectangle.To find the Area of a rectangle the formula is: Length x BreadthAnd Perimeter of a rectangle is − 2 x (Length+Breadth).ExamplesInput: 20 30 Output: area of rectangle ... Read More

C Program for N-th Odd Number

Sunidhi Bansal
Updated on 23-Sep-2019 11:46:16

1K+ Views

Given a number N we have to find the N-th odd number.Odd numbers are the numbers which are not completely divided by 2 and their remainder is not zero. Like 1, 3, 5, 7, 9, ….If we closely observe the list of even numbers we can also represent them as(2*1)-1=1, (2*2)-1=3, ( 2*3)-1=5, (2*4)-1=7, ….(2*N)-1.So, to solve the problem we can simply multiply the number N with 2 and subtract 1 from the result, that makes up an odd number.ExamplesInput: 4 Output: 7 The 4th odd number is 1, 3, 5, 7.. Input: 10 Output: 19AlgorithmSTART    STEP 1 -> ... Read More

Advertisements