karthikeya Boyini

karthikeya Boyini

1,421 Articles Published

Articles by karthikeya Boyini

Page 61 of 143

Iterate over the elements of HashSet in Java

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

Declare a HashSet and add elements −Set hs = new HashSet(); hs.add(20); hs.add(39); hs.add(67); hs.add(79);Now, iterate over the elements −for (Iterator i = hs.iterator(); i.hasNext();) {    Object ele = i.next();    System.out.println(ele); }The following is an example that iterate over the elements of HashSet −Exampleimport java.util.HashSet; import java.util.Iterator; import java.util.Set; public class Demo {    public static void main(String[] argv) throws Exception {       Set hs = new HashSet();       hs.add(20);       hs.add(39);       hs.add(67);       hs.add(79);       System.out.println("Elements = ");       for (Iterator i ...

Read More

Provider get() method in Java

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

The value to which a key is mapped can be obtained using the get() method in the class java.security.Provider. This method requires a single parameter i.e. the key whose value is required. It returns the value to which the key is mapped or it returns null if there is no value to which the key is mapped.A program that demonstrates this is given as follows −Exampleimport java.security.*; import java.util.*; public class Demo {    public static void main(String[] argv) {       try {          SecureRandom sRandom = SecureRandom.getInstance("SHA1PRNG");          Provider p = ...

Read More

Remove duplicate elements in Java with HashSet

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

Set implementations in Java has only unique elements. Therefore, it can be used to remove duplicate elements.Let us declare a list and add elements −List < Integer > list1 = new ArrayList < Integer > (); list1.add(100); list1.add(200); list1.add(300); list1.add(400); list1.add(400); list1.add(500); list1.add(600); list1.add(600); list1.add(700); list1.add(400); list1.add(500);Now, use the HashSet implementation and convert the list to HashSet to remove duplicates −HashSetset = new HashSet(list1); Listlist2 = new ArrayList(set);Above, the list2 will now have only unique elements.Exampleimport java.util.ArrayList; import java.util.HashSet; import java.util.List; public class Demo {    public static void main(String[] argv) {       Listlist1 = new ArrayList();   ...

Read More

NavigableMap clear() Method in Java

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

Clear the NavigableMap in Java, using the clear() method.Let us first create a NavigableMap and add some elements to it −NavigableMap n = new TreeMap(); n.put(5, "Tom"); n.put(9, "John"); n.put(14, "Jamie"); n.put(1, "Tim"); n.put(4, "Jackie"); n.put(15, "Kurt"); n.put(19, "Tiger"); n.put(24, "Jacob");Now, use the clear() method −n.clear();The following is an example to implement clear() method and clear the NavigableMap −Exampleimport java.util.*; public class Demo {    public static void main(String[] args) {       NavigableMap n = new TreeMap();       n.put(5, "Tom");       n.put(9, "John");       n.put(14, "Jamie");       n.put(1, "Tim");   ...

Read More

Provider getName() method in Java

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

The name of the provider can be obtained using the getName() method in the class java.security.Provider. This method requires no parameter and it returns the name of the provider as required.A program that demonstrates this is given as follows −Exampleimport java.security.*; import java.util.*; public class Demo {    public static void main(String[] argv) {       try {          KeyPairGenerator kpGenerator = KeyPairGenerator.getInstance("DSA");          Provider p = kpGenerator.getProvider();          System.out.println("The Provider is: " + p.getName());       } catch (NoSuchAlgorithmException e) { ...

Read More

Extract values from HashMap in Java

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

To extract values from HashMap, let us first create a HashMap with keys and values −HashMapm = new HashMap();Now, add some elements to the HashMap −m.put(10, 20); m.put(30, 40); m.put(50, 60); m.put(70, 80); m.put(90, 100); m.put(110, 120); m.put(130, 140); m.put(150, 160);Now, extract the values from the HashMap −for (Integer i: m.keySet()) {    System.out.println(m.get(i)); }Exampleimport java.util.HashMap; public class Demo {    public static void main(String args[]) {       HashMapm = new HashMap();       m.put(10, 20);       m.put(30, 40);       m.put(50, 60);       m.put(70, 80);       m.put(90, 100);   ...

Read More

Provider toString() method in Java

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

The name and the version number of the provider in string form can be obtained using the method toString() in the class java.security.Provider. This method requires no parameters and it returns the name as well as the version number of the provider in string form.A program that demonstrates this is given as follows −Exampleimport java.security.*; import java.util.*; public class Demo {    public static void main(String[] argv) {       try {          KeyPairGenerator kpGenerator = KeyPairGenerator.getInstance("DSA");          Provider p = kpGenerator.getProvider();          System.out.println("The name and version number of the ...

Read More

Java Program to loop through Map by Map.Entry

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

Create a Map and insert elements to in the form of key and value −HashMap map = new HashMap (); map.put("1", "A"); map.put("2", "B"); map.put("3", "C"); map.put("4", "D"); map.put("5", "E"); map.put("6", "F"); map.put("7", "G"); map.put("8", "H"); map.put("9", "I");Now, loop through Map by Map.Entry. Here, we have displayed the key and value separately −Sets = map.entrySet(); Iteratori = s.iterator(); while (i.hasNext()) {    Map.Entrye = (Map.Entry) i.next();    String key = (String) e.getKey();    String value = (String) e.getValue();    System.out.println("Key = "+key + " => Value = "+ value); }Exampleimport java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import ...

Read More

Provider getService() method in Java

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

The service that can describe the implementation of a Provider in regards to any algorithm is obtained using the method getService() in the class java.security.Provider. This method requires two parameters i.e. the service type required and the algorithm name of the required service.A program that demonstrates this is given as follows −Exampleimport java.security.*; import java.util.*; public class Demo {    public static void main(String[] argv) throws Exception {       try {          Signature sign = Signature.getInstance("SHA256withDSA");          Provider p = sign.getProvider();          Provider.Service s = p.getService("Signature", sign.getAlgorithm());     ...

Read More

Java Program to display a prime number less than the given number

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

Let’s say the value you have set is 20 and you have to display a prime number less than this value i.e. 19 in this case.The following is an example that displays a prime number less than the given number −Examplepublic class Demo {    public static void main(String[] args) {       int val = 20;       boolean[] isprime = new boolean[val + 1];       for (int i = 0; i

Read More
Showing 601–610 of 1,421 articles
« Prev 1 59 60 61 62 63 143 Next »
Advertisements