Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Compile and Execute Java Online

package com.india.sachin;
import java.util.*;
import java.util.stream.*;


class Student
{
    int age;
    String name;
    int marks;
    
    
    public Student(int age,String name,int marks)
    {
        
        this.age=age;
        this.name=name;
        this.marks=marks;
        
    }
    public int getAge() {
		return age;
	}


	public void setAge(int age) {
		this.age = age;
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public int getMarks() {
		return marks;
	}


	public void setMarks(int marks) {
		this.marks = marks;
	}
	

    
    
}

public class HelloWorld
{
    
    
    public static void main(String[] args)
    {
        
        
        ArrayList<Student> al=new ArrayList<Student>();
        
        
        al.add(new Student(27,"sachin",78));
        al.add(new Student(26,"nitin",45));
        
      /*  
        al.forEach((n)->System.out.println(n.age+" "+n.name));
        
        
        al.forEach(
            
           (m)->System.out.println(m.marks)
            
            );
      */      
            
         /*   al.stream().filter(p->p.age>26).forEach((p)->System.out.println(p.age+" "+p.name+" "+p.marks));
         */   
            
            Set<Integer> set=al.stream().filter(p->p.age>26).map(p->p.age).collect(Collectors.toSet());
            
            
        for(int a:set)
        {
       //     System.out.println(a);
        }
        
        
        Set<Student> set1=al.stream().sorted(new Comparator<Student>()
        {
            public int compare(Student s1,Student s2)
            {
                if(s1.getAge()<s2.getAge())
                {
                    return 1;
                }
                else if(s1.getAge()>s2.getAge())
                {
                    return -1;
                }
                else
                {
                    return 0;
                }
            }
        }
        
        ).collect(Collectors.toSet());
        
    
                set1.forEach((n)->System.out.println(n.age+" "+n.name+" "+n.marks));
                
    Map<Integer,String> ma=al.stream().collect(Collectors.toMap(m->m.age,m->m.name));            
                
        System.out.println(ma);        
                
    }
    
    
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.