Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is ObjectUtils.mode in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

mode() is a staticthe methods in Java that can be called without creating an object of the class. method of the ObjectUtils class that is used to find the most frequently occurring item in a given list of items.

In statistics, the mode is the value that occurs most frequently in a set of data values.

Importing ObjectUtils

The definition of ObjectUtils can be found in the Apache Commons Lang package, which we can add to the Maven project by adding the following dependency to the pom.xml file:


<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
</dependency>

You can import the ObjectUtils class as follows:


import org.apache.commons.lang3.ObjectUtils;

Syntax


public static <T> T mode(final T... items)

Parameters

  • final T... items: The list of items.

Return value

This method returns the most frequently occurring item.

Code

import org.apache.commons.lang3.ObjectUtils;
import java.util.Arrays;
public class Main{
public static void main(String[] args) {
Integer[] ints = new Integer[]{1,22,2,1,3,42,21,1,2,1,21,1,1};
System.out.printf("ObjectUtils.mode(%s) = %s", Arrays.toString(ints), ObjectUtils.mode(ints));
}
}
The ObjectUtils.mode() method

In the code above, we define an array of integers. We get the most frequently occurring integer with the help of ObjectUtils.mode().

Output

The output of the code will be as follows:


ObjectUtils.mode([1, 22, 2, 1, 3, 42, 21, 1, 2, 1, 21, 1, 1]) = 1

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring