Advertisement
jasonpogi1669

Set of Pairs using Java

Apr 8th, 2022 (edited)
755
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.82 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5.     public static void main(String[] args) {
  6.         TreeSet<Pair> arr = new TreeSet<>();
  7.        
  8.         arr.add(new Pair(89, 9));
  9.         arr.add(new Pair(1, 2));
  10.         arr.add(new Pair(1, 2));
  11.         arr.add(new Pair(1, 2));
  12.         arr.add(new Pair(0, 67));
  13.         arr.add(new Pair(0, 67));
  14.         arr.add(new Pair(0, 67));
  15.         arr.add(new Pair(0, 67));
  16.         arr.add(new Pair(0, 67));
  17.        
  18.         for (Pair p : arr) {
  19.             System.out.println(p.first + " " + p.second);
  20.         }
  21.     }
  22.    
  23.     static class Pair implements Comparable<Pair> {
  24.         int first;
  25.         int second;
  26.        
  27.         Pair(int first, int second) {
  28.             this.first = first;
  29.             this.second = second;
  30.         }
  31.  
  32.         @Override
  33.         public int compareTo(Pair other) {
  34.             if (this.first == other.first && this.second == other.second) {
  35.                 return 0;
  36.             }
  37.             return -1;
  38.         }
  39.     }
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement