tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Spring > Expression Language > Map Last Selection

Map Last Selection 

The Spring Expression Language (SpEL) is a simple and powerful expression language which helps to query and manipulate objects at runtime. The following example shows using last matching selection operator ($[selectionExpression]). It filters and returns a new map with last element matching given selection criteria.

File Name  :  
com/bethecoder/tutorials/spring3/tests/spel/MapLastSelectionTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.spring3.tests.spel;

import java.util.HashMap;
import java.util.Map;

import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import com.bethecoder.tutorials.spring3.basic.Employee;

public class MapLastSelectionTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    StandardEvaluationContext stdContext = new StandardEvaluationContext();
    Map<Integer, Employee> employees = new HashMap<Integer, Employee>();
    employees.put(1new Employee(1"ONE"1111));
    employees.put(2new Employee(2"TWO"2222));
    employees.put(3new Employee(3"THREE"3333));
    employees.put(4new Employee(4"FOUR"4444));
    employees.put(5new Employee(5"FIVE"5555));
    employees.put(6new Employee(6"SIX"6666));
    
    stdContext.setVariable("empMap", employees);
    
    /**
     * Last matching Employee having salary greater than 4000
     */
    ExpressionParser parser = new SpelExpressionParser();
    Map<Integer, Employee> subSet = (Map<Integer, Employee>parser.parseExpression(
        "#empMap.$[value.salary > 4000]").getValue(stdContext);
    System.out.println(subSet)

    /**
     * Last matching Employee having salary less than 4000
     */
    subSet = (Map<Integer, Employee>parser.parseExpression(
        "#empMap.$[value.salary < 4000]").getValue(stdContext);
    System.out.println(subSet);
  }

}
   

It gives the following output,
{6=Employee[6, SIX, 6666.0]}
{3=Employee[3, THREE, 3333.0]}



 
  


  
bl  br