{"id":77563,"date":"2020-04-04T14:19:55","date_gmt":"2020-04-04T08:49:55","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=77563"},"modified":"2020-04-04T14:19:55","modified_gmt":"2020-04-04T08:49:55","slug":"java-pair-class","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-pair-class\/","title":{"rendered":"Java Pair Class &#8211; Learn to Implement them in Java"},"content":{"rendered":"<p>In this Java tutorial, we will learn another highly useful programming concept that is pair class in Java. The Pair class in Java was introduced since Java 8.<\/p>\n<p>We will learn how to use and implement a javafx.util.pair class in programs. We will also study various methods provided by a javafx.util.pair class.<\/p>\n<p>At last, we will discuss the Java Pair Example. The javafx.util.Pair in Java is a class that stores a pair. We use the parameterized constructor of the javafx.util.Pair class, to store the values into Pair.<\/p>\n<h3>Pair Class in Java<\/h3>\n<p>A pair is a container that provides a convenient way to store a simple key to value. It stores a tuple of two objects. In Java, Maps.Entry is an excellent example that stores key-value pairs. Maps store a collection of pairs and treat them as a single unit.<\/p>\n<p>The Pair class does not specify the relationship between the specified values. In C++, std::pair is similar to pair class in Java. In C++, the first and second fields can be anything.<\/p>\n<p><strong>The implementation of Pair class contains the following components:<\/strong><\/p>\n<ol>\n<li>Two public fields: first and second, just like in C++.<\/li>\n<li>A private constructor: takes two arguments &#8211; a key and, its corresponding value.<\/li>\n<li>Method of(): A static factory method for creating an immutable, Typed Pair instance.<\/li>\n<li>Overridden methods hashCode() and equals(): These methods are used to ensure the desired behavior in hash-based collections.<\/li>\n<li>Overridden toString() method: This method prints the Pair object in String form.<\/li>\n<\/ol>\n<p><strong>Syntax of Constructor of the pair class:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Pair&lt;Integer, String&gt; pair = new Pair&lt;&gt;(3, \"Three\");\nInteger key = pair.getKey();\nString value = pair.getValue();<\/pre>\n<h4>Need for Pair Class in Java<\/h4>\n<p>We use Pairs or Pair class in Java to return two values from a method. For example, there is a method that returns both the cube root of the number and the number itself. Therefore, we need to merge a number with its cube root in a pair.<\/p>\n<p>This pair or combination may result in (number, cube root of a number). For example, (125, 5) and (216, 6).<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/example-of-java-pair-class.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78000\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/example-of-java-pair-class.jpg\" alt=\"\" width=\"416\" height=\"286\" \/><\/a><\/p>\n<h4>Types of Pair Class in Java<\/h4>\n<h5>Mutable Pair<\/h5>\n<p>Mutable pairs are the pairs for which we can change their data values. Through this pointer, we can call their getter and setter methods.<\/p>\n<p>These methods are pair.getValue(); and pair.setValue (3, 4). The function setValue() sets the values in the pair, and getValue() returns the values from the pairs.<\/p>\n<h5>Immutable Pair<\/h5>\n<p>Immutable pairs are the pairs for which we can\u2019t change or set the data values. Once, we set the data values in the pair, then we cannot call the pair.setValue (3, 4) method gain to change the values.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.techvidvan.pairclass;\npublic class Pair&lt;T&gt;\n{\n  T p1, p2;\n\n  void setValue(T a, T b)\n  {\n    this.p1 = a;\n    this.p2 = b;\n  }\n\n  Pair&lt;T&gt; getValue()\n  {\n    return this;\n  }\n  public static void main(String args[ ])\n  {\n    Pair&lt;Integer&gt; pair = new Pair&lt;Integer&gt; ( );\n    pair.setValue(1,2);\n\n    Pair &lt;Integer&gt; answer= new Pair &lt;Integer&gt;( );\n    answer = pair.getValue();\n\n    System.out.println(answer.p1 + \" \" + answer.p2);\n  }\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">1 2<\/div>\n<h4>Methods of javafx.util.Pair Class<\/h4>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/methods-of-java-pair-class.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78001\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/methods-of-java-pair-class.jpg\" alt=\"\" width=\"802\" height=\"420\" \/><\/a><\/p>\n<h5>1. boolean equals()<\/h5>\n<p>This method compares two pair objects on the basis of the (&lt;Key, Value&gt;) of the pair objects.<\/p>\n<p><strong>Example to understand the equals() method of Pair class:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Pair p1 = new Pair(3,4);\nPair p2 = new Pair(3,4);\nPair p3 = new Pair(4,4);\nSystem.out.println(p1.equals(p2));\t \/\/prints true\nSystem.out.println(p2.equals(p3));\t \/\/prints false<\/pre>\n<h5>2. String toString()<\/h5>\n<p>It returns the String representation of the Pair object.<\/p>\n<h5>3. K getKey()<\/h5>\n<p>It returns the key for the pair.<\/p>\n<h5>4. V getValue()<\/h5>\n<p>It returns the value for the pair.<\/p>\n<h5>5. int hashCode()<\/h5>\n<p>hashCode() method generates a hash code for the Pair.<\/p>\n<h4>Implementation of the javafx.util.Pair Class<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.techvidvan.pairclass;\nimport java.util.ArrayList;\npublic class Pair&lt;T1, T2&gt;\n{\n  private String string;\n  private int i;\n\n  public Pair(String string, int i)\n  {\n    this.string=string;\n    this.i=i;\n  }\n\n  public static Pair &lt;String,Integer&gt; getMaximum(ArrayList &lt; Pair \t&lt;String,Integer&gt; &gt; arrayList)\n  {\n    int max = Integer.MIN_VALUE;\n    Pair &lt;String, Integer&gt; ans = new Pair &lt;String, Integer&gt; (\" \", 0);\n\n    for (Pair &lt;String,Integer&gt; temp : l)\n    {\n      int val = temp.getValue();\n      if (val &gt; max)\n      {\n        max = val; \/\/ update maximum\n        ans = temp; \/\/ update the Pair\n      }\n    }\n    return ans;\n  }\n\n  private int getValue()\n  {\n    return i;\n  }\n  private String getKey()\n  {\n    return string;\n  }\n\n  public static void main (String[] args)\n  {\n    \/\/Create an Array List\n    ArrayList &lt;Pair &lt;String, Integer&gt;&gt; arrayList = new ArrayList &lt;&gt; ( );\n    arrayList.add(new Pair &lt;String,Integer&gt; (\"Cricketer A\", 76));\n    arrayList.add(new Pair &lt;String,Integer&gt; (\"Cricketer B\", 97));\n    arrayList.add(new Pair &lt;String,Integer&gt; (\"Cricketer C\", 45));\n    arrayList.add(new Pair &lt;String,Integer&gt; (\"Cricketer D\", 65));\n    arrayList.add(new Pair &lt;String,Integer&gt; (\u201cCricketer E\", 110));\n\n    Pair &lt;String,Integer&gt; answer = getMaximum(arrayList);\n    System.out.println(answer.getKey() + \" is the man of the match \" + \"with a number of runs: \" + answer.getValue());\n  }\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Cricketer E is the man of the match with the number of runs: 110<\/div>\n<h3>Summary<\/h3>\n<p>Here we come to the end of our tutorial. In this article, we learned about the pair class in Java which is used for storing key-value pairs. We got to know the importance of the javafx.util.pair class along with its various types and methods associated with it.<\/p>\n<p>At last, we explored how to implement pair class in Java taking real-life examples for your better understanding.<\/p>\n<p>Thank you for reading our article. Do share it on Social Media.<\/p>\n<p>Happy Learning \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this Java tutorial, we will learn another highly useful programming concept that is pair class in Java. The Pair class in Java was introduced since Java 8. We will learn how to use&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":78001,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[2197,2198,2199,2200,2201,2202,2203,2204],"class_list":["post-77563","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-implementation-of-java-pair-class","tag-java-pair-class","tag-java-pair-class-example","tag-java-pair-class-implementation","tag-java-pair-class-need","tag-need-for-java-pair-class","tag-pair-class-in-java","tag-what-is-pair-class-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java Pair Class - Learn to Implement them in Java - TechVidvan<\/title>\n<meta name=\"description\" content=\"Get to know in detail about Java Pair Class with its importance, types, methods &amp; its real-life examples. Also, explore how to implement 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:\/\/techvidvan.com\/tutorials\/java-pair-class\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Pair Class - Learn to Implement them in Java - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Get to know in detail about Java Pair Class with its importance, types, methods &amp; its real-life examples. Also, explore how to implement pair class in Java.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-pair-class\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-04-04T08:49:55+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/methods-of-java-pair-class.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Pair Class - Learn to Implement them in Java - TechVidvan","description":"Get to know in detail about Java Pair Class with its importance, types, methods & its real-life examples. Also, explore how to implement 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:\/\/techvidvan.com\/tutorials\/java-pair-class\/","og_locale":"en_US","og_type":"article","og_title":"Java Pair Class - Learn to Implement them in Java - TechVidvan","og_description":"Get to know in detail about Java Pair Class with its importance, types, methods & its real-life examples. Also, explore how to implement pair class in Java.","og_url":"https:\/\/techvidvan.com\/tutorials\/java-pair-class\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-04-04T08:49:55+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/methods-of-java-pair-class.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/java-pair-class\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-pair-class\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Java Pair Class &#8211; Learn to Implement them in Java","datePublished":"2020-04-04T08:49:55+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-pair-class\/"},"wordCount":619,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-pair-class\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/methods-of-java-pair-class.jpg","keywords":["Implementation of Java Pair Class","java pair class","Java Pair Class Example","Java Pair Class Implementation","Java Pair Class Need","Need For Java Pair Class","pair class in java","What is Pair class in java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/java-pair-class\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-pair-class\/","url":"https:\/\/techvidvan.com\/tutorials\/java-pair-class\/","name":"Java Pair Class - Learn to Implement them in Java - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-pair-class\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-pair-class\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/methods-of-java-pair-class.jpg","datePublished":"2020-04-04T08:49:55+00:00","description":"Get to know in detail about Java Pair Class with its importance, types, methods & its real-life examples. Also, explore how to implement pair class in Java.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-pair-class\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-pair-class\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-pair-class\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/methods-of-java-pair-class.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/methods-of-java-pair-class.jpg","width":802,"height":420,"caption":"java pair class methods"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-pair-class\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java Pair Class &#8211; Learn to Implement them in Java"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/77563","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=77563"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/77563\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/78001"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=77563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=77563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=77563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}