Sort a Java List with Specific Fields

By // No comments:
In this tutorial, we will discuss about sorting a list by specific field/property. Often, we need to sort a list with its specific field. This is very easy and short code in java 8. Let's go for details...

Example:

1:  package com.javabd1.blogspot.controller;  
2:  import java.util.ArrayList;  
3:  import java.util.HashMap;  
4:  import java.util.List;  
5:  import java.util.Map;  
6:  import org.springframework.beans.factory.annotation.Autowired;  
7:  import org.springframework.stereotype.Controller;  
8:  import org.springframework.validation.BindingResult;  
9:  import org.springframework.web.bind.annotation.ModelAttribute;  
10:  import org.springframework.web.bind.annotation.RequestMapping;  
11:  import org.springframework.web.bind.annotation.RequestMethod;  
12:  import org.springframework.web.servlet.ModelAndView;  
13:  /**  
14:   * @author Ferdous Islam  
15:   *  
16:   */  
17:  @Controller  
18:  public class EmployeeController {  
19:   @Autowired  
20:   private EmployeeService employeeService;  
21:   @RequestMapping(value="/employees", method = RequestMethod.GET)  
22:   public ModelAndView listEmployees() {  
23:   Map<String, Object> model = new HashMap<String, Object>();  
24:   List<Employee> empList=employeeService.listEmployeess();  
25:   empList.sort(Comparator.comparing(Employee::getName()));  
26:    model.put("employees", empList );  
27:    return new ModelAndView("employeesList", model);  
28:   }  
29:  }
Note: Here, the list sorted with name field, you may sort it with any other field.


This may Work Fine!!

Visit this blog for more solution of various problem...


Methods of Java Class Part-1

By // No comments:

In the previous tutorial, discussed five elements of a java class. Method is one of them. Today we will learn about method of java class. As directly writing business logic are not allowed in java class. So we have to write business logic in a method. Method is used to write the logic of the program. Let's go for details of the method.

1. Method Signature : A method signature is the method name and the number and type of its parameters. 

Example:-  methodName(parameters);
calculate(int a, double b);

Note: In the above example calculate is the method name and int, double are the type of parameter a and b.



2. Defining Methods : Syntax for a method definition.

access optionalStatic returnType methodNameparameters ){
declarations;
statements;
return expression;
}

Explanation: 
  • Here, access is one of public, protected, package(default: no keyword used)or private, to indicate what other code may use the method. 
  • optionalStatic is the keyword static for static methods, omitted for non-static methods.
  • returnType is the type of value returned by the method, if any. Methods may return any valid type, or void if no type is to be returned. 
  • methodName is the name that you can choose but related to functionality is the best. 
  • parameters describe what information must be given to the method. Each parameter consists a type and a variable.
  • The declarations and statements are just ordinary Java code. The statements may use any instance variables or static variables of the class, the parameters, or any variables declared within the method. 
  • If the method is declared to return a value, the expression in the return statement must be a value of the correct type. 
There are two parts of a method. One is Method Header and another is Method body. 

Visit this site for next tutorial...