Found 33676 Articles for Programming

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

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

388 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 −Example Live Demopublic class Demo {    public static void main(String[] args) {       int val = 20;       boolean[] isprime = new boolean[val + 1];       for (int i = 0; i

Java Program to convert positive int to negative and negative to positive

Samual Sam
Updated on 30-Jul-2019 22:30:25

9K+ Views

To convert positive int to negative and vice-versa, use the Bitwise Complement Operator.Let us first initialize a positive int −int positiveVal = 200;Now, let us convert it to negative −int negativeVal = (~(positiveVal - 1));Now, let’s say we have the following negative int −int negativeVal = -300;The following will convert the negative to positive int −positiveVal = ~(negativeVal - 1);Example Live Demopublic class Demo {    public static void main(String[] args) throws java.lang.Exception {       int positiveVal = 100;       int negativeVal = (~(positiveVal - 1));       System.out.println("Result: Positive value converted to Negative = "+negativeVal); ... Read More

Compare BigDecimal movePointRight and scaleByPowerOfTen in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

281 Views

The java.math.BigDecimal.movePointRight(int n) returns a BigDecimal which is equivalent to this one with the decimal point moved n places to the right. If n is non-negative, the call merely subtracts n from the scale.The java.math.BigDecimal.scaleByPowerOfTen(int n) returns a BigDecimal whose numerical value is equal to (this * 10n). The scale of the result is (this.scale() - n).The following is an example displaying the usage of both −Example Live Demoimport java.math.BigDecimal; public class Demo {    public static void main(String... args) {       long base = 3676;       int scale = 5;       BigDecimal d = ... Read More

Java Program to read map by Map.Entry

Samual Sam
Updated on 30-Jul-2019 22:30:25

175 Views

To read the Map, first use getProperties() ad then iterator to iterate through the entire list of Map −Properties prop = System.getProperties(); Iterator i = prop.entrySet().iterator();Now, loop through Map.Entry and get the key-value pair for the Map −while (i.hasNext()) {    Map.Entry entry = (Map.Entry) i.next();    System.out.println(entry.getKey() + " => " + entry.getValue()); }Example Live Demoimport java.util.Iterator; import java.util.Map; import java.util.Properties; public class Demo {    public static void main(String[] a) {       Properties prop = System.getProperties();       Iterator i = prop.entrySet().iterator();       while (i.hasNext()) {          Map.Entry entry = (Map.Entry) ... Read More

Java Program to loop through Map by Map.Entry

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

197 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); }Example Live Demoimport java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; ... Read More

How to keep the insertion order with Java LinkedHashMap?

Samual Sam
Updated on 30-Jul-2019 22:30:25

224 Views

To keep the insertion order with LinkedHashMap, use Iterator. Let us first create a HashMap and add elements to it −LinkedHashMaplHashMap = new LinkedHashMap(); lHashMap.put("1", "A"); lHashMap.put("2", "B"); lHashMap.put("3", "C"); lHashMap.put("4", "D"); lHashMap.put("5", "E"); lHashMap.put("6", "F"); lHashMap.put("7", "G"); lHashMap.put("8", "H"); lHashMap.put("9", "I");Now, get the values with the values() method. Iterate through the elements and display them −Collection collection = lHashMap.values(); Iterator i = collection.iterator(); while (i.hasNext()) {    System.out.println(i.next()); }Example Live Demoimport java.util.Collection; import java.util.Iterator; import java.util.LinkedHashMap; public class Demo {    public static void main(String[] args) {       LinkedHashMaplHashMap = new LinkedHashMap();       lHashMap.put("1", "A");   ... Read More

Extract values from HashMap in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

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)); }Example Live Demoimport 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

Java Program to create String to super class type mapping

Samual Sam
Updated on 30-Jul-2019 22:30:25

182 Views

Here, we have a superclass Vehicle and within that some subclasses −class Vehicle { } class Motorcycle extends Vehicle { } class Bus extends Vehicle { } class Car extends Vehicle { }Now, we will create some strings for mapping with super class type −Mapmap = new HashMap(); map.put("motorcycle", new Motorcycle()); map.put("bus", new Bus()); map.put("car", new Car());Example Live Demoimport java.util.HashMap; import java.util.Map; class Vehicle { } class Motorcycle extends Vehicle { } class Bus extends Vehicle { } class Car extends Vehicle { } public class Demo {    public static void main(String... args) {       Mapmap = new ... Read More

Rotate a List in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

236 Views

To rotate a list in Java, let us first create a List and add elements −List < Integer > list = new ArrayList < Integer > (); list.add(5); list.add(10); list.add(15); list.add(20); list.add(25); list.add(30); list.add(35); list.add(40); list.add(45);Now, rotate the list −Collections.reverse(list);Example Live Demoimport java.util.ArrayList; import java.util.Collections; import java.util.List; public class Demo {    public static void main(String args[]) {       Listlist = new ArrayList();       list.add(5);       list.add(10);       list.add(15);       list.add(20);       list.add(25);       list.add(30);       list.add(35);       list.add(40);       list.add(45); ... Read More

Replace an element from a Java List using ListIterator

Samual Sam
Updated on 30-Jul-2019 22:30:25

322 Views

Let us first create a Java List and add elements −ArrayList < String > list = new ArrayList < String > (); list.add("Katie"); list.add("Tom"); list.add("Jack"); list.add("Amy"); list.add("Andre"); list.add("Brad"); list.add("Peter"); list.add("Bradley");Now, use ListIterator and return the next element in the List with next() −ListIteratoriterator = list.listIterator(); iterator.next();Replace the element in the List with set() method. Here, whatever element is set will get replaced as the first element of the Iterator −iterator.set("Angelina");Example Live Demoimport java.util.ArrayList; import java.util.ListIterator; public class Demo {    public static void main(String[] args) {       ArrayListlist = new ArrayList();       list.add("Katie");       list.add("Tom"); ... Read More

Advertisements