import java.util.List; public class WeightedRoundRobin { private List servers; private List weights; private int currentIndex; private int currentWeight; public WeightedRoundRobin(List servers, List weights) { this.servers = servers; this.weights = weights; this.currentIndex = -1; this.currentWeight = 0; } public String getNextServer() { while (true) { currentIndex = (currentIndex + 1) % servers.size(); if (currentIndex == 0) { currentWeight--; if (currentWeight <= 0) { currentWeight = getMaxWeight(); } } if (weights.get(currentIndex) >= currentWeight) { return servers.get(currentIndex); } } } private int getMaxWeight() { return weights.stream().max(Integer::compare).orElse(0); } public static void main(String[] args) { List servers = List.of("Server1", "Server2", "Server3"); List weights = List.of(5, 1, 1); WeightedRoundRobin weightedRoundRobinLB = new WeightedRoundRobin(servers, weights); for (int i = 0; i < 7; i++) { System.out.println(weightedRoundRobinLB.getNextServer()); } } }