Found 4334 Articles for Java 8

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

102 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

62 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

3K+ 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 30-Jul-2019 22:30:25

790 Views

To generate random letters, set letters as a strong and use the toCharArray() to convert it into character array −"abcdefghijklmnopqrstuvwxyz".toCharArray()Now, use the nextInt() to generate random letters from it −System.out.println("" + "abcdefghijklmnopqrstuvwxyz".toCharArray()[randNum.nextInt("abcdefghijklmnopqrstuvwxyz".toCharArray().length)]);Above, initially we have created a Random object −static Random randNum = new Random();Example Live Demoimport java.util.Random; public class Demo {    static Random randNum = new Random();    public static void main(String args[]) {       System.out.println("Lowercase random letters...");       for (int i = 0; i < 5; i++) {          System.out.println("" + "abcdefghijklmnopqrstuvwxyz".toCharArray()[randNum.nextInt("abcdefghijklmnopqrstuvwxyz".toCharArray().length)]);       }       System.out.println("Uppercase random ... Read More

How can I generate random booleans in Java?

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

245 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

2K+ 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

463 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

Program to iterate over a List using Java 8 Lambda

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

2K+ Views

Let us first create a List and add elements −ArrayListarrayList = new ArrayList(); arrayList.add("100"); arrayList.add("200"); arrayList.add("300"); arrayList.add("400"); arrayList.add("500");Now, iterate over it with Lambda Expressions −ArrayListlist = arrayList; System.out.println("Iterating..."); list.stream().forEach(elem -> System.out.println(elem));Example Live Demoimport java.util.ArrayList; import java.util.List; public class Demo {    public static void main(String[] args) {       ArrayListarrayList = new ArrayList();       arrayList.add("100");       arrayList.add("200");       arrayList.add("300");       arrayList.add("400");       arrayList.add("500");       arrayList.add("600");       arrayList.add("700");       arrayList.add("800");       arrayList.add("900");       arrayList.add("1000");       System.out.println("ArrayList...");       ... Read More

How to test if a List is an Unmodifable List in Java?

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

859 Views

We will first set a list to unmodifiable and after that test if it is unmodifiable or not. Let us create a List and add elements −List list = new LinkedList (); list.add(10); list.add(20); list.add(30); list.add(40); list.add(50);Set the above list to unmodifiable −Listunmodifiable = Collections.unmodifiableList(list);Now, use If-else, to check whether the list in unmodifiable or not −if (unmodifiable.getClass().getName().contains("UnmodifiableList"))    System.out.println("This is an UnmodifiableList" ); else    System.out.println("This is not an UnmodifiableList" );Example Live Demoimport java.util.Collections; import java.util.LinkedList; import java.util.List; public class Demo {    public static void main(String[] args) {       Listlist = new LinkedList();     ... Read More

How to shuffle a List in Java

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

155 Views

First, create an Integer array −Integer[] strArray = new Integer[] { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };Now, convert it into a List −Listlist = Arrays.asList(strArray);Use Collections to shuffle as shown below −Collections.shuffle(list);Example Live Demoimport java.util.Arrays; import java.util.Collections; import java.util.List; public class Demo {    public static void main(String args[]) {       Integer[] strArray = new Integer[] { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };       Listlist = Arrays.asList(strArray);       System.out.println("List = "+list);       Collections.shuffle(list);       System.out.println("Shuffled List = "+list);    } }OutputList = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] Shuffled List = [100, 90, 40, 70, 20, 10, 80, 30, 60, 50]

Advertisements