Showing posts with label max. Show all posts
Showing posts with label max. Show all posts

Monday, 5 April 2021

php: Array: max, min: get maximum and minimum value of array

‘max’ and ‘min’ functions takes an array as argument and return maximum and minimum values of array.

 

Syntax

max($array_name);

min($array_name);

 

Example

$max_element = max($arr1);

$min_element = min($arr1);

 

array_max_min_demo.php

#!/usr/bin/php

<?php

    $arr1 = array(22, 31, -5, 77, -98, 18);
    
    $max_element = max($arr1);
    $min_element = min($arr1);

    print_r($arr1);
    echo "\nMaximum Element : $max_element\n";
    echo "\nMinimum Element : $min_element\n";
?>

 

Output

$./array_max_min_demo.php 

Array
(
    [0] => 22
    [1] => 31
    [2] => -5
    [3] => 77
    [4] => -98
    [5] => 18
)

Maximum Element : 77

Minimum Element : -98

 

 

 

 

Previous                                                    Next                                                    Home

Sunday, 4 April 2021

Bean Validation: @Max: Value must be less than or equal to this number

You can apply @Max annotation on a number whose value must be lower or equal to the specified maximum.

 

Example

@Max(58)

private int age;

 

Supported Types

a.   BigDecimal

b.   BigInteger

c.    byte

d.   short

e.   int

f.     long

 

Where can I apply this annotation?

a.   METHOD

b.   FIELD

c.    ANNOTATION_TYPE

d.   CONSTRUCTOR

e.   PARAMETER

f.     TYPE_USE

 

Find the below working application.

 

Employee.java

package com.sample.app.model;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

public class Employee {

	private int id;

	private String name;

	@Max(58)
	@Min(18)
	private int age;

	public Employee(int id, String name, int age) {
		this.id = id;
		this.name = name;
		this.age = age;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

}

 

Test.java

package com.sample.app;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import com.sample.app.model.Employee;

public class Test {
	private static ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
	private static Validator validator = validatorFactory.getValidator();

	private static void validateBean(Employee emp) {
		System.out.println("************************************");
		Set<ConstraintViolation<Employee>> validationErrors = validator.validate(emp);

		if (validationErrors.size() == 0) {
			System.out.println("No validation errors....");
		}

		for (ConstraintViolation<Employee> violation : validationErrors) {
			System.out.println(violation.getPropertyPath() + "," + violation.getMessage());
		}
		System.out.println("");
	}

	public static void main(String args[]) {

		Employee emp1 = new Employee(1, "Krishna", 28);
		System.out.println("Validation errors on bean emp1");
		validateBean(emp1);

		Employee emp2 = new Employee(2, "Siva", 59);
		System.out.println("Validation errors on bean emp2");
		validateBean(emp2);
		
		Employee emp3 = new Employee(3, "Sailaja", 9);
		System.out.println("Validation errors on bean emp3");
		validateBean(emp3);
	}
}

Output

Validation errors on bean emp1
************************************
No validation errors....

Validation errors on bean emp2
************************************
age,must be less than or equal to 58

Validation errors on bean emp3
************************************
age,must be greater than or equal to 18




 

 

  

Previous                                                    Next                                                    Home

Tuesday, 31 March 2020

TableSaw: max: Find maximum value in a column

‘max’ function returns the maximum value in a column.

App.java
package com.sample.app;

import tech.tablesaw.api.IntColumn;

public class App {

 public static void main(String args[]) {
  int[] numbers = { 23, 45, 6, 32, 1, 43 };
  
  IntColumn numbersColumn = IntColumn.create("My Numbers", numbers);
  double maximumValue = numbersColumn.max();
  

  System.out.println("maximumValue : " + maximumValue);
 }
}

Output
maximumValue : 45.0

Previous                                                    Next                                                    Home

Thursday, 26 March 2020

Streams: max(): Get maximum value

Stream interface provides 'max' method, it takes a comparator and return the maximum element of this stream according to the provided comparator.

Signature
Optional<T> max(Comparator<? super T> comparator);

Example
Integer highestNumber = numbers.stream().max(Integer::compare).get();

App.java
package com.sample.app;

import java.util.Arrays;
import java.util.List;

public class App {

 public static void main(String args[]) {
  List<Integer> numbers = Arrays.asList(11, 3, 43, 23, 213, 32, 54, 67, 89);

  Integer highestNumber = numbers.stream().max(Integer::compare).get();

  System.out.println("Highest Number : " + highestNumber);

 }

}

Output
Highest Number : 213

Let’s try to get the student with highest age and student with last name using max() function.

Let's write name and age comparator like below.
Comparator<Student> nameComparator = new Comparator<Student>() {

 @Override
 public int compare(Student s1, Student s2) {
  return s1.getName().compareTo(s2.getName());
 }

};

Comparator<Student> ageComparator = new Comparator<Student>() {

 @Override
 public int compare(Student s1, Student s2) {
  int x = s1.getAge();
  int y = s2.getAge();

  return (x < y) ? -1 : ((x == y) ? 0 : 1);

 }

};

How to get the student with name is at last (Descending Order)?
Student studentWithLastName = students.stream().max(nameComparator).get();
                 
How to get the student with highest age?
Student studentWithHighestAge = students.stream().max(ageComparator).get();

Find the below working application.

Student.java
package com.sample.app.model;

public class Student {
 private int id;
 private String name;
 private int age;

 public Student(int id, String name, int age) {
  this.id = id;
  this.name = name;
  this.age = age;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

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

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Student [id=");
  builder.append(id);
  builder.append(", name=");
  builder.append(name);
  builder.append(", age=");
  builder.append(age);
  builder.append("]");
  return builder.toString();
 }

}

App.java
package com.sample.app;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import com.sample.app.model.Student;

public class App {

 public static void main(String args[]) {
  Student s1 = new Student(1, "Ram", 22);
  Student s2 = new Student(2, "Joel", 18);
  Student s3 = new Student(3, "Gopi", 17);
  Student s4 = new Student(4, "Bhargavi", 23);
  Student s5 = new Student(5, "Gowthami", 20);
  Student s6 = new Student(6, "Jhansi", 25);

  List<Student> students = Arrays.asList(s1, s2, s3, s4, s5, s6);

  Comparator<Student> nameComparator = new Comparator<Student>() {

   @Override
   public int compare(Student s1, Student s2) {
    return s1.getName().compareTo(s2.getName());
   }

  };

  Comparator<Student> ageComparator = new Comparator<Student>() {

   @Override
   public int compare(Student s1, Student s2) {
    int x = s1.getAge();
    int y = s2.getAge();

    return (x < y) ? -1 : ((x == y) ? 0 : 1);

   }

  };

  Student studentWithLastName = students.stream().max(nameComparator).get();
  Student studentWithHighestAge = students.stream().max(ageComparator).get();

  System.out.println("studentWithLastName : " + studentWithLastName);
  System.out.println("studentWithHighestAge : " + studentWithHighestAge);
 }

}

Output
studentWithLastName : Student [id=1, name=Ram, age=22]
studentWithHighestAge : Student [id=6, name=Jhansi, age=25]



Previous                                                    Next                                                    Home