Found 4336 Articles for Java 8

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

90 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 30-Jul-2019 22:30:25

384 Views

Create a LinkedHashSet −LinkedHashSetset = new LinkedHashSet();Now, add elements to the Set −set.add(20); set.add(60); set.add(80); set.add(120); set.add(150); set.add(200);For removing the elements −set.remove(150); set.remove(260);Above process of insertion and deletion won’t affect the insertion order.Example Live Demoimport java.util.LinkedHashSet; public class Demo {    public static void main(String[] args) {       LinkedHashSetset = new LinkedHashSet();       set.add(20);       set.add(60);       set.add(80);       set.add(120);       set.add(150);       set.add(200);       set.add(220);       set.add(260);       set.add(380);       System.out.println("Set = "+set);       set.remove(150);   ... Read More

How to create a Queue from LinkedList in Java?

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

298 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 Property list

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

232 Views

First, set properties and use put() to put value to property list −Properties p = new Properties(); p.put("India", "1983"); p.put("Australia", "1987"); p.put("Pakistan", "1992"); p.put("Srilanka", "1996");Now, display the properties key and value pairs −Sets = p.keySet(); for (Object record: s) System.out.println(record + " / " + p.getProperty((String) record));Exampleimport java.util.Properties; import java.util.Set; class Demo {    public static void main(String args[]) {       Properties p = new Properties();       p.put("India", "1983");       p.put("Australia", "1987");       p.put("Pakistan", "1992");       p.put("Srilanka", "1996");       Sets = p.keySet();       for (Object record: ... Read More

Java Program to convert Properties list into a Map

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

580 Views

To convert Properties list into a map, let us first create an object of Properties class −=Properties p = new Properties();Now, use setProperty() to set key-value pair −p.setProperty("P", "1"); p.setProperty("Q", "2"); p.setProperty("R", "3"); p.setProperty("S", "4"); p.setProperty("T", "5"); p.setProperty("U", "6");With that, the following is how the Properties list is converted into a Map −HashMapmap = new HashMap((Map) p);Example Live Demoimport java.util.HashMap; import java.util.Map; import java.util.Properties; import java.util.Set; public class Demo {    public static void main(String args[]) {       Properties p = new Properties();       p.setProperty("P", "1");       p.setProperty("Q", "2");       p.setProperty("R", "3");   ... Read More

Java Program to get Tail Map from TreeMap

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

83 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

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

175 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]

Advertisements