Advertisement
Guest User

Untitled

a guest
Dec 10th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1.     static String[] paginate(int num, String[] results) {
  2.         // map host id's to listings and maintains order of insertion
  3.         LinkedHashMap<Integer, String> hm = new LinkedHashMap<Integer, String>();
  4.         // store host id's that have been seen before
  5.         Queue<String> notUnique = new LinkedList<String>();
  6.        
  7.         for (String result : results) {
  8.             String[] split = result.split(",");
  9.             int id = Integer.parseInt(split[0]);
  10.             if (!hm.containsKey(id)) {
  11.                 hm.put(id, result);
  12.             }
  13.             else {
  14.                 notUnique.offer(result);
  15.             }
  16.         }
  17.  
  18.         // convert linked hash map to a queue
  19.         Queue<String> unique = new LinkedList<String>();
  20.         for (String value : hm.values()) {
  21.             unique.offer(value);
  22.         }
  23.        
  24.         int numBreaks = results.length / num; // rounds down
  25.         String[] paginated = new String[results.length + numBreaks];
  26.         int pageCount = 0; // stores paginated array index
  27.         int count = 0; // stores when to add a page break
  28.         while (!unique.isEmpty() || !notUnique.isEmpty()) {
  29.             while (!unique.isEmpty()) {
  30.                 String curr = unique.poll();
  31.                 paginated[pageCount] = curr;
  32.                 System.out.println(curr);
  33.                 pageCount++;
  34.                 count++;
  35.                 if (count == num) {
  36.                     break;
  37.                 }
  38.             }
  39.             while (!notUnique.isEmpty()) {
  40.                 String curr = notUnique.poll();
  41.                 System.out.println(curr);
  42.                 paginated[pageCount] = curr;
  43.                 pageCount++;
  44.                 count++;
  45.                 if (count == num) {
  46.                     break;
  47.                 }
  48.             }
  49.             paginated[pageCount] = "";
  50.             pageCount++;
  51.         }
  52.         return paginated;
  53.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement