Found 33676 Articles for Programming

How to create a Queue from LinkedList in Java?

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

391 Views

Let us create a queue from LinkedList like this −Queuequeue = new LinkedList(); queue.add("P"); queue.add("Q"); queue.add("R"); queue.add("S"); queue.add("T"); queue.add("U"); queue.add("V");Now, use a List to display the elements of the queue −Listlist = new ArrayList(queue);Example Live Demoimport java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Queue; public class Demo {    public static void main(String[] args) {       Queuequeue = new LinkedList();       queue.add("P");       queue.add("Q");       queue.add("R");       queue.add("S");       queue.add("T");       queue.add("U");       queue.add("V");       Listlist = new ArrayList(queue);       for (Object ... Read More

Java program to put value to a Properties list

karthikeya Boyini
Updated on 30-Oct-2024 18:44:44

390 Views

In this article, we will learn how to use the Properties class in Java to store and display key-value pairs. The Properties class is useful for handling configuration settings or storing data in a structured way, where both keys and values are strings. By using the put() method, we can easily add values to a property list and later retrieve or display these key-value pairs. This approach provides a simple and efficient way to manage data in a key-value format in Java applications. Problem Statement A properties list contains country-year pairs. Write a Java program to store values ... Read More

Java program to convert properties list into a Map

Samual Sam
Updated on 19-Sep-2024 21:55:48

1K+ Views

In this article, we will learn how to convert a Properties list into a Map in Java. The Properties class is commonly used to store key-value pairs, but sometimes you may need to work with these pairs in a Map structure. We will demonstrate how to take the properties and convert them into a HashMap. Problem Statement Write a program in Java to convert the properties list into a Map structure − Output Key and Value of the Map... P: 1 Q: 2 R: 3 S: 4 T: 5 U: 6 V: 7 Steps to convert the properties list into ... Read More

Java Program to get Tail Map from TreeMap

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

155 Views

To get Tail Map from TreeMap, let us first create a TreeMap and set key-value pair −TreeMaptMap = new TreeMap(); tMap.put("1", "A"); tMap.put("2", "B"); tMap.put("3", "C"); tMap.put("4", "D"); tMap.put("5", "E"); tMap.put("6", "F"); tMap.put("7", "G");Now, let’s say you need to get the tailmap from 2 i.e. all the key-value pairs after 2. For that, use the method tailMap() −SortedMapmap = tMap.tailMap("2");Example Live Demoimport java.util.SortedMap; import java.util.TreeMap; public class Demo {    public static void main(String[] args) {       TreeMaptMap = new TreeMap();       tMap.put("1", "A");       tMap.put("2", "B");       tMap.put("3", "C");       ... Read More

Simple way to find if two different lists contain exactly the same elements in Java

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

2K+ Views

Two lists are equal if they consist of same number of elements in the same order.Let’s say we have the following two lists −ListarrList1 = Arrays.asList(new Integer[] { 10, 20, 30, 45, 55, 70, 90, 100 }); ListarrList2 = Arrays.asList(new Integer[] {15, 25, 35, 50, 55, 75, 95, 120});Now, let’s find out whether both the lists are equal or not −arrList1.equals(arrList2);If both the above lists have equal elements, then TRUE is returned, else FALSE is the return value.Example Live Demoimport java.util.Arrays; import java.util.List; public class Demo {    public static void main(String[] a) {       ListarrList1 = Arrays.asList(new Integer[] ... Read More

Check the frequency of an element in Java with Collections.frequency

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

238 Views

Create a List in Java −List< String >list = Arrays.asList("P", "Q", "R", "P", "T", "P", "U", "V", "W", "X", "W" );Now, let’s say you want to get the frequency of element P. For that, use the Collections.frequency() method −Collections.frequency(list, "P");A shown above, we can find the occurrence of any letter as well −Collections.frequency(list, "T");Example Live Demoimport java.util.Arrays; import java.util.Collections; import java.util.List; public class Demo {    public static void main(String[] args) {       Listlist = Arrays.asList("P", "Q", "R", "P", "T", "P", "U", "V", "W", "X", "W" );       int checkOccurrence = Collections.frequency(list, "P");       System.out.println("Occurrence ... Read More

What is the best way to check capacity in Java?

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

1K+ Views

To check capacity in Java, firstly create a list and add elements. After that use ensureCapacity() and increase the capacity.Let us first create an ArrayList and add some elements −ArrayListarrList = new ArrayList(5); arrList.add(100); arrList.add(200); arrList.add(300); arrList.add(400); arrList.add(500);Now, increase the capacity of the ArrayList −arrList.ensureCapacity(15);Meanwhile, with the size() method, you can check the current size of the ArrayList as well.Example Live Demoimport java.util.ArrayList; public class Demo {    public static void main(String[] a) {       ArrayListarrList = new ArrayList(5);       arrList.add(100);       arrList.add(200);       arrList.add(300);       arrList.add(400);       arrList.add(500); ... Read More

Java Program to convert java.util.Date to ZonedDateTime

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

3K+ Views

Create a Date object −Date date = new Date();Now, set the ZonedId to default −final ZoneId id = ZoneId.systemDefault();Convert java.util.date to ZonedDateTime −System.out.println(ZonedDateTime.ofInstant(date.toInstant(), id));Example Live Demoimport java.time.ZoneId; import java.time.ZonedDateTime; import java.util.Date; public class Demo {    public static void main(String[] args) {       Date date = new Date();       final ZoneId id = ZoneId.systemDefault();       System.out.println(ZonedDateTime.ofInstant(date.toInstant(), id));    } }Output2019-04-19T00:37:33.344+05:30[Asia/Calcutta]

Java Program to change a file attribute to writable

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

139 Views

Let’s say our file is “input.txt”, which is set read-only −File myFile = new File("input.txt"); myFile.createNewFile(); myFile.setReadOnly();Now, set the above file to writable −myFile.setWritable(true);After that, you can use canWrite() to check whether the file is writable or not.Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] args) throws Exception {       File myFile = new File("input.txt");       myFile.createNewFile();       myFile.setReadOnly();       if (myFile.canWrite()) {          System.out.println("Writable!");       } else {          System.out.println("Read only mode!");       }       ... Read More

How to get the Checksum of a Byte Array in Java?

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

1K+ Views

Create a Byte Array for which you want the Checksum −byte[] arr = "This is it!".getBytes();Now, create a Checksum object −Checksum checksum = new Adler32(); checksum.update(arr, 0, arr.length);The update() above updates the current checksum with the specified array of bytes.Now, get the checksum with getValue() method, which gives the current checksum value.Example Live Demoimport java.util.zip.Adler32; import java.util.zip.Checksum; public class Demo {    public static void main(String[] argv) throws Exception {       byte[] arr = "This is it!".getBytes();       Checksum checksum = new Adler32();       checksum.update(arr, 0, arr.length);       long res = checksum.getValue();   ... Read More

Advertisements