gudii9
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
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>.compar e
(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.
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());
}
}
}
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;
}
}
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>.compar
(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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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());
}
}
}
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.
If you're not seeing Java 1.8 in the dropdown in Eclipse, your version of Eclipse is probably too old.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
please advise
It would not hurt to install both.
ASKER