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.
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CPColinSenior Java ArchitectCommented:
That looks like your compiler isn't the one that comes with Java 8 or is trying to target an earlier version of Java.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gudii9Author Commented:
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
0
gudii9Author Commented:
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
0
Increase Security & Decrease Risk with NSPM Tools

Analyst firm, Enterprise Management Associates (EMA) reveals significant benefits to enterprises when using Network Security Policy Management (NSPM) solutions, while organizations without, experienced issues including non standard security policies and failed cloud migrations

CPColinSenior Java ArchitectCommented:
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.
0
gurpsbassiCommented:
Have you even installed Java 8?
0
srikoteshCommented:
install eclipse luna if u already installed java 8 in ur machine
0
gudii9Author Commented:
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
0
gudii9Author Commented:
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
0
CPColinSenior Java ArchitectCommented:
It would not hurt to install both.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.