Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static String[] paginate(int num, String[] results) {
- // map host id's to listings and maintains order of insertion
- LinkedHashMap<Integer, String> hm = new LinkedHashMap<Integer, String>();
- // store host id's that have been seen before
- Queue<String> notUnique = new LinkedList<String>();
- for (String result : results) {
- String[] split = result.split(",");
- int id = Integer.parseInt(split[0]);
- if (!hm.containsKey(id)) {
- hm.put(id, result);
- }
- else {
- notUnique.offer(result);
- }
- }
- // convert linked hash map to a queue
- Queue<String> unique = new LinkedList<String>();
- for (String value : hm.values()) {
- unique.offer(value);
- }
- int numBreaks = results.length / num; // rounds down
- String[] paginated = new String[results.length + numBreaks];
- int pageCount = 0; // stores paginated array index
- int count = 0; // stores when to add a page break
- while (!unique.isEmpty() || !notUnique.isEmpty()) {
- while (!unique.isEmpty()) {
- String curr = unique.poll();
- paginated[pageCount] = curr;
- System.out.println(curr);
- pageCount++;
- count++;
- if (count == num) {
- break;
- }
- }
- while (!notUnique.isEmpty()) {
- String curr = notUnique.poll();
- System.out.println(curr);
- paginated[pageCount] = curr;
- pageCount++;
- count++;
- if (count == num) {
- break;
- }
- }
- paginated[pageCount] = "";
- pageCount++;
- }
- return paginated;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement