Programming Articles - Page 2665 of 3366

How can I check if there are any selected items in Java JList

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

775 Views

To check if there are any selected items, use the following:boolean res = !list.isSelectionEmpty();The value of res would be TRUE IF we have a selected item in the JList.The following is an example to check if there are any selected items in JList:Examplepackage my; import java.awt.event.*; import java.awt.*; import javax.swing.*; class SwingDemo extends JFrame {    static JFrame frame;    static JList list;    public static void main(String[] args) {       frame = new JFrame("JList Demo");       SwingDemo s = new SwingDemo();       JPanel panel = new JPanel();       String sports[]= {"Squash", ... Read More

Functions that can’t be overloaded in C++

Anvi Jain
Updated on 30-Jul-2019 22:30:26

391 Views

In C++, we can overload the functions. But sometimes the overloading is not done. In this section, we will see what are the different cases, in which we cannot overload the functions.When function signatures are same, only the return type is different, then we cannot overload the function.int my_func() {    return 5; } char my_func() {    return 'd'; }When the member functions have the same name and same parameter list in a class, then they cannot be overloaded.class My_Class{    static void func(int x) {       //Something    }    void func(int x) {     ... Read More

How to filter non-null value in Java?

Nancy Den
Updated on 30-Jul-2019 22:30:26

2K+ Views

Let’s say the following is our List with string elements:List leagues = Arrays.asList("BBL", "IPL", "MLB", "FPL", "NBA", "NFL");Now, create a stream and filter elements that end with a specific letter:Stream stream = leagues.stream().filter(leagueName -> leagueName.endsWith("L"));Now, use Objects::nonnull for non-null values:List list = stream.filter(Objects::nonNull).collect(Collectors.toList());The following is an example to filter non-null value in Java:Exampleimport java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; import java.util.stream.Stream; public class Demo {    public static void main(String[] args) {       List leagues = Arrays.asList("BBL", "IPL", "MLB", "FPL", "NBA", "NFL");       Stream stream = leagues.stream().filter(leagueName -> leagueName.endsWith("L"));       List list = ... Read More

How to filter empty string values from a Java List?

Nancy Den
Updated on 30-Jul-2019 22:30:26

1K+ Views

Let’s say we have a String List with an empty value. Here, we have empty array elements before Football and after Squash:List sports = Arrays.asList("", "Football", "Cricket", "Tennis", "Squash", "", "Fencing", "Rugby");Now filter the empty string values. At first, we have used Predicate to negate values:Stream stream = sports.stream(); Predicate empty = String::isEmpty; Predicate emptyRev = empty.negate(); stream.filter(emptyRev).collect(Collectors.toList()));The following is an example to filter empty string values from a List:Exampleimport java.util.Arrays; import java.util.List; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; public class Demo {    public static void main(String[] args) {       List sports = Arrays.asList("", "Football", "Cricket", "Tennis", ... Read More

How to generate a random number in C++?

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

550 Views

Let us see how to generate random numbers using C++. Here we are generating a random number in range 0 to some value. (In this program the max value is 100).To perform this operation we are using the srand() function. This is in the C library. The function void srand(unsigned int seed) seeds the random number generator used by the function rand.The declaration of srand() is like below −void srand(unsigned int seed)It takes a parameter called seed. This is an integer value to be used as seed by the pseudorandom number generator algorithm. This function returns nothing.To get the number ... Read More

How to count element after filtering in Java?

Nancy Den
Updated on 30-Jul-2019 22:30:26

528 Views

Let’s say the following is the String List:List list = new ArrayList(); list.add("Tom"); list.add("John"); list.add("David"); list.add("Paul"); list.add("Gayle"); list.add("Narine"); list.add("Joseph");Now, let’s say you need to filter elements beginning with a specific letter. For that, use filter() and startsWith():long res = list    .stream()    .filter((s) -> s.startsWith("J"))    .count();We have also counted the elements above after filtering using count().The following is an example to count element after filtering in Java:Exampleimport java.util.ArrayList; import java.util.List; public class Demo {    public static void main(final String[] args) {       List list = new ArrayList();       list.add("Tom");       list.add("John"); ... Read More

How does generic lambda work in C++14?

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

293 Views

In C++11, the lambda was introduced. Lambdas are basically a part of, that can be nested inside other function call statements. By combining lambda expressions with the auto keyword, they can be used later.In C++14, these lambda expressions are improved. Here we can get the generalized or generic lambda. For example, if we want to create a lambda, that can add integers, add numbers, also concatenate strings, then we have to use this generalized lambda.Syntax of the lambda expression is looking like this −[](auto x, auto y) { return x + y; }Let us see one example to get the ... Read More

How to convert Stream to TreeSet in Java?

Nancy Den
Updated on 30-Jul-2019 22:30:26

1K+ Views

Let us first create a Stream:Stream stream = Stream.of("UK", "US", "India", "Australia", "Armenia", "Canada", "Poland");Now convert Stream to TreeSet:Set set = stream.collect(Collectors.toCollection(TreeSet::new));The following is an example to convert String to TreeSet in Java:Exampleimport java.util.Set; import java.util.TreeSet; import java.util.stream.Collectors; import java.util.stream.Stream; public class Demo {    public static void main(String[] args) {       Stream stream = Stream.of("UK", "US", "India", "Australia", "Armenia", "Canada", "Poland");       Set set = stream.collect(Collectors.toCollection(TreeSet::new));       set.forEach(val -> System.out.println(val));    } }OutputArmenia Australia Canada India Poland UK USRead More

What is the use of `%p` in printf in C?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

12K+ Views

In C we have seen different format specifiers. Here we will see another format specifier called %p. This is used to print the pointer type data. Let us see the example to get a better idea.Example#include main() {    int x = 50;    int *ptr = &x;    printf("The address is: %p, the value is %d", ptr, *ptr); }OutputThe address is: 000000000022FE44, the value is 50

How to convert string Stream to join them in Java?

Nancy Den
Updated on 30-Jul-2019 22:30:26

598 Views

Let us create string Stream:Stream stream = Stream.of("Bing Bang Theory", "Vampire Diaries", "Game of Thrones", "Homecoming");Convert the above string stream and join them with Collectors:final String str = stream.collect(Collectors.joining(" "));The following is an example to convert string Stream to join them:Exampleimport java.util.stream.Collectors; import java.util.stream.Stream; public class Demo {    public static void main(String[] args) {       Stream stream = Stream.of("Bing Bang Theory", "Vampire Diaries", "Game of Thrones", "Homecoming");       final String str = stream.collect(Collectors.joining(" "));       System.out.println("Join result..."+str);    } }OutputJoin result... Bing Bang Theory Vampire Diaries Game of Thrones HomecomingRead More

Advertisements