Improve several map iteration (#2938)

This commit is contained in:
Yuhao Bi 2018-12-12 14:23:58 +08:00 committed by Ian Luo
parent 6c9f74c090
commit 227a1699ac
11 changed files with 29 additions and 28 deletions

View File

@ -31,8 +31,8 @@ public class ConsistentHashLoadBalanceTest extends LoadBalanceBaseTest {
public void testConsistentHashLoadBalance() {
int runs = 10000;
Map<Invoker, AtomicLong> counter = getInvokeCounter(runs, ConsistentHashLoadBalance.NAME);
for (Invoker minvoker : counter.keySet()) {
Long count = counter.get(minvoker).get();
for (Map.Entry<Invoker, AtomicLong> entry : counter.entrySet()) {
Long count = entry.getValue().get();
Assert.assertTrue("abs diff should < avg", Math.abs(count - runs / (0f + invokers.size())) < runs / (0f + invokers.size()));
}
}

View File

@ -30,8 +30,8 @@ public class LeastActiveBalanceTest extends LoadBalanceBaseTest {
public void testLeastActiveLoadBalance_select() {
int runs = 10000;
Map<Invoker, AtomicLong> counter = getInvokeCounter(runs, LeastActiveLoadBalance.NAME);
for (Invoker minvoker : counter.keySet()) {
Long count = counter.get(minvoker).get();
for (Map.Entry<Invoker, AtomicLong> entry : counter.entrySet()) {
Long count = entry.getValue().get();
// System.out.println(count);
Assert.assertTrue("abs diff shoud < avg",
Math.abs(count - runs / (0f + invokers.size())) < runs / (0f + invokers.size()));

View File

@ -32,8 +32,8 @@ public class RandomLoadBalanceTest extends LoadBalanceBaseTest {
public void testRandomLoadBalanceSelect() {
int runs = 1000;
Map<Invoker, AtomicLong> counter = getInvokeCounter(runs, RandomLoadBalance.NAME);
for (Invoker minvoker : counter.keySet()) {
Long count = counter.get(minvoker).get();
for (Map.Entry<Invoker, AtomicLong> entry : counter.entrySet()) {
Long count = entry.getValue().get();
Assert.assertTrue("abs diff should < avg", Math.abs(count - runs / (0f + invokers.size())) < runs / (0f + invokers.size()));
}
@ -43,8 +43,8 @@ public class RandomLoadBalanceTest extends LoadBalanceBaseTest {
}
}
counter = getInvokeCounter(runs, LeastActiveLoadBalance.NAME);
for (Invoker minvoker : counter.keySet()) {
Long count = counter.get(minvoker).get();
for (Map.Entry<Invoker, AtomicLong> entry : counter.entrySet()) {
Long count = entry.getValue().get();
}
Assert.assertEquals(runs, counter.get(invoker1).intValue());
Assert.assertEquals(0, counter.get(invoker2).intValue());

View File

@ -47,8 +47,8 @@ public class RoundRobinLoadBalanceTest extends LoadBalanceBaseTest {
public void testRoundRobinLoadBalanceSelect() {
int runs = 10000;
Map<Invoker, AtomicLong> counter = getInvokeCounter(runs, RoundRobinLoadBalance.NAME);
for (Invoker minvoker : counter.keySet()) {
Long count = counter.get(minvoker).get();
for (Map.Entry<Invoker, AtomicLong> entry : counter.entrySet()) {
Long count = entry.getValue().get();
Assert.assertTrue("abs diff should < 1", Math.abs(count - runs / (0f + invokers.size())) < 1f);
}
}

View File

@ -432,10 +432,10 @@ public class AbstractClusterInvokerTest {
counter.get(sinvoker).incrementAndGet();
}
for (Invoker minvoker : counter.keySet()) {
Long count = counter.get(minvoker).get();
for (Map.Entry<Invoker, AtomicLong> entry : counter.entrySet()) {
Long count = entry.getValue().get();
// System.out.println(count);
if (minvoker.isAvailable())
if (entry.getKey().isAvailable())
Assert.assertTrue("count should > avg", count > runs / invokers.size());
}

View File

@ -30,8 +30,8 @@ public class Menu {
}
public Menu(Map<String, List<String>> menus) {
for (String key : menus.keySet()) {
this.menus.put(key, new ArrayList<String>(menus.get(key)));
for (Map.Entry<String, List<String>> entry : menus.entrySet()) {
this.menus.put(entry.getKey(), new ArrayList<String>(entry.getValue()));
}
}

View File

@ -161,11 +161,11 @@ public class MergeableClusterInvokerTest {
merge(expected, firstMenuMap);
merge(expected, secondMenuMap);
assertEquals(expected.keySet(), menu.getMenus().keySet());
for (String key : expected.keySet()) {
for (Map.Entry<String, List<String>> entry : expected.entrySet()) {
// FIXME: cannot guarantee the sequence of the merge result, check implementation in
// MergeableClusterInvoker#invoke
List<String> values1 = new ArrayList<String>(expected.get(key));
List<String> values2 = new ArrayList<String>(menu.getMenus().get(key));
List<String> values1 = new ArrayList<String>(entry.getValue());
List<String> values2 = new ArrayList<String>(menu.getMenus().get(entry.getKey()));
Collections.sort(values1);
Collections.sort(values2);
assertEquals(values1, values2);

View File

@ -144,12 +144,11 @@ public final class JavaBeanSerializeUtil {
}
} else if (obj instanceof Map) {
Map map = (Map) obj;
for (Object key : map.keySet()) {
Object value = map.get(key);
map.forEach((key, value) -> {
Object keyDescriptor = key == null ? null : createDescriptorIfAbsent(key, accessor, cache);
Object valueDescriptor = value == null ? null : createDescriptorIfAbsent(value, accessor, cache);
descriptor.setProperty(keyDescriptor, valueDescriptor);
} // ~ end of loop map
});// ~ end of loop map
} else {
if (JavaBeanAccessor.isAccessByMethod(accessor)) {
Map<String, Method> methods = ReflectUtils.getBeanPropertyReadMethods(obj.getClass());

View File

@ -126,9 +126,9 @@ public class DefaultFuture implements ResponseFuture {
* @param channel channel to close
*/
public static void closeChannel(Channel channel) {
for (long id : CHANNELS.keySet()) {
if (channel.equals(CHANNELS.get(id))) {
DefaultFuture future = getFuture(id);
for (Map.Entry<Long, Channel> entry: CHANNELS.entrySet()) {
if (channel.equals(entry.getValue())) {
DefaultFuture future = getFuture(entry.getKey());
if (future != null && !future.isDone()) {
Response disconnectResponse = new Response(future.getId());
disconnectResponse.setStatus(Response.CHANNEL_INACTIVE);

View File

@ -41,6 +41,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.fail;
@ -114,8 +115,8 @@ public class ExchangeCodecTest extends TelnetCodecTest {
inputBytes.put(new byte[]{MAGIC_HIGH, 0}, TelnetCodec.DecodeResult.NEED_MORE_INPUT);
inputBytes.put(new byte[]{0, MAGIC_LOW}, TelnetCodec.DecodeResult.NEED_MORE_INPUT);
for (byte[] input : inputBytes.keySet()) {
testDecode_assertEquals(assemblyDataProtocol(input), inputBytes.get(input));
for (Map.Entry<byte[], Object> entry: inputBytes.entrySet()) {
testDecode_assertEquals(assemblyDataProtocol(entry.getKey()), entry.getValue());
}
}

View File

@ -35,6 +35,7 @@ import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
public class TelnetCodecTest {
protected Codec2 codec;
@ -235,8 +236,8 @@ public class TelnetCodecTest {
exitbytes.put(new byte[]{1, -1, -12, -1, -3, 6}, false); //must equal the bytes
exitbytes.put(new byte[]{-1, -19, -1, -3, 6}, true); /* Linux Pause */
for (byte[] exit : exitbytes.keySet()) {
testDecode_WithExitByte(exit, exitbytes.get(exit));
for (Map.Entry<byte[], Boolean> entry : exitbytes.entrySet()) {
testDecode_WithExitByte(entry.getKey(), entry.getValue());
}
}