user61301
0
Q:

java streams

public class StreamBuilders 
{
     public static void main(String[] args)
     {
         Stream<Integer> stream = Stream.of( new Integer[]{1,2,3,4,5,6,7,8,9} );
         stream.forEach(p -> System.out.println(p));
     }
}
1
//a simple program to demonstrate the use of stream in java 
import java.util.*; 
import java.util.stream.*; 
  
class Demo 
{ 
  public static void main(String args[]) 
  { 
  
    // create a list of integers 
    List<Integer> number = Arrays.asList(2,3,4,5); 
  
    // demonstration of map method 
    List<Integer> square = number.stream().map(x -> x*x). 
                           collect(Collectors.toList()); 
    System.out.println(square); 
  
    // create a list of String 
    List<String> names = 
                Arrays.asList("Reflection","Collection","Stream"); 
  
    // demonstration of filter method 
    List<String> result = names.stream().filter(s->s.startsWith("S")). 
                          collect(Collectors.toList()); 
    System.out.println(result); 
  
    // demonstration of sorted method 
    List<String> show = 
            names.stream().sorted().collect(Collectors.toList()); 
    System.out.println(show); 
  
    // create a list of integers 
    List<Integer> numbers = Arrays.asList(2,3,4,5,2); 
  
    // collect method returns a set 
    Set<Integer> squareSet = 
         numbers.stream().map(x->x*x).collect(Collectors.toSet()); 
    System.out.println(squareSet); 
  
    // demonstration of forEach method 
    number.stream().map(x->x*x).forEach(y->System.out.println(y)); 
  
    // demonstration of reduce method 
    int even = 
       number.stream().filter(x->x%2==0).reduce(0,(ans,i)-> ans+i); 
  
    System.out.println(even); 
  } 
} 
0
Arrays.asList("a1", "a2", "a3")
    .stream()
    .findFirst()
    .ifPresent(System.out::println);  // a1
0
public class Employee {

	private int employeeID;
	private String employeeName;
	private String employeeGender;
	private String employeeCountry;
	private String employeeState;
	private String employeeCity;
	
	public Employee() {
		// TODO Auto-generated constructor stub
	}

	public Employee(int employeeID, String employeeName, String employeeGender, String employeeCountry,
			String employeeState, String employeeCity) {
		super();
		this.employeeID = employeeID;
		this.employeeName = employeeName;
		this.employeeGender = employeeGender;
		this.employeeCountry = employeeCountry;
		this.employeeState = employeeState;
		this.employeeCity = employeeCity;
	}

	public int getEmployeeID() {
		return employeeID;
	}

	public void setEmployeeID(int employeeID) {
		this.employeeID = employeeID;
	}

	public String getEmployeeName() {
		return employeeName;
	}

	public void setEmployeeName(String employeeName) {
		this.employeeName = employeeName;
	}

	public String getEmployeeGender() {
		return employeeGender;
	}

	public void setEmployeeGender(String employeeGender) {
		this.employeeGender = employeeGender;
	}

	public String getEmployeeCountry() {
		return employeeCountry;
	}

	public void setEmployeeCountry(String employeeCountry) {
		this.employeeCountry = employeeCountry;
	}

	public String getEmployeeState() {
		return employeeState;
	}

	public void setEmployeeState(String employeeState) {
		this.employeeState = employeeState;
	}

	public String getEmployeeCity() {
		return employeeCity;
	}

	public void setEmployeeCity(String employeeCity) {
		this.employeeCity = employeeCity;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "[Employee ID : " + employeeID + ", Employee Name : " + employeeName + ", Employee Gender : "
				+ employeeGender + ", Employee Country : " + employeeCountry + ", Employee State : " + employeeState
				+ ", Employee City : " + employeeCity + "]";
	}
	
	public static void main(String[] args) {
		ArrayList<Employee> employees=new ArrayList<Employee>();
		
		employees.add(new Employee(101, "John", "M", "United States", "California", "Los Angeles"));
		employees.add(new Employee(91, "Jacob", "M", "United States", "California", "Los Angeles"));
		employees.add(new Employee(111, "Lisa", "F", "United States", "California", "Los Angeles"));
		employees.add(new Employee(97, "Mary", "F", "United States", "California", "Sacramento"));
		employees.add(new Employee(76, "Christine", "F", "United States", "California", "Sacramento"));
		employees.add(new Employee(114, "David", "M", "United States", "California", "San Jose"));
		employees.add(new Employee(103, "Kevin", "M", "United States", "California", "Oakland"));
		employees.add(new Employee(109, "Joe", "M", "United States", "California", "Oakland"));
		employees.add(new Employee(119, "Mathew", "M", "United States", "California", "San Jose"));
		employees.add(new Employee(99, "Angelina", "F", "United States", "California", "San Diego"));
		employees.add(new Employee(98, "Tom", "M", "United States", "California", "San Diego"));
		employees.add(new Employee(116, "Curl", "M", "United States", "California", "Los Angeles"));
		employees.add(new Employee(66, "Christopher", "M", "United States", "California", "Oakland"));
		employees.add(new Employee(56, "Chelse", "F", "United States", "California", "Oakland"));
		employees.add(new Employee(88, "Murali", "M", "United States", "California", "San Jose"));
		employees.add(new Employee(87, "Daisy", "F", "United States", "California", "Sacramento"));
		employees.add(new Employee(85, "Niza", "F", "United States", "Virginia", "Richmond"));
		employees.add(new Employee(86, "Chris", "M", "United States", "Virginia", "Fairfax"));
		employees.add(new Employee(90, "Andrew", "M", "United States", "Virginia", "Reston"));
		
	}

}

Operations:

1. Get list of all the employees from "California"; Return a List
2. Count the number of Females; Return a Count
3. Add 10 to the ID of each Employee; Return the updated List
4. Sort in the Descending order by employee name (z-a); Return the List
5. Get the details of the second highest employee ID; Return the employee

Solution:

System.out.println(employees.stream().filter(employee->employee.getEmployeeState().equals("California")).collect(Collectors.toList()));
System.out.println(employees.stream().map(emp->emp.getEmployeeID()+10).collect(Collectors.toList()));
System.out.println(employees.stream().filter(employee->employee.getEmployeeGender().equalsIgnoreCase("f")).count());
System.out.println(employees.stream().sorted((e1,e2)->e2.getEmployeeName().compareTo(e1.getEmployeeName())).collect(Collectors.toList()));
Collections.sort(employees, (s1,s2)-> s2.getEmployeeID() - s1.getEmployeeID());
System.out.println(employees.get(1));
0

New to Communities?

Join the community