Untitled
unknown
java
a year ago
841 B
2
Indexable
Never
package org.example; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Random; import java.util.stream.Collectors; public class LoadBalancer { Map<String, Instance> instances = new HashMap<>(); public void addInstance(Instance instance) { if (instances.size() > 9) { throw new RuntimeException("Max instances"); } instances.put(instance.getAddress(), instance); } public List<String> gatAllInstances() { return instances.values().stream() .map(Instance::getAddress) .collect(Collectors.toList()); } public Instance getRandomInstance() { Random random = new Random(); final int randomId = random.nextInt(instances.size()); return instances.get(String.valueOf(randomId)); } }