Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

Lamba in java 8

Hi,
what is Lamda and its uses  in java 8. I was reading below link

http://www.java2blog.com/2014/06/lambda-expressions-in-java-8.html


when i ran example

package snippet;

import java.util.ArrayList;  
import java.util.Collections;  
import java.util.Comparator;  
import java.util.List;  
  
public class ComparatorMain {  
  
 /** 
  * @author Arpit Mandliya 
  */  
 public static void main(String[] args) {  
  Country indiaCountry=new Country(1, "India");  
  Country chinaCountry=new Country(4, "China");  
  Country nepalCountry=new Country(3, "Nepal");  
  Country bhutanCountry=new Country(2, "Bhutan");  
  
  List<Country> listOfCountries = new ArrayList<Country>();  
  listOfCountries.add(indiaCountry);  
  listOfCountries.add(chinaCountry);  
  listOfCountries.add(nepalCountry);  
  listOfCountries.add(bhutanCountry);  
  
  System.out.println("Before Sort by name : ");  
  for (int i = 0; i < listOfCountries.size(); i++) {  
   Country country=(Country) listOfCountries.get(i);  
   System.out.println("Country Id: "+country.getCountryId()+"||"+"Country name: "+country.getCountryName());  
  }  
  
  //Sort by countryName  
  // Anonymous Comparator  
  // old way  
  Collections.sort(listOfCountries,new Comparator<country>() {  
  
   @Override  
   public int compare(Country o1, Country o2) {  
    return o1.getCountryName().compareTo(o2.getCountryName());  
   }  
  });  
    
  // Using lambda expression  
  Collections.sort(listOfCountries,(o1,o2)-> o1.getCountryName().compareTo(o2.getCountryName()));  
    
  System.out.println("After Sort by name: ");  
  for (int i = 0; i < listOfCountries.size(); i++) {  
   Country country=(Country) listOfCountries.get(i);  
   System.out.println("Country Id: "+country.getCountryId()+"|| "+"Country name: "+country.getCountryName());  
        
   }  
 }  
  
}

Open in new window


package snippet;    
public class Country{  
    int countryId;  
    String countryName;  
      
    public Country(int countryId, String countryName) {  
        super();  
        this.countryId = countryId;  
        this.countryName = countryName;  
    }  
  
    public int getCountryId() {  
        return countryId;  
    }  
    public void setCountryId(int countryId) {  
        this.countryId = countryId;  
    }  
    public String getCountryName() {  
        return countryName;  
    }  
    public void setCountryName(String countryName) {  
        this.countryName = countryName;  
    }  
} 

Open in new window


i am getting below error at line 43

Multiple markers at this line
      - o2 cannot be resolved
      - Syntax error on token "-", --
       expected
      - Syntax error on token ",", .
       expected
      - o1 cannot be resolved to a
       variable
      - o1 cannot be resolved

and below error at line 34

Multiple markers at this line
      - country cannot be resolved to a type
      - The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments
       (List<Country>, new Comparator<country>(){})
      - The type new Comparator<country>(){} must implement the inherited abstract method Comparator<country>.compare
       (country, country)

at below error at line 37
The method compare(Country, Country) of type new Comparator<country>(){} must override or implement a supertype method


Please advise on how to fix it.
ASKER CERTIFIED SOLUTION
Avatar of CPColin
CPColin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

when i go to Eclipse --->window-->preferences--->compiler I see drop down till 1.7. I do not see 1.8. How do i see 1.8 in dropdown to select. please advise
Avatar of gudii9

ASKER

package snippet;

import java.util.ArrayList;  
import java.util.Collections;  
import java.util.Comparator;  
import java.util.List;  
  
public class ComparatorMain {  
  
 /** 
  * @author Arpit Mandliya 
  */  
 public static void main(String[] args) {  
  Country indiaCountry=new Country(1, "India");  
  Country chinaCountry=new Country(4, "China");  
  Country nepalCountry=new Country(3, "Nepal");  
  Country bhutanCountry=new Country(2, "Bhutan");  
  
  List<Country> listOfCountries = new ArrayList<Country>();  
  listOfCountries.add(indiaCountry);  
  listOfCountries.add(chinaCountry);  
  listOfCountries.add(nepalCountry);  
  listOfCountries.add(bhutanCountry);  
  
  System.out.println("Before Sort by name : ");  
  for (int i = 0; i < listOfCountries.size(); i++) {  
   Country country=(Country) listOfCountries.get(i);  
   System.out.println("Country Id: "+country.getCountryId()+"||"+"Country name: "+country.getCountryName());  
  }  
  
  //Sort by countryName  
  // Anonymous Comparator  
  // old way  
  Collections.sort(listOfCountries,new Comparator<country>() {  
  
   @Override  
   public int compare(Country o1, Country o2) {  
    return o1.getCountryName().compareTo(o2.getCountryName());  
   }  
  });  
    
  // Using lambda expression  
 // Collections.sort(listOfCountries,(o1,o2)-> o1.getCountryName().compareTo(o2.getCountryName()));  
    
  System.out.println("After Sort by name: ");  
  for (int i = 0; i < listOfCountries.size(); i++) {  
   Country country=(Country) listOfCountries.get(i);  
   System.out.println("Country Id: "+country.getCountryId()+"|| "+"Country name: "+country.getCountryName());  
        
   }  
 }  
  
}

Open in new window


even after commneting Lamda  line 43 still i have same compilation errors in line 34 and 37. How to resolve them. Please advise
The word "country" in line 34 needs to be capitalized.

If you're not seeing Java 1.8 in the dropdown in Eclipse, your version of Eclipse is probably too old.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

The word "country" in line 34 needs to be capitalized.

i corrected that. I will try on latest eclipse mars which i installed on other machine
Avatar of gudii9

ASKER

do i need jdk 8 also installed separately apart from eclipse mars. Or eclipse mars has default jdk 1.8 bundled with it?
please advise
It would not hurt to install both.