Found 9150 Articles for Object Oriented Programming

Map to add String value to each element in Java

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

833 Views

Let’s say the following is our String List −List list = Arrays.asList("Football", "Basketball", "Hockey", "Cricket", "Fencing");Now, map to add string to each element −List str = list.stream().map(name -> "Sports " + name + " Outdoor")    .collect(Collectors.toList());Above, we have added “Sports” and “Outdoor” strings to each element.The following is an example to Map and add string value to each element in Java −Exampleimport java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Demo {    public static void main(String[] args) {       List list = Arrays.asList("Football", "Basketball", "Hockey", "Cricket", "Fencing");       List str = list.stream().map(name -> "Sports " ... Read More

Java program to map string list to lowercase and sort

Alshifa Hasnain
Updated on 03-Mar-2025 18:48:58

705 Views

In this article, we will learn to map a String list to lowercase and sort in Java. We need to manipulate the elements of a list, such as converting all strings to lowercase and sorting them. This is particularly useful when dealing with user input, file processing, or data normalization. Problem Statement Given a list of strings, we want to convert all strings to lowercase and sort the list in reverse alphabetical order.For Example: Input ["Apple", "banana", "Cherry", "date"] Output ["date", "cherry", "banana", "apple"] Using Streams API The Streams API provides a useful and functional approach to processing ... Read More

How to Map double to int Object with mapToInt and mapToObj in Java?

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

473 Views

At first, we have set the stream −Stream.of(3.5, 4.7, 7.9, 8.2, 9.1, 10.5, 12.3, 15.8)Now to Map double to int Object, use mapToObj −.mapToInt(Double::intValue) .mapToObj(a -> "val" + a)The following is an example to Map double to int Object with mapToInt and mapToObj −Exampleimport java.util.stream.Stream; public class Demo {    public static void main(String[] args) throws Exception {       Stream.of(3.5, 4.7, 7.9, 8.2, 9.1, 10.5, 12.3, 15.8)       .mapToInt(Double::intValue)       .mapToObj(a -> "val" + a)       .forEach(System.out::println);    } }Outputval3 val4 val7 val8 val9 val10 val12 val15

How to Map IntSteam to String object in Java

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

201 Views

To Map IntStream to String object, use mapToObj and within that set the values −.mapToObj(val -> "z" + val)Before that, we used range() on IntStream −IntStream.range(1, 10)The following is an example to map IntStream to String object in Java −Exampleimport java.util.stream.IntStream; public class Demo {    public static void main(String[] args) throws Exception {       IntStream.range(1, 10)       .mapToObj(val -> "z" + val)       .forEach(System.out::println);    } }Outputz1 z2 z3 z4 z5 z6 z7 z8 z9

How can I filter string value with starting letter?

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

3K+ Views

Let’s say the following is our String List −List list = Arrays.asList("Jonny", "David", "Tony", "Jacob", "Smith", "Bravo", "Gayle", "John");Now filter the string value with starting letter −Stream stream = list.stream().filter(name -> name.startsWith("J"));The following is an example to filter string value with starting letter −Exampleimport java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class Demo {    public static void main(String[] args) {       List list = Arrays.asList("Jonny", "David", "Tony", "Jacob", "Smith",          "Bravo", "Gayle", "John");       System.out.println("List with elements...");       for (String res : list) {          System.out.print(res+" ... Read More

How to filter String stream and map to lower case in Java? Perform sort as well.

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

3K+ Views

Let’s say the following is String array, which we have converted to List −Arrays.asList("DE", "GH", "JK", "MN", "PQ", "RS", "TU", "VW", "XY", "BC")Now filter String stream and map to lower case −.stream() .filter(a-> a.startsWith("V")) .map(String::toLowerCase)To sort now, use the sorted() and display using forEach().The following is an example to filter string stream and map to lowercase −Exampleimport java.util.Arrays; public class Demo {    public static void main(String[] args) throws Exception {       Arrays.asList("DE", "GH", "JK", "MN", "PQ", "RS", "TU", "VW", "XY", "BC")       .stream()       .filter(a-> a.startsWith("V"))       .map(String::toLowerCase)       ... Read More

How to filter String list by starting value in Java?

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

2K+ Views

Let’s first create a String list −List list = new ArrayList(); list.add("wxy"); list.add("zabc"); list.add("ddd2"); list.add("def"); list.add("ghi"); list.add("wer"); list.add("uij"); list.add("wqy");To filter String list by starting value, use filter() and startsWith() −list.stream().filter((b) -> b.startsWith("w"))The following is an example to filter string list by starting value −Exampleimport java.util.ArrayList; import java.util.List; public class Demo {    public static void main(final String[] args) {       List list = new ArrayList();       list.add("wxy");       list.add("zabc");       list.add("ddd2");       list.add("def");       list.add("ghi");       list.add("wer");       list.add("uij");       list.add("wqy");   ... Read More

How to add tooltip to JLabel in Java?

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

1K+ Views

Tooltip is visible whenever you will place the mouse cursor on the label. Use the setToolTipText() method to add tooltip to JLabel −label.setToolTipText("This is a demo tooltip");The following is an example to add tooltip to JLabel −Exampleimport java.awt.Color; import java.awt.Font; import javax.swing.*; import javax.swing.border.Border; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Demo");       JLabel label;       label = new JLabel("Demo Label!");       label.setFont(new Font("Verdana", Font.PLAIN, 14));       label.setToolTipText("This is a demo tooltip");       Border border = BorderFactory.createLineBorder(Color.ORANGE);     ... Read More

How to add line border to JLabel in Java?

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

677 Views

Use the createLineBorder() method to ad line border to JLabel −Border border = BorderFactory.createLineBorder(Color.ORANGE); label.setBorder(border);Above, we have set the line border to color orange.The following is an example to add line border to JLabel −Examplepackage my; import java.awt.Color; import java.awt.Font; import javax.swing.*; import javax.swing.border.Border; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Demo");       JLabel label;       label = new JLabel("Demo Label!", JLabel.RIGHT);       label.setFont(new Font("Verdana", Font.PLAIN, 13));       Border border = BorderFactory.createLineBorder(Color.ORANGE);       label.setBorder(border);       frame.add(label);   ... Read More

Java Program to set alignment for text in JLabel

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

8K+ Views

JLabel Left AlignedThe following is an example to set left alignment for JLabel −Exampleimport java.awt.Font; import javax.swing.*; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Label Demo");       JLabel label;       label = new JLabel("Left aligned!", JLabel.LEFT);       label.setFont(new Font("Verdana", Font.PLAIN, 13));       frame.add(label);       frame.setSize(500, 300);       frame.setVisible(true);    } }OutputJLabel Center AlignedThe following is an example to set center alignment for JLabel −Exampleimport java.awt.Font; import javax.swing.*; public class SwingDemo {    public static void main(String args[]) ... Read More

Advertisements