1
回答
用guava对list分组
华为云4核8G,高性能云服务器,免费试用   

例如有这样一个List对象

{
    {
        "name": "张三",
        "age": 24,
        "sex": "男"
    },
    {
        "name": "李四",
        "age": 25,
        "sex": "女"
    },
    {
        "name": "王五",
        "age": 14,
        "sex": "男"
    },
    {
        "name": "赵六",
        "age": 24,
        "sex": "女"
    },
    {
        "name": "王麻子",
        "age": 25,
        "sex": "男"
    },
    {
        "name": "二狗子",
        "age": 26,
        "sex": "男"
    }
}

如何用guava按照对象中的某个属性进行分组、得到List<List<Object>> 这样的返回值

我试了https://my.oschina.net/matt0614/blog/672418这个方法、但是报如下异常

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to cn.com.ping2.test.guava.Person
	at cn.com.ping2.test.guava.TestGuava$1.apply(TestGuava.java:1)
	at com.google.common.collect.Multimaps.index(Multimaps.java:1509)
	at com.google.common.collect.Multimaps.index(Multimaps.java:1455)
	at cn.com.ping2.test.guava.TestGuava.test1(TestGuava.java:35)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

 

 

这是我的代码块

public class TestGuava {
	@Test
	public void test1() throws Exception {
		Person p1 = new Person("a", 10, "男", "china");
		Person p2 = new Person("b", 11, "女", "japan");
		Person p3 = new Person("c", 12, "男", "japan");
		Person p4 = new Person("d", 14, "男", "USA");
		Person p5 = new Person("e", 14, "女", "china");
		List<Person> persons = new ArrayList<>();
		persons.add(p1);
		persons.add(p2);
		persons.add(p3);
		persons.add(p4);
		persons.add(p5);

		ImmutableSet digits = ImmutableSet.of(persons.toArray());
		Function<Person, String> sex = new Function<Person, String>() {
			@Override
			public String apply(Person person) {
				return person.getSex();
			}
		};
		ImmutableListMultimap<String, Person> sexList = Multimaps.index(digits, sex);

		ImmutableListMultimap ageList = Multimaps.index(digits, new Function<Person, Integer>() {
			@Override
			public Integer apply(Person person) {
				return person.getAge();
			}
		});

		ImmutableListMultimap countryList = Multimaps.index(digits, new Function<Person, String>() {
			@Override
			public String apply(Person person) {
				return person.getContry();
			}
		});
		System.out.println("按性别分组 = " + sexList);
		System.out.println("按年龄分组 = " + ageList);
		System.out.println("按国家分组 = " + countryList);
	}
}
public class Person implements Serializable{
	private static final long serialVersionUID = -3930910593527340005L;
	private String name;
	private Integer age;
	private String sex;
	private String contry;



	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getContry() {
		return contry;
	}

	public void setContry(String contry) {
		this.contry = contry;
	}

	public Person(String name, Integer age, String sex, String contry) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.contry = contry;
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", sex=" + sex + ", contry=" + contry + "]";
	}
}

 

举报
JDog
发帖于1年前 1回/792阅
共有1个答案 最后回答: 1年前

    public static void main(String[] args) {
        Person p1 = new Person("a", 10, "男", "china");
        Person p2 = new Person("b", 11, "女", "japan");
        Person p3 = new Person("c", 12, "男", "japan");
        Person p4 = new Person("d", 14, "男", "USA");
        Person p5 = new Person("e", 14, "女", "china");
        List<Person> persons = Lists.newArrayList(p1,p2,p3,p4,p5);
        Multimap<String,Person> multimap1 = ArrayListMultimap.create();
        Multimap<Integer,Person> multimap2 = ArrayListMultimap.create();
        Multimap<String,Person> multimap3 = ArrayListMultimap.create();
        for (Person p : persons) {
            multimap1.put(p.getSex(), p);
            multimap2.put(p.getAge(), p);
            multimap3.put(p.getContry(), p);
        }

        Map<String, Collection<Person>> sex = multimap1.asMap();
        for (Entry<String, Collection<Person>> p : sex.entrySet()) {
            System.out.println("按性别分组 = " +p.getKey()+"--"+ p.getValue());
        }
        Map<Integer, Collection<Person>> age = multimap2.asMap();
        for (Entry<Integer, Collection<Person>> p : age.entrySet()) {
            System.out.println("按年龄分组 = " +p.getKey()+"岁"+"--"+ p.getValue());
        }
        Map<String, Collection<Person>> country = multimap3.asMap();
        for (Entry<String, Collection<Person>> p : country.entrySet()) {
            System.out.println("按国家分组 = " +p.getKey() +"--" + p.getValue());
        }
    }

--- 共有 1 条评论 ---
JDog3ks、成功解决问题 1年前 回复
顶部