gpt4 book ai didi

java - java中从List中随机选择一个对象

转载 作者:行者123 更新时间:2023-12-01 18:30:59 30 4
gpt4 key购买 nike

在你开始给这个问题打-1之前,我想说我已经阅读了至少4个关于此类问题的问题,但每个问题都没有完全帮助我。

我有这个类,其中包含以下方法:

class OperationDispatcher {

OperationDispatcher(ManagementService.Client managementClient) {
this.managementClient = managementClient;
}

ManagementService.Client managementClient;

public long executeOperation(String opName, int num1, int num2) throws TException {

List<ServiceProvider> providers = new ArrayList<ServiceProvider>();
providers = managementClient.getProvidersForService(opName);

long result = 0;

if(providers.isEmpty())
System.out.println("No Service Available");
else
switch(opName) {
case "addition":
result = arithmeticClient.add(num1, num2);
break;
case "multiplication":
result = arithmeticClient.multiply(num1, num2);
break;
case "substraction":
result = arithmeticClient.substract(num1, num2);
break;
case "division":
result = arithmeticClient.divide(num1, num2);
break;
}
return result;
}

}

我正在尝试实现此行为:

  1. 两个或多个对象“节点”注册到我在测试中创建的本地中央服务器;
  2. 每个节点都有一个服务列表,并向其他节点发出请求或提供服务;
  3. 当发出请求时,会返回一个“ServiceProviders”列表,其中每个服务提供商都是可能的“应答节点”;
  4. 从列表中随机选择一个服务提供商;
  5. 所选的 ServiceProvider 执行请求的算术运算。

我对第 1、2、3 和 5 点没有任何问题。我现在正在尝试修改该方法,使其从列表中存在的 ServiceProvider 中随机选择一个 ServiceProvider。

我不确定应该将代码放在哪里(在我的类(class)中),以便从列表中随机选取对象并使其执行请求的操作。你能帮我吗?

编辑所以,我修改了我的代码如下:

class OperationDispatcher {

private List<ServiceProvider> providers = new ArrayList<ServiceProvider>();

OperationDispatcher(ManagementService.Client managementClient) {
this.managementClient = managementClient;
}

//tiro su un management client
ManagementService.Client managementClient;

public long executeOperation(String opName, int num1, int num2) throws TException {

ServiceProvider randomProvider = new ServiceProvider();
providers = managementClient.getProvidersForService(opName);

long result = 0;

if(providers.isEmpty())
System.out.println("No Service Available");

else if(providers.size() == 1) {
switch(opName) {
case "addition":
result = arithmeticClient.add(num1, num2);
break;
case "multiplication":
result = arithmeticClient.multiply(num1, num2);
break;
case "substraction":
result = arithmeticClient.substract(num1, num2);
break;
case "division":
result = arithmeticClient.divide(num1, num2);
break;
}
}

else if (providers.size() >= 2) {
randomProvider = providers.get(random.nextInt(providers.size()));
if(randomProvider != null) {
switch(opName) {
case "addition":
result = arithmeticClient.add(num1, num2);
break;
case "multiplication":
result = arithmeticClient.multiply(num1, num2);
break;
case "substraction":
result = arithmeticClient.substract(num1, num2);
break;
case "division":
result = arithmeticClient.divide(num1, num2);
break;
}
}
}
return result;
}

}

我的测试是这样的:

@Before
public void initializeManagementServer() throws Exception {

managementServer = new ManagementServer(7911);
managementServer.start();

Thread.sleep(200);
managementServerTransport = new TSocket("localhost", 7911);
managementServerProtocol = new TBinaryProtocol(managementServerTransport);
managementClient = new ManagementService.Client(managementServerProtocol);
managementServerTransport.open();
}

@Test
public void testArithmeticServer() throws Exception {

ServiceProvider randomProvider = new ServiceProvider();

List<String> node1Svc = new ArrayList<>();
node1Svc.add("addition");

List<String> node2Svc = new ArrayList<>();
node2Svc.add("substraction");
node2Svc.add("multiplication");

List<String> node3Svc = new ArrayList<>();
node3Svc.add("addition");
node3Svc.add("multiplication");
node3Svc.add("division");

List<String> node4Svc = new ArrayList<>();
node4Svc.add("addition");
node4Svc.add("division");

NodeManifest node1 = new NodeManifest("Node 1", node1Svc);
NodeManifest node2 = new NodeManifest("Node 2", node2Svc);
NodeManifest node3 = new NodeManifest("Node 3", node3Svc);
NodeManifest node4 = new NodeManifest("Node 4", node4Svc);

int port = managementClient.registerNode(node1);
assertEquals(1026, port);
port = managementClient.registerNode(node2);
assertEquals(1027, port);
port = managementClient.registerNode(node3);
assertEquals(1028, port);
port = managementClient.registerNode(node4);
assertEquals(1029, port);



arithmeticServer = new ArithmeticServer(port);
arithmeticServer.start();

arithmeticTransport = new TSocket("localhost", port);
arithmeticProtocol = new TBinaryProtocol(arithmeticTransport);
arithmeticClient = new ArithmeticService.Client(arithmeticProtocol);
arithmeticTransport.open();

OperationDispatcher dispatcher = new OperationDispatcher(managementClient);
long result = dispatcher.executeOperation("addition", 1, 2);
assertEquals(3, result);
assertEquals(3, dispatcher.providers.size());

arithmeticTransport.close();
arithmeticServer.stop();

@After
public void teardownManagementServer() throws Exception {
managementServerTransport.close();
managementServer.stop();
}
}

我已经注册了4个节点。节点#1 是请求“添加”服务的节点,节点#2、#3 和#4 都有该服务。然后我创建一个对象OperationDispatcher,请求“添加”服务,它返回一个包含三个ServiceProvider 的列表。现在我想从该列表中随机选择一项,最后由 ServiceProvider 执行该操作。现在,始终选择节点 4。怎么了?

最佳答案

您可以使用Java的Random类如下:

ServiceProvider provider = providers.get(new Random().nextInt(providers.size()));

请注意,初始化新的Random对象是一个稍微昂贵的操作,因此您可能希望创建一次,然后继续重用它:

private static final Random RANDOM = new Random();

// ...

ServiceProvider provider = providers.get(RANDOM.nextInt(providers.size()));

关于java - java中从List中随机选择一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24239334/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com