Java Articles - Page 270 of 440

Java Program to create a new list with values from existing list with Function Mapper

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

184 Views

To create a new list with values from fields from existing list with Function Mapper, the following is an example. Here, we have a class Employee −class Employee {    private String emp_name;    private int emp_age;    private String emp_zone; }The following is an example that creates a new list from existing with Function Mapper −Example Live Demoimport java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Function; public class Demo {    public static void main(String args[]) {       List < Employee > emp = Arrays.asList(new Employee("Jack", 29, "South"), new Employee("Tom", 24, "North"),          new Employee("Harry", 35, ... Read More

Java program to add and remove elements from a set which maintains the insertion order

karthikeya Boyini
Updated on 19-Nov-2024 18:23:22

719 Views

In this article, we will learn how to use a LinkedHashSet in Java to add and remove elements while maintaining the insertion order. You will see how elements are stored, removed, and updated in a LinkedHashSet without altering the sequence in which they were added. Problem Statement Write a Java program to add elements to a LinkedHashSet, remove specific elements, and display the updated set while ensuring the insertion order of the elements remains unchanged. Below is a demonstration of the same − Input Set = [20, 60, 80, 120, 150, 200, 220, 260, 380] Output Set = [20, 60, ... Read More

How to create a Queue from LinkedList in Java?

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

438 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

457 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

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

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

281 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

Java Program to change a file attribute to writable

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

170 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

How to extract multiple integers from a String in Java?

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

918 Views

Let’s say the following is our string with integer and characters −String str = "(29, 12; 29, ) (45, 67; 78, 80)";Now, to extract integers, we will be using the following pattern −\dWe have set it with Pattern class −Matcher matcher = Pattern.compile("\d+").matcher(str);Example Live Demoimport java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String[] args) {       String str = "(29, 12; 29, ) (45, 67; 78, 80)";       Matcher matcher = Pattern.compile("\d+").matcher(str);       Listlist = new ArrayList();       while(matcher.find()) {          list.add(Integer.parseInt(matcher.group()));       }       System.out.println("Integers = "+list);    } }OutputIntegers = [29, 12, 29, 45, 67, 78, 80]

Java Program to get frequency of words with Lambda Expression

Alshifa Hasnain
Updated on 13-Dec-2024 13:55:29

735 Views

In this article, we will learn how to calculate the frequency of words in a text using Java and Lambda Expressions. By leveraging concise syntax and functional programming techniques, we can efficiently process text data and determine word occurrences. This approach is particularly useful for applications like text analysis and natural language processing. What is Lambda Expression? A lambda expression in Java is a concise way to represent an anonymous function(a function without a name). Introduced in Java 8, it simplifies the implementation of functional interfaces (interfaces with a single abstract method), enabling cleaner and more readable code. Syntax of ... Read More

Advertisements