#keywords guava, collections, java, code clip

package com.google.common.collect;

  • exam
	public static void main(String[] args) {
		
		List<Data> list = Lists.newArrayList(d("a", 1), d("b", 2), d("a", 3));
		System.out.println("src : "+list);
		
		Multimap<String, Data> mm = Multimaps.index(list, Data::getKey);

		System.out.println("mm : "+mm);
		System.out.println("mm.a : "+mm.get("a"));
		System.out.println("mm.b : "+mm.get("b"));
		System.out.println("mm.keys : "+mm.keys());
		System.out.println("mm.keys.size : "+mm.keys().size());
		System.out.println("mm.keySet : "+mm.keySet());
		System.out.println("mm.keySet.size : "+mm.keySet().size());
		System.out.println("mm.values : "+mm.values());

	}

	@AllArgsConstructor @ToString @Getter
	static class Data {
		String key;
		long qty;
		public String toString() {
			return key+"-"+qty;
		}
	}

	private static Data d(String vendorId, int qty) {
		return new Data(vendorId, qty);
	}

  • output
src : [a-1, b-2, a-3]
mm : {a=[a-1, a-3], b=[b-2]}
mm.a : [a-1, a-3]
mm.b : [b-2]
mm.keys : [a x 2, b]
mm.keys.size : 3
mm.keySet : [a, b]
mm.keySet.size : 2
mm.values : [a-1, a-3, b-2]


Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2019-06-11 21:43:27
Processing time 0.0083 sec