public static void main(String[] args) { List<Frob> list = new ArrayList<Frob>(); Map<Frob, Fnorkle> map = new HashMap<Frob, Fnorkle>(); Quark<Fnorkle> quark = new Quark<Fnorkle>(); Particle<Long, Double> p = new Particle<Long, Double>(); System.out.println(Arrays.toString(list.getClass().getTypeParameters())); System.out.println(Arrays.toString(map.getClass().getTypeParameters())); System.out.println(Arrays.toString(quark.getClass().getTypeParameters())); System.out.println(Arrays.toString(p.getClass().getTypeParameters())); }
public void testExactStringStringMap() throws Exception { ObjectMapper mapper = new ObjectMapper(); // to get typing, must use type reference String JSON = "{ \"a\" : \"b\" }"; Map<String, Integer> result = mapper.readValue(JSON, new TypeReference<TreeMap<String, String>>() {}); assertNotNull(result); assertEquals(TreeMap.class, result.getClass()); assertEquals(1, result.size()); assertEquals("b", result.get("a")); assertNull(result.get("b")); }
public static void performTest(final Map<String, Integer> m) throws Exception { out.println("Test started for: " + m.getClass()); long avgTime = 0; for (int i = 0; i < 5; i++) { long startTime = System.nanoTime(); ExecutorService exService = Executors.newFixedThreadPool(POOL_SIZE); for (int j = 0; j < POOL_SIZE; j++) { exService.execute( new Runnable() { @SuppressWarnings("unused") public void run() { for (int i = 0; i < 500000; i++) { Integer randomNum = (int) Math.ceil(Math.random() * 550000); // Retrieve value. We are not using it anywhere Integer value = m.get(String.valueOf(randomNum)); // Put value m.put(String.valueOf(randomNum), randomNum); } } }); } // Make sure executor stops exService.shutdown(); // Blocks until all tasks have completed execution after a shutdown request exService.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); long entTime = System.nanoTime(); long totalTime = (entTime - startTime) / 1000000L; avgTime += totalTime; out.println("500K entried added/retrieved in " + totalTime + " ms"); } out.println("For " + m.getClass() + " the average time is " + avgTime / 5 + " ms\n"); }
/** * Let's also check that it is possible to do type conversions to allow use of non-String Map * keys. */ public void testIntBooleanMap() throws Exception { ObjectMapper mapper = new ObjectMapper(); // to get typing, must use type reference String JSON = "{ \"1\" : true, \"-1\" : false }"; Map<String, Integer> result = mapper.readValue(JSON, new TypeReference<HashMap<Integer, Boolean>>() {}); assertNotNull(result); assertEquals(HashMap.class, result.getClass()); assertEquals(2, result.size()); assertEquals(Boolean.TRUE, result.get(Integer.valueOf(1))); assertEquals(Boolean.FALSE, result.get(Integer.valueOf(-1))); assertNull(result.get("foobar")); assertNull(result.get(0)); }
public void testExactStringIntMap() throws Exception { ObjectMapper mapper = new ObjectMapper(); // to get typing, must use type reference String JSON = "{ \"foo\" : 13, \"bar\" : -39, \n \"\" : 0 }"; Map<String, Integer> result = mapper.readValue(JSON, new TypeReference<HashMap<String, Integer>>() {}); assertNotNull(result); assertEquals(HashMap.class, result.getClass()); assertEquals(3, result.size()); assertEquals(Integer.valueOf(13), result.get("foo")); assertEquals(Integer.valueOf(-39), result.get("bar")); assertEquals(Integer.valueOf(0), result.get("")); assertNull(result.get("foobar")); assertNull(result.get(" ")); }
/** * ArrayList<String>和ArrayList<Integer>很容易认为是不同的类型, 不同的类型在行为方面肯定不同 但输出发现c1==c2为true * * @param args */ public static void main(String[] args) { Class c1 = new ArrayList<String>().getClass(); Class c2 = new ArrayList<Integer>().getClass(); System.out.println(c1 == c2); // 输出为true List<Frob> list = new ArrayList<Frob>(); Map<Frob, Fnorkle> map = new HashMap<Frob, Fnorkle>(); Quark<Fnorkle> quark = new Quark<Fnorkle>(); Particle<Long, Double> part = new Particle<Long, Double>(); // Class.getTypeParameters()将返回一个Typeariable对象数组,表示有泛型声明 // 的类型参数... System.out.println(Arrays.toString(list.getClass().getTypeParameters())); System.out.println(Arrays.toString(map.getClass().getTypeParameters())); System.out.println(Arrays.toString(quark.getClass().getTypeParameters())); System.out.println(Arrays.toString(part.getClass().getTypeParameters())); /** 输出结果: [E] [K, V] [Q] [POSITION, MOMENTUM] */ /** * 输出结果是一些占位符 说明:在泛型代码内部,无法获得任何有关泛型参数类型的信息。 当你在使用泛型时,任何具体的类型信息都被擦除了,你唯一知道的就是你在使用一个对象。 * 因此,List<String>和List<Integer>在运行时事实上是相同的类型。这两种形式都被 擦除成它们的原生类型,即List */ }
protected String buildSnippetAssertMap(String expression, Map value) { Random r = new Random(); String type = value.getClass().getCanonicalName(); String localVar = "map_" + Math.abs(r.nextInt()); String newCollection = type + " " + localVar + " = new " + type + "<Object, Object>();"; Set<Map.Entry> set = value.entrySet(); for (Map.Entry v : set) { newCollection += "\n\t" + localVar + ".put(" + printPrimitiveString(v.getKey()) + ", " + printPrimitiveString(v.getValue()) + ");\n"; } newCollection += "\t" + junitAssertClassName + ".assertEquals(" + localVar + ", " + expression + ");"; return newCollection; }