JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Method Reference - Reference to a Instance Method example.


This example shows how to use Method Reference feature on instance method:

package com.java2novice.methodreference;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.java2novice.lambda.Employee;

public class InstanceMethodReferenceEx {

	public static void main(String a[]) {

		List<Employee> empList = new ArrayList<>();
		empList.add(new Employee("Nataraja G", "Accounts", 8000));
		empList.add(new Employee("Nagesh Y", "Admin", 15000));
		empList.add(new Employee("Vasu V", "Security", 2500));
		empList.add(new Employee("Amar", "Entertinment", 8500));

		MyExampComparator comp = new MyExampComparator();

		// sort it in method reference way
		System.out.println("<--- Sorted list with Method Reference --->");
		Collections.sort(empList, comp::compare);
		empList.forEach(System.out::println);

		// sort it in lambda expression way
		System.out.println("\n\n<--- Sorted list with Lambda expression --->");
		Collections.sort(empList, (emp1, emp2) -> comp.compare(emp1, emp2));
		empList.forEach(System.out::println);
	}
}

Here is MyExampComparator class:

package com.java2novice.methodreference;

import com.java2novice.lambda.Employee;

public class MyExampComparator {

	public int compare(Employee emp1, Employee emp2) {

		return emp1.getSalary().compareTo(emp2.getSalary());
	}
}

Here is Employee POJO class:

package com.java2novice.lambda;

public class Employee {

	private String name;
	private String account;
	private Integer salary;

	public Employee(String name, String account, Integer salary) {
		super();
		this.name = name;
		this.account = account;
		this.salary = salary;
	}

	@Override
	public String toString() {

		return "name: "+ this.name +" | account: "+ this.account +" | salary: "+this.salary;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public Integer getSalary() {
		return salary;
	}

	public void setSalary(Integer salary) {
		this.salary = salary;
	}
}

Output:
<--- Sorted list with Method Reference --->
name: Vasu V | account: Security | salary: 2500
name: Nataraja G | account: Accounts | salary: 8000
name: Amar | account: Entertinment | salary: 8500
name: Nagesh Y | account: Admin | salary: 15000


<--- Sorted list with Lambda expression --->
name: Vasu V | account: Security | salary: 2500
name: Nataraja G | account: Accounts | salary: 8000
name: Amar | account: Entertinment | salary: 8500
name: Nagesh Y | account: Admin | salary: 15000
<< Previous Program | Next Program >>

Java-8 Method References Examples

  1. Method Reference - Reference to a Static Method example.
  2. Method Reference - Reference to a Instance Method example.
  3. Method Reference - Reference to a Constructor example.
Knowledge Centre
doPost Vs doGet methods
doGet() method is used to get information, while doPost() method is used for posting information. doGet() requests can't send large amount of information and is limited to 240-255 characters. However, doPost()requests passes all of its data, of unlimited length. A doGet() request is appended to the request URL in a query string and this allows the exchange is visible to the client, whereas a doPost() request passes directly over the socket connection as part of its HTTP request body and the exchange are invisible to the client.
Famous Quotations
Never argue with a fool, onlookers may not be able to tell the difference.
-- Mark Twain

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.