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...







Variables Of Java Class part-2

By // 2 comments:

In the previous post, talked about how many elements of a java class & local variable. Now we learn about Instance variable.



2. Instance Variable
  • Declaration:- The variables which are declared inside the class but outside of the method that are called instance variable.
  • Scope:- The variable can access inside the class. All methods, constructors and blocks of the class also able to access the variables.
  • Memory Allocation:- Memory is allocated in object creation time. Memory released when object is destroyed.
  • Stored Memory:- Heap is a memory  place where the objects, and its instance variable are stored.

3. Static Variable
  • Declaration:- The variables which are declared inside the class and outside of the method with static modifier are called static variable.
  • Memory Allocation:- When .class file is loaded the memory allocated.
  • Scope:- The static variable can access within the class. Access by class name.
  • Stored Area:- The variable stored in non-heap memory.
    To set up java environment visit here.
Next post is coming soon.......

Variables Of Java Class part-1

By // 5 comments:

In the first tutorial, I discussed that, a java class have five elements. Variable is one of these. Now we discuss about variable. There are three types of variables. They are..



  1. Local Variable
  • Declaration:- Declared inside the method or constructor or blocks.
  • Scope:- We can access the local variable only within the method, within the constructor or within the block. Out side is not possible.
  • Memory allocation:- When method or constructor of blocks start memory are allocated. When method invoked memory allocated and memory released when method completed.
  • Stored Area:- Local variable are stored in stack memory
        Next post is coming soon.......

Basics of Java

By // 2 comments:

Java is a programming language. Based on the concept of Object Oriented Programming(OOP). It is platform independent language (Write Once and Run Anywhere). This technology is used to develop applications for a wide range of environments, from client devices to multifarious enterprise systems. It is class based language. The class contains five elements. These are.. 

1. Variables 
2. Methods
3. Constructors
4. Instance blocks 
5. Static blocks

Details are coming soon....