Here you can find the source of loadBalancingLeftHeavy(List
public static <T> List<T> loadBalancingLeftHeavy(List<T> tasks, int size)
//package com.java2s; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Queue; public class Main { public static <T> List<T> loadBalancingLeftHeavy(List<T> tasks, int size) { Queue<T> sucQueue = new LinkedList<T>(); sucQueue.addAll(tasks);//from ww w . jav a 2 s. c om int todo_size = sucQueue.size(); List<List<T>> pList = new ArrayList<List<T>>(size); for (int i = 0; i < size; i++) pList.add(new LinkedList<T>()); T t1; int b = 0; for (int i = 0; i < todo_size / size; i++) { for (int j = 0; j < size; j++) { t1 = sucQueue.poll(); pList.get(Math.abs(b * (size - 1) - j)).add(t1); } b = (b == 0 ? 1 : 0); } for (int j = 0; j < size && !sucQueue.isEmpty(); j++) { t1 = sucQueue.poll(); pList.get(j).add(t1); } List<T> result = new ArrayList<T>(todo_size); for (int i = 0; i < size; i++) for (T t2 : pList.get(i)) result.add(t2); return result; } }