Java Programs

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Prime no

int i =0;
int num =0;
//Empty String
String primeNumbers = "";

for (i = 1; i <= 100; i++)


{
int counter=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
{
counter = counter + 1;
}
}
if (counter ==2)
{
//Appended the Prime number to the String
primeNumbers = primeNumbers + i + " ";
}
}
System.out.println("Prime numbers from 1 to 100 are :");
System.out.println(primeNumbers);

Prime numbers from 1 to 100 are :2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83


89 97

import java.util.ArrayList;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class StreamListSorting {


public static void main(String[] args) {

// sort employee by salary in ascending order


List < Employee > employees = new ArrayList < Employee > ();
employees.add(new Employee(10, "Ramesh", 30, 400000));
employees.add(new Employee(20, "John", 29, 350000));
employees.add(new Employee(30, "Tom", 30, 450000));
employees.add(new Employee(40, "Pramod", 29, 500000));

List < Employee > employeesSortedList1 = employees.stream()


.sorted((o1, o2) -> (int)(o1.getSalary() -
o2.getSalary())).collect(Collectors.toList());
System.out.println(employeesSortedList1);
List < Employee > employeesSortedList2 = employees.stream()
.sorted(Comparator.comparingLong(Employee::getSalary)).collect(Collect
ors.toList()); //ascending order
System.out.println(employeesSortedList2);
}
}

[Employee [id=20, name=John, age=29, salary=350000], Employee [id=10,


name=Ramesh, age=30, salary=400000], Employee [id=30, name=Tom, age=30,
salary=450000], Employee [id=40, name=Pramod, age=29, salary=500000]]
[Employee [id=20, name=John, age=29, salary=350000], Employee [id=10,
name=Ramesh, age=30, salary=400000], Employee [id=30, name=Tom, age=30,
salary=450000], Employee [id=40, name=Pramod, age=29, salary=500000]]

You might also like