Untitled
unknown
java
2 years ago
841 B
4
Indexable
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)); } }
Editor is loading...