Using Java to achieve two-time ordering

Source: Internet
Author: User

Two sorting tool classes:

Import Java.io.serializable;import scala.math.ordered;/*** @author author e-mail:* @version creation time: August 30, 2017 PM 3:48:11* Class Description * Two orders Keypublic class Secondeindexsort implements Ordered<secondeindexsort>, Serializable{private static final Long Serialversionuid = -2366006422945129991l;//First inside the custom key, define the column that needs to be sorted private int first;private int second;public Secondeindexsort (int first, int second) {This.first = First;this.second = Second;} @Overridepublic Boolean $greater (Secondeindexsort other) {if (This.first > Other.getfirst ()) {return true;} else if ( This.first = = Other.getfirst () && this.second > Other.getsecond ()) {return true;} return false;} @Overridepublic boolean $greater $eq (Secondeindexsort other) {if (this. $greater (other)) {return true;} else if ( This.first = = Other.getfirst () && This.second = = Other.getsecond ()) {return true;} return false;} @Overridepublic Boolean $less (Secondeindexsort other) {if (This.first < Other.getfirst ()) {return true;} else if ( This.first = = Other.getfirst () && This.second < Other.getsecond ()) {return true;} return false;} @Overridepublic boolean $less $eq (Secondeindexsort other) {if (the. $less (Other)) {return true;} else if (This.first = = Other.getfirst () && This.second = = Other.getsecond ()) {return true;} return false;} @Overridepublic int Compare (Secondeindexsort other) {if (This.first-other.getfirst ()! = 0) {return this.first-other.get First ();} else {return This.second-other.getsecond ();}} @Overridepublic int CompareTo (Secondeindexsort other) {if (This.first-other.getfirst ()! = 0) {return THIS.FIRST-OTHER.G Etfirst ();} else {return This.second-other.getsecond ();}} For multiple columns to be sorted, provide getter and setter methods, as well as hashcode and equals method public int GetFirst () {return first;} public void Setfirst (int first) {This.first = first;} public int Getsecond () {return second;} public void Setsecond (int second) {this.second = second;} @Overridepublic int hashcode () {final int prime = 31;int result = 1;result = Prime * result + first;resUlt = Prime * result + Second;return result;} @Overridepublic boolean equals (Object obj) {if (this = = obj) return true;if (obj = = null) return False;if (GetClass ()! = obj . GetClass ()) return false; Secondeindexsort other = (Secondeindexsort) obj;if (First! = Other.first) return false;if (second! = Other.second) return FA Lse;return true;}}
Import Org.apache.spark.sparkconf;import Org.apache.spark.api.java.javapairrdd;import Org.apache.spark.api.java.javardd;import Org.apache.spark.api.java.javasparkcontext;import Org.apache.spark.api.java.function.function;import Org.apache.spark.api.java.function.pairfunction;import Org.apache.spark.api.java.function.voidfunction;import Scala. tuple2;/*** @author author e-mail:* @version created: August 30, 2017 PM 4:26:10* class Description */public class Sortindexbykey {public static void Main (string[] args) {sparkconf conf = new sparkconf (). Setappname ("Sortaction"). Setmaster ("local"); Javasparkcontext sc = new Javasparkcontext (conf); javardd<string> tf = sc.textfile ("G://122.txt"); Javapairrdd<secondeindexsort, string> Maptopair = Tf.maptopair (new pairfunction<string, SecondeIndexSort, String> () {private static final long serialversionuid = 1L; @Overridepublic tuple2<secondeindexsort, string> Call (String Paramt) throws Exception {string[] lines = Paramt.trim (). Split (""); System.out.println ("KEys ... "+integer.valueof (Lines[0])); System.out.println ("Keys1 ..." +integer.valueof (lines[1])); Secondeindexsort keys = new Secondeindexsort (integer.valueof (Lines[0]), integer.valueof (lines[1])); return new Tuple2 <secondeindexsort, string> (keys, Paramt);}); Javapairrdd<secondeindexsort, string> Sortbykey = Maptopair.sortbykey (); javardd<string> map = sortbykey.map (new Function<tuple2<secondeindexsort,string>, String> () { Private static final long serialversionuid = 1L; @Overridepublic String Call (Tuple2<secondeindexsort, string> paramT1) throws Exception {return paramt1._2;}}); Map.foreach (New voidfunction<string> () {private static final long serialversionuid = 1L; @Overridepublic void Call ( String Paramt) throws Exception {System.out.println ("..." +paramt);}); Sc.close ();}}

  

 

Test data:

1 4
3 4
4 6
6 {
4 18
6 6
5 9
8 9
8 9
4 6

Using Java to achieve two-time ordering

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.