

{"id":12707,"date":"2018-04-07T16:57:06","date_gmt":"2018-04-07T16:57:06","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=12707"},"modified":"2026-05-28T16:02:16","modified_gmt":"2026-05-28T10:32:16","slug":"pair-class-in-java","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/pair-class-in-java\/","title":{"rendered":"Explore Pair Class in Java With Examples"},"content":{"rendered":"<p>Pair classes &#8211;\u00a0 Well seems like if someone really wanted to have a class called pair, they would have created it themselves.<\/p>\n<p>True, but there is more to it than what the eye sees. Java didn\u2019t have a pair class until Java 7. JavaFX had a util package, which now contains the pair class.<\/p>\n<p>The pair class allows the programmer to store data in the form of pairs. Well, you could have done that easily by declaring a class yourself. The pair class comes with more than that out of the box. Let us dive in!<\/p>\n<h3>What is Pair Class in Java?<\/h3>\n<p>The Pair class in Java is under the util package of the JavaFX package. However, the value-value pairs have to be stored with the help of a parameterized constructor,<\/p>\n<p>This class stores two objects that are connected. It is used when a method needs to retrieve two objects in the form of a pair.<\/p>\n<p>Let us see what it\u2019s like to use it.<\/p>\n<h3>Pair Class Methods in Java<\/h3>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Methods-of-pair-class-in-java.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-84895\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Methods-of-pair-class-in-java.jpg\" alt=\"Methods of pair class in java\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Methods-of-pair-class-in-java.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Methods-of-pair-class-in-java-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Methods-of-pair-class-in-java-1024x536.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Methods-of-pair-class-in-java-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Methods-of-pair-class-in-java-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Methods-of-pair-class-in-java-720x377.jpg 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Methods-of-pair-class-in-java-520x272.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Methods-of-pair-class-in-java-320x167.jpg 320w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><\/p>\n<p>The Pair class has the following methods to support extensive usage.<\/p>\n<ul>\n<li><strong>Pair(Value1, Value2) &#8211;<\/strong> This constructor assigns two values to the Pair class. Simple. Direct. This method takes two arguments containing the values of two objects.<\/li>\n<li><strong>equals(Pairobject1, PairObject2) &#8211;<\/strong> This returns a boolean value based on the value of the two pairs. Do note that the comparison checks the values of both the pairs. This is also popularly known as Deep comparison.<\/li>\n<li><strong>getKey() &#8211;<\/strong> As the name suggests, the method returns the key of the pair. This is generally the first value of the pair. It is of the same type as the datatype of the Key.<\/li>\n<li><strong>getValue() &#8211;<\/strong> As the name suggests, the method returns the value of the pair. This is generally the second value of the pair. It is of the same data type as that of the Value of the pair.<\/li>\n<li><strong>toString() &#8211;<\/strong> This method will return the String representation of the Pair.<\/li>\n<li><strong>hashcode() &#8211;<\/strong> This method is responsible for returning the hashcode of the pair.<\/li>\n<\/ul>\n<h3>Need of Pair Class in Java<\/h3>\n<p>We need a \u201cpair class\u201d for a lot of reasons.<\/p>\n<p>One primary example would be when we want to return multiple values. I know some of you might be thinking about arrays and other data structures.<\/p>\n<p>But if you are using a cluster of variables together then returning both of them together will be a hassle.<\/p>\n<p>Then the pair class would be useful.<\/p>\n<p>If you want to perform some mathematical calculations on a number and then return the number along with the result, you would generally be able to do it with a pair class very easily.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p>In Java, declaring Pair class objects are similar to normal object creation. Its syntax is as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Pair&lt;Wrapper Class1, Wrapper Class2&gt;object_name = new Pair&lt;&gt;();\r\n<\/pre>\n<h3>Types of Pair Class in Java<\/h3>\n<p>&nbsp;<\/p>\n<p>There are primarily two types of pair classes in Java:<\/p>\n<p><strong>1. Immutable Pair Class &#8211;<\/strong> This is a type of class where you cannot use setter methods to change the values of the pair according to your choice. When the class is created, it cannot be modified later on.<\/p>\n<p>Generally, when we set the values of an immutable pair, we cannot change it again.<\/p>\n<p><strong>2. Mutable Pair Class &#8211;<\/strong> This type of class allows you to change the values even after you set them at the beginning. You can make changes even after the creation of the class.<\/p>\n<p>Simply speaking, this class allows you to use getter and setter methods such as pair.setValue(a,b) and pair.getValue() methods.<\/p>\n<h3>Defining a Custom Pair Class in Java<\/h3>\n<p>We can explicitly define our own pair class in Java. We will see how it goes.<\/p>\n<p><strong>Java program to illustrate the use of a user-defined Pair class:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.dataflair.pairclass;\r\nclass Pair\r\n{\r\n    int value1;\r\n    int value2;\r\n    Pair(int a, int b)\r\n    {\r\n        value1=a;\r\n        value2=b;\r\n        \r\n    }\r\n    \r\n}\r\npublic class Main\r\n{\r\n    public static void main(String[] args) {\r\n        \/\/Here is an example of how you can set up your own pair class in java.\r\n        Pair b = new Pair(3,4);\r\n        \/\/add methods to the class as you like.\r\n        \/\/You can also make it generic in nature!\r\n        System.out.println(\"Example of custom pair class in Java.\");\r\n        System.out.println(\"The Pair is &lt;\"+b.value1+\",\"+b.value2+\"&gt;\");\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Example of custom pair class in Java.<br \/>\nThe Pair is &lt;3,4&gt;<\/div>\n<p>We can create our own class based on our requirements anyway.<\/p>\n<p>But Java already has a predefined class to suit our needs.<\/p>\n<p><strong>Java program to illustrate the use of the Pair class:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.dataflair.pairclass;\r\n\r\nimport javafx.util.Pair;\r\nimport java.util.*;\r\n\r\npublic class Main\r\n{\r\n    \r\n    public static void main(String[] args) {\r\n        Pair&lt;String,String&gt;p1=new Pair&lt;&gt;(\"WINDOWS\",\"it's GUI\");\r\n        Pair&lt;String,String&gt;p2=new Pair&lt;&gt;(\"LINUX\",\"it is open source and secure\");\r\n        Pair&lt;String,String&gt;p3=new Pair&lt;&gt;(\"MAC OS\",\"its productivity\");\r\n        ArrayList&lt;Pair&lt;String,String&gt;&gt; listofpairs = new ArrayList&lt;&gt;();\r\n        listofpairs.add(p1);\r\n        listofpairs.add(p2);\r\n        listofpairs.add(p3);\r\n        for(Pair&lt;String,String&gt; p:listofpairs)\r\n        {\r\n            System.out.println(p.getKey()+\" is famous for \"+p.getValue());\r\n        }\r\n        \r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">WINDOWS is famous for it&#8217;s GUI<br \/>\nLINUX is famous for it is open source and secure<br \/>\nMAC OS is famous for its productivity<\/div>\n<h3>Functions with Pair Class<\/h3>\n<p>We can also use the Pair class with functions in a program. Since these are objects, we can also return pair objects from functions.<\/p>\n<p><strong>Let us see an example:<\/strong><\/p>\n<p><strong>Java program to illustrate the use of Pair classes in a function:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.dataflair.pairclass;\r\n\r\nimport javafx.util.Pair;\r\nimport java.util.*;\r\n\r\npublic class Main\r\n{\r\n    public static Pair pairfunc(String firstname,String lastname)\r\n    \r\n    {\r\n        Pair&lt;String,String&gt; name = new Pair&lt;&gt;(firstname,lastname);\r\n        \r\n        return name;\r\n    \r\n    }\r\n    public static void main (String[] args) {\r\n        Scanner sc = new Scanner(System.in);\r\n        System.out.println(\"Enter your first name\");\r\n        String fname=sc.nextLine();\r\n        System.out.println(\"Enter your last name\");\r\n        String lname=sc.nextLine();\r\n        Pair result=pairfunc(fname,lname);\r\n        System.out.println(\"Hi there \"+ result.getKey()+\" \"+result.getValue());\r\n        \r\n        \r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Enter your first name<br \/>\nShraman<br \/>\nEnter your last name<br \/>\nDas<br \/>\nHi there Shraman Das<\/div>\n<h3>Summary<\/h3>\n<p>We learnt a lot about the usage of the Pair class in Java and how it can be leveraged to suit our needs in programming.<\/p>\n<p>It allows us to store two values together inside a single reference object. It also provides several other methods which come in handy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Pair classes &#8211;\u00a0 Well seems like if someone really wanted to have a class called pair, they would have created it themselves. True, but there is more to it than what the eye sees.&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":84894,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[3875,6368,6611,7362,7628,7630,7725,9388,12884,14502],"class_list":["post-12707","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-difference-between-pair-and-map-in-java","tag-how-to-use-pair-in-java","tag-import-pair-java","tag-java-8-pair-class","tag-java-pair-api","tag-java-pair-class-example","tag-java-tuple-class","tag-pair-class-in-java","tag-simple-pair-in-java","tag-techniques-given-by-the-javafx-util-pair-class"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Explore Pair Class in Java With Examples - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn What is Pair Class in Java and its need with example, Types of Pair Class in Java and how to use pair class in java,\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/pair-class-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Explore Pair Class in Java With Examples - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn What is Pair Class in Java and its need with example, Types of Pair Class in Java and how to use pair class in java,\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/pair-class-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-04-07T16:57:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-28T10:32:16+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Pair-Class-in-Java.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Explore Pair Class in Java With Examples - DataFlair","description":"Learn What is Pair Class in Java and its need with example, Types of Pair Class in Java and how to use pair class in java,","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/pair-class-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Explore Pair Class in Java With Examples - DataFlair","og_description":"Learn What is Pair Class in Java and its need with example, Types of Pair Class in Java and how to use pair class in java,","og_url":"https:\/\/data-flair.training\/blogs\/pair-class-in-java\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-04-07T16:57:06+00:00","article_modified_time":"2026-05-28T10:32:16+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Pair-Class-in-Java.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/pair-class-in-java\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/pair-class-in-java\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Explore Pair Class in Java With Examples","datePublished":"2018-04-07T16:57:06+00:00","dateModified":"2026-05-28T10:32:16+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/pair-class-in-java\/"},"wordCount":809,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/pair-class-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Pair-Class-in-Java.jpg","keywords":["difference between pair and map in java","how to use pair in java","import pair java","java 8 pair class","java pair api","Java Pair Class Example","java tuple class","pair class in java","simple pair in java","Techniques given by the javafx.util.Pair Class"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/pair-class-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/pair-class-in-java\/","url":"https:\/\/data-flair.training\/blogs\/pair-class-in-java\/","name":"Explore Pair Class in Java With Examples - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/pair-class-in-java\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/pair-class-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Pair-Class-in-Java.jpg","datePublished":"2018-04-07T16:57:06+00:00","dateModified":"2026-05-28T10:32:16+00:00","description":"Learn What is Pair Class in Java and its need with example, Types of Pair Class in Java and how to use pair class in java,","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/pair-class-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/pair-class-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/pair-class-in-java\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Pair-Class-in-Java.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Pair-Class-in-Java.jpg","width":1200,"height":628,"caption":"Pair Class in Java"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/pair-class-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Java Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/java\/"},{"@type":"ListItem","position":3,"name":"Explore Pair Class in Java With Examples"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/12707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=12707"}],"version-history":[{"count":8,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/12707\/revisions"}],"predecessor-version":[{"id":148475,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/12707\/revisions\/148475"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/84894"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=12707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=12707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=12707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}