×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Shamir Yona
Added: Sep 19, 2015 2:39 PM
Views: 11
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package OtherTests;
  7.  
  8. import java.util.*;
  9.  
  10. /**
  11.  *
  12.  * @author shayzukerman
  13.  */
  14. public class CopyPasted
  15. {
  16.  
  17.     public static String largestNumber(int[] nums)
  18.     {
  19.         String[] strs = new String[nums.length];
  20.         for (int i = 0; i < nums.length; i++)
  21.         {
  22.             strs[i] = String.valueOf(nums[i]);
  23.         }
  24.  
  25.         Arrays.sort(strs, new Comparator<String>()
  26.         {
  27.             public int compare(String s1, String s2)
  28.             {
  29.                 String leftRight = s1 + s2;
  30.                 String rightLeft = s2 + s1;
  31.                 return -leftRight.compareTo(rightLeft);
  32.  
  33.             }
  34.         });
  35.  
  36.         StringBuilder sb = new StringBuilder();
  37.         for (String s : strs)
  38.         {
  39.             sb.append(s);
  40.         }
  41.  
  42.         while (sb.charAt(0) == '0' && sb.length() > 1)
  43.         {
  44.             sb.deleteCharAt(0);
  45.         }
  46.  
  47.         return sb.toString();
  48.     }
  49.  
  50.    
  51.     static class PQsort implements Comparator<Integer>
  52.     {
  53.  
  54.         public int compare(Integer one, Integer two)
  55.         {
  56.             return two - one;
  57.         }
  58.     }
  59.  
  60.     public static void linkedHashMapExample()
  61.     {
  62.         LinkedHashMap<Integer, String> m = new LinkedHashMap<>(16, 0.75f, true);
  63.         m.put(1, "Ant");
  64.         m.put(2, "Buffalo");
  65.         m.put(3, "Cat");
  66.         m.put(4, "Dog");
  67.         displayValues(m);
  68.         //access a value out of turn
  69.         String s = m.get(new Integer(2));
  70.         s = m.get(new Integer(4));
  71.         System.out.println("\n\nAfter:");
  72.         displayValues(m);
  73.     }
  74.  
  75.     public static void displayValues(LinkedHashMap<Integer, String> m)
  76.     {
  77.         for (String value : m.values())
  78.         {
  79.             System.out.println(value);
  80.         }
  81.     }
  82.  
  83.     public static void main(String[] args)
  84.     {
  85.         String elements[] = { "A", "Z", "M", "B", "C", "D", "G", "F" };
  86.         Set set = new TreeSet(Collections.reverseOrder());
  87.         for (int i = 0, n = elements.length; i < n; i++) {
  88.           set.add(elements[i]);
  89.         }
  90.         System.out.println(set);
  91.         System.out.println(((TreeSet) set).comparator());
  92.         System.exit(1);
  93.        
  94.         linkedHashMapExample();
  95.         System.exit(1);
  96.         //int[] nums = {3, 30, 34, 5, 9};
  97.         //System.out.println(largestNumber(nums));
  98.  
  99.         int[] ia =
  100.         {
  101.             //1, 10, 5, 3, 4, 7, 6, 9, 8
  102.             1, 3, 4, 2
  103.         };
  104.         PriorityQueue<Integer> pq1 = new PriorityQueue<Integer>();
  105.  
  106.         // use offer() method to add elements to the PriorityQueue pq1
  107.         for (int x : ia)
  108.         {
  109.             pq1.offer(x);
  110.         }
  111.  
  112.         System.out.println("pq1: " + pq1);
  113.  
  114.         PQsort pqs = new PQsort();
  115.         PriorityQueue<Integer> pq2 = new PriorityQueue<Integer>(10, pqs);
  116.                 // In this particular case, we can simply use Collections.reverseOrder()
  117.         // instead of self-defined comparator
  118.         for (int x : ia)
  119.         {
  120.             pq2.offer(x);
  121.         }
  122.  
  123.         System.out.println("pq2: " + pq2);
  124.  
  125.         // print size
  126.         System.out.println("size: " + pq2.size());
  127.         // return highest priority element in the queue without removing it
  128.         System.out.println("peek: " + pq2.peek());
  129.         // print size
  130.         System.out.println("size: " + pq2.size());
  131.         // return highest priority element and removes it from the queue
  132.         System.out.println("poll: " + pq2.poll());
  133.         // print size
  134.         System.out.println("size: " + pq2.size());
  135.  
  136.         System.out.print("pq2: " + pq2);
  137.  
  138.     }
  139. }
  140.