Found 7442 Articles for Java

How to fill multiple copies of specified Object to a List in Java

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

831 Views

To fill multiple copies of specified object to a list means let’s say you have an element 100 and want to display it 10 times. For this, let us see an example.The following is our list and iterator. We have used nCopiec Collections method to set the elements and how many copies you want −Listlist = Collections.nCopies(10, 100); Iteratoriterator = list.iterator();After that display the multiple copies −while (iterator.hasNext()) System.out.println(iterator.next());Example Live Demoimport java.util.Collections; import java.util.Iterator; import java.util.List; public class Demo {    public static void main(String[] args) {       Listlist = Collections.nCopies(10, 100);       Iteratoriterator = list.iterator();   ... Read More

Java program to convert LocalDate to java.util.Date in UTC

Samual Sam
Updated on 02-Jul-2024 10:26:21

5K+ Views

In this article, we will learn how to convert a local date to a java.util.Date in UTC using Java's date and time API. Problem Statement Convert the current date, to a java.util.Date object in Coordinated Universal Time (UTC). The goal is to achieve this conversion while ensuring that the resulting java.util.Date object represents the start of the day in UTC for the given LocalDate. Steps to Convert LocalDate to java.util.Date in UTC The following are the steps to convert LocalDate to java.util.Date in UTC Step 1: Import the required classes i.e., java.time and java.util ... Read More

Java Program to convert LocalDate to java.util.Date by timezone offset

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

211 Views

Set the LocalDate to now −LocalDate date = LocalDate.now();Now, set the TimeZone offset −ZoneOffset timeZone = ZoneOffset.UTC;Convert the LocalDate to java.util.Date −Date.from(date.atStartOfDay().toInstant(timeZone))Example Live Demoimport java.time.LocalDate; import java.time.ZoneOffset; import java.util.Date; public class Demo {    public static void main(String[] args) {       LocalDate date = LocalDate.now();       System.out.println("Date = "+date);       ZoneOffset timeZone = ZoneOffset.UTC;       System.out.println(Date.from(date.atStartOfDay().toInstant(timeZone)));    } }OutputDate = 2019-04-19 Fri Apr 19 05:30:00 IST 2019

Java Program to find keys from a Linked HashMap and store it in a list

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

180 Views

Let us first create a LinkedHashMap with key-value pair −Mapmap = new LinkedHashMap(); map.put("1", "Katie"); map.put("2", "Peter"); map.put("3", "Amy"); map.put("4", "Kane"); map.put("5", "Colin"); map.put("6", "Andre"); map.put("7", "Pollard"); map.put("8", "David"); map.put("9", "Jofra"); map.put("10", "Akila");Now, create a new List and store the keys in it for the above Map −Listlist = new ArrayList(); list.addAll(map.keySet());Example Live Demoimport java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; public class Demo {    public static void main(String[] args) {       Mapmap = new LinkedHashMap();       map.put("1", "Katie");       map.put("2", "Peter");       map.put("3", "Amy");       map.put("4", "Kane");     ... Read More

Java Program to find keys from both the Linked HashMap and store it in a list alternatively

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

122 Views

Let us first create a LinkedHashMap with key-value pair −Mapmap1 = new LinkedHashMap(); map1.put("1", "Jim"); map1.put("2", "David"); map1.put("3", "Tom"); map1.put("4", "Sam"); map1.put("5", "Steve");Let us now create another LinkedHashMap with key-value pair −Mapmap2 = new LinkedHashMap(); map2.put("6", "Katie"); map2.put("7", "John"); map2.put("8", "Kane"); map2.put("9", "Chris");Now, create a new List and store the keys in it for both the above Map −Listlist = new ArrayList(); list.addAll(map1.keySet()); list.addAll(map2.keySet());Example Live Demoimport java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; public class Demo {    public static void main(String[] args) {       Mapmap1 = new LinkedHashMap();       map1.put("1", "Jim");       map1.put("2", "David"); ... Read More

Java Program to Toss a Coin

Samual Sam
Updated on 28-Jun-2024 11:20:56

5K+ Views

Tossing a coin is flipping a coin into the air and letting it fall back down. When you toss a coin, it's like a game where you can choose heads or tails, and the side that lands facing up is the result. This method is used when we want to make decisions or settle things randomly. Problem Statement Create a Java program to simulate the flipping of a coin 10 times, recording the number of times the result is "heads" and "tails". Output Chances = 10 Heads: 3 Tails: 7 Algorithms Step-1: Create an instance of the "Toss" ... Read More

Java program to get random letters

karthikeya Boyini
Updated on 05-Sep-2024 11:24:47

2K+ Views

In this article, we will learn to write a program in Java to get the random letters in both lowercase and uppercase. We will be using Random class from java.util package.The program will first create random lowercase letters by selecting them from the alphabet, and then it will generate random uppercase letters in a similar way.  Problem Statement Write a program in Java to get the random letters both uppercase and lower case. Below is the demostration − Output Lowercase random letters...dlhjsUppercase random letters...BKIZN Step to get random letters Following are the steps to get the random letter using Java ... Read More

How can I generate random booleans in Java?

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

347 Views

To generate random booleans like TRUE or FALSE, at first create a new Random object −Random randNum = new Random();Now, loop through the count of booleans you want and generate random booleans with nextBooleans() method −for (int i = 1; i

How to convert Float array list to float array in Java?

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

3K+ Views

Let us first create a float array list −ArrayList < Float > arrList = new ArrayList < Float > (); arrList.add(5.2 f); arrList.add(10.3 f); arrList.add(15.3 f); arrList.add(20.4 f);Now, convert the float array list to float array. At first, we have set the same size to float array i.e. the same number of elements. After that, we have assigned each value −final float[] arr = new float[arrList.size()]; int index = 0; for (final Float value: arrList) {    arr[index++] = value; }Example Live Demoimport java.util.ArrayList; public class Demo {    public static void main(String[] args) {       ArrayListarrList = new ... Read More

How to generate large random numbers in Java?

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

685 Views

For large random numbers, use BigInteger type in Java. At first, create a Random object −Random randNum = new Random();Now, declare a byte array and generate random bytes −byte[] b = new byte[max]; randNum.nextBytes(b);Now, generate a large random number with BigInteger type −BigInteger bigInt = new BigInteger(b);Example Live Demoimport java.math.BigInteger; import java.util.Random; public class Demo {    public static void main(String... a) {       int max = 10;       Random randNum = new Random();       byte[] b = new byte[max];       randNum.nextBytes(b);       // BigInteger type       BigInteger bigInt ... Read More

Advertisements