Found 33676 Articles for Programming

Java Program to convert Stream to List

Daniol Thomas
Updated on 30-Jul-2019 22:30:26

231 Views

Declare and initialize an Integer array:Integer[] arr = {50, 100, 150, 200, 300, 400, 500, 600, 700, 800, 1000};Now, create a stream with the above elements:Stream stream = Arrays.stream(arr);To convert the above stream to list, use Collectors.toList():stream.collect(Collectors.toList()The following is an example to convert Stream to List:Exampleimport java.util.Arrays; import java.util.stream.Collectors; import java.util.stream.Stream; public class Demo {    public static void main(String[] args) {       Integer[] arr = {50, 100, 150, 200, 300, 400, 500, 600, 700, 800, 1000};       Stream stream = Arrays.stream(arr);       System.out.println("Stream = "+stream.collect(Collectors.toList()));    } }OutputStream = [50, 100, 150, 200, 300, 400, 500, 600, 700, 800, 1000]

Java Program to retrieve a Stream from a List

Daniol Thomas
Updated on 30-Jul-2019 22:30:26

164 Views

Let us first create a List:List list = Arrays.asList(25, 50, 100, 200, 250, 300, 400, 500);Now, create a stream from the List:Stream stream = list.stream(); Arrays.toString(stream.toArray()));The following is an example to retrieve a Stream from a ListExampleimport java.util.Arrays; import java.util.List; import java.util.stream.Stream; public class Demo {    public static void main(String[] args) {       List list = Arrays.asList(25, 50, 100, 200, 250, 300, 400, 500);       System.out.println("List elements...");       for (int res : list)       {          System.out.println(res);       }       Stream stream = list.stream();       System.out.println("Stream = "+Arrays.toString(stream.toArray()));    } }OutputList elements... 25 50 100 200 250 300 400 500 Stream = [25, 50, 100, 200, 250, 300, 400, 500]

Modulus of two float or double numbers using C

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

8K+ Views

Here we will see how to get the modulus of two floating or double type data in C. The modulus is basically finding the remainder. For this, we can use the remainder() function in C. The remainder() function is used to compute the floating point remainder of numerator/denominator.So the remainder(x, y) will be like below.remainder(x, y) = x – rquote * yThe rquote is the value of x/y. This is rounded towards the nearest integral value. This function takes two arguments of type double, float, long double, and returns the remainder of the same type, that was given as argument. ... Read More

trunc() , truncf() , truncl() in C language

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

2K+ Views

Here we will see three functions. These functions are trunc(), truncf() and the truncl(). These functions are used to convert floating point values into truncated form.The trunc() FunctionThis function is used to truncate double type value. And return only the integer part. The syntax is like below.double trunc(double argument)Example#include #include main() {    double a, b, x, y;    x = 53.26;    y = 75.86;    a = trunc(x);    b = trunc(y);    printf("The value of a: %lf", a);    printf("The value of a: %lf", b); }OutputThe value of a: 53.000000 The value of a: 75.000000The ... Read More

setjump() and longjump() in C

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

3K+ Views

In this section, we will see what are the setjump and longjump in C. The setjump() and longjump() is located at setjmp.h library. The syntax of these two functions is like below.setjump(jmp_buf buf) : uses buf to store current position and returns 0. longjump(jmp_buf buf, i) : Go back to place pointed by buf and return i.These are used in C for exception handling. The setjump() can be used as try block, and longjump() can be used as throw statement. The longjump() transfers control the pointe which is pointed by setjump().Here we will see how to print a number 100 ... Read More

How to measure time taken by a function in C?

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

15K+ Views

Here we will see how to calculate the time taken by the process. For this problem, we will use the clock() function. The clock() is present in the time.h header file.To get the elapsed time, we can get the time using clock() at the beginning, and at the end of the tasks, then subtract the values to get the differences. After that, we will divide the difference by CLOCK_PER_SEC (Number of clock ticks per second) to get the processor time.Example#include #include void take_enter() {    printf("Press enter to stop the counter ");    while(1) {       ... Read More

How to change the output of printf() in main()?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

331 Views

Here we will see how to change the output of the printf() function from main(). Here we will define a function that will change all of the printf() statements with the given type to another type.We will use the #define macro to do this task. This macro will be defined inside the function. We can directly put the #define line without using it in the function, but in that case always the printf() will be changed. To control it using main, we have to call the function first.Example#include void changePrintf() { //always any printf will print 50    #define ... Read More

Write a one line C function to round floating point numbers

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

759 Views

Here we will see how to write one-line C function, that can round floating point numbers. To solve this problem, we have to follow these steps.Take the numberif the number is positive, then add 0.5Otherwise, subtract 0.5Convert the floating point value to an integer using typecastingExample#include    int my_round(float number) {    return (int) (number < 0 ? number - 0.5 : number + 0.5); } int main () {    printf("Rounding of (2.48): %d", my_round(2.48));    printf("Rounding of (-5.79): %d",my_round(-5.79)); }OutputRounding of (2.48): 2 Rounding of (-5.79): -6

Can we disable JComboBox arrow button in Java?

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

631 Views

Yes, we can do that using removeArrow() method.The following is an example to disable JComboBox arrow button:Exampleimport java.awt.Component; import java.awt.Container; import javax.swing.*; public class SwingDemo {    public static void main(String[] args) {       String[] strValues = {"One", "Two"};       JComboBox comboBox = new JComboBox(strValues);       removeArrow(comboBox);       JOptionPane.showMessageDialog(null, comboBox);    }    private static void removeArrow(Container container) {       Component[] c = container.getComponents();       for (Component res : c) {          if (res instanceof AbstractButton) {             container.remove(res);          }       }    } }Output

How to handle action event for JComboBox in Java?

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

824 Views

The following is an example to handle action event for JComboBox in Java:Exampleimport java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new JFrame();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JComboBox combo = new JComboBox(new String[] { "One", "Two", "Three", "Four", "Five", "Six" });       JButton add = new JButton("Add");       add.addActionListener(new ActionListener() {          @Override          public void actionPerformed(ActionEvent e) {             ... Read More

Advertisements