将对象列表根据对象字段的唯一值数量分成n个列表的方法是否存在?

huangapple go评论104阅读模式
英文:

Is there a way to split a list of objects into n number of lists depending on the number of unique values of an object field?

问题

I have a method that returns a list of objects.

In that object, there is a String field orderType.

Is there a way for me to separate that list into n number of lists depending on the unique values of orderType?

For example, if the method returns a list containing 10 objects, then 4 of them have an orderType value of x, 3 have an orderType value of y, and another 4 have a value of z.

Then the result will be 3 lists containing 4, 3, 4 objects respectively.

orderType does not only have x, y, and z as possible values.

I am having a hard time putting this into actual code, but my idea is like this:

  1. List<Orders> orderList = getOrder();
  2. for (int i = 0; i < orderList.size(); i++) {
  3. if (orderList.get(i).orderType is unique) {
  4. create a new list
  5. } else {
  6. add to an existing list having the same orderType
  7. }
  8. }
英文:

I have a method that returns a list of objects.

In that object, there is a String field orderType.

Is there a way for me to separate that list into n number of lists depending on the unique values of orderType?

For example the method returns a list containing 10 objects, then 4 of them has orderType value of x, 3 has orderType value of y, then another 4 has a value of z.

then the result will be 3 lists containing 4,3,4 objects respectively

orderType does not only have x, y and z as possible values.

i am having a hard time putting this into an actual code but my idea is like this

  1. List&lt;Orders&gt; orderList = getOrder();
  2. for(int i=0; i&lt;orderList.size();i++){
  3. if(orderList.get(i).orderType is unique){
  4. create a new list
  5. }else{
  6. add to an existing list having the same orderType
  7. }
  8. }

答案1

得分: 1

以下是翻译后的代码部分:

  1. 我建议使用这个相对通用的函数
  2. ```java
  3. public static <T, U> Map<U, List<T>> getLists(List<T> values, Function<T, U> orderBy) {
  4. Map<U, List<T>> lists = new HashMap<>();
  5. for (T value : values) {
  6. U type = orderBy.apply(value);
  7. if (!lists.containsKey(type)) {
  8. lists.put(type, new ArrayList<>());
  9. }
  10. lists.get(type).add(value);
  11. }
  12. return lists;
  13. }

订单类:

  1. class Order {
  2. private String orderType;
  3. private String name;
  4. public Order(String orderType, String name) {
  5. this.orderType = orderType;
  6. this.name = name;
  7. }
  8. public String getOrderType() {
  9. return orderType;
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. }

如何使用:

  1. public static void main(String args[]){
  2. List<Order> orders = new ArrayList<>();
  3. orders.add(new Order("TYPE1", "Order1"));
  4. orders.add(new Order("TYPE2", "Order2"));
  5. orders.add(new Order("TYPE1", "Order3"));
  6. orders.add(new Order("TYPE1", "Order4"));
  7. orders.add(new Order("TYPE2", "Order5"));
  8. orders.add(new Order("TYPE1", "Order6"));
  9. orders.add(new Order("TYPE1", "Order7"));
  10. orders.add(new Order("TYPE1", "Order8"));
  11. Map<String, List<Order>> map = getLists(orders, Order::getOrderType);
  12. for (String key : map.keySet()) {
  13. System.out.println(key);
  14. for (Order order : map.get(key)) {
  15. System.out.println(order.getOrderType() + " " + order.getName());
  16. }
  17. System.out.println();
  18. }
  19. }

请注意,代码中的HTML实体(如&lt;&quot;)已被移除,以便更好地显示代码。

英文:

I'd suggest this rather general function:

  1. public static &lt;T, U&gt; Map&lt;U, List&lt;T&gt;&gt; getLists(List&lt;T&gt; values, Function&lt;T, U&gt; orderBy) {
  2. Map&lt;U, List&lt;T&gt;&gt; lists = new HashMap&lt;&gt;();
  3. for (T value : values) {
  4. U type = orderBy.apply(value);
  5. if (!lists.containsKey(type)) {
  6. lists.put(type, new ArrayList&lt;&gt;());
  7. }
  8. lists.get(type).add(value);
  9. }
  10. return lists;
  11. }

Order Class:

  1. class Order {
  2. private String orderType;
  3. private String name;
  4. public Order(String orderType, String name) {
  5. this.orderType = orderType;
  6. this.name = name;
  7. }
  8. public String getOrderType() {
  9. return orderType;
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. }

How to use:

  1. public static void main(String args[]){
  2. List&lt;Order&gt; orders = new ArrayList&lt;&gt;();
  3. orders.add(new Order(&quot;TYPE1&quot;, &quot;Order1&quot;));
  4. orders.add(new Order(&quot;TYPE2&quot;, &quot;Order2&quot;));
  5. orders.add(new Order(&quot;TYPE1&quot;, &quot;Order3&quot;));
  6. orders.add(new Order(&quot;TYPE1&quot;, &quot;Order4&quot;));
  7. orders.add(new Order(&quot;TYPE2&quot;, &quot;Order5&quot;));
  8. orders.add(new Order(&quot;TYPE1&quot;, &quot;Order6&quot;));
  9. orders.add(new Order(&quot;TYPE1&quot;, &quot;Order7&quot;));
  10. orders.add(new Order(&quot;TYPE1&quot;, &quot;Order8&quot;));
  11. Map&lt;String, List&lt;Order&gt;&gt; map = getLists(orders, Order::getOrderType);
  12. for (String key : map.keySet()) {
  13. System.out.println(key);
  14. for (Order order : map.get(key)) {
  15. System.out.println(order.getOrderType() + &quot; &quot; + order.getName());
  16. }
  17. System.out.println();
  18. }
  19. }

答案2

得分: 0

我认为更容易将元素提取到一个Map中,然后您可以利用Collectors.toMap的第三个参数,将相同键的值合并起来。

  1. public void tryIt() {
  2. // 给定类
  3. class Foo {
  4. String x;
  5. Foo(String x) {
  6. this.x = x;
  7. }
  8. @Override
  9. public String toString() {
  10. return this.x;
  11. }
  12. }
  13. List<Foo> foos = new ArrayList<>();
  14. IntStream.range(0, 4).forEach(i -> {
  15. foos.add(new Foo("x"));
  16. foos.add(new Foo("z"));
  17. });
  18. IntStream.range(0, 3).forEach(i -> foos.add(new Foo("y")));
  19. // 创建一个元素到原始对象列表的映射,根据相同的元素键进行分组
  20. Map<String, List<Foo>> collect = foos.stream().collect(Collectors.toMap(
  21. f -> f.x,
  22. Collections::singletonList,
  23. (l1, l2) -> Stream.concat(l1.stream(), l2.stream()).collect(Collectors.toList())));
  24. System.out.println(collect);
  25. }

输出

  1. {x=[x, x, x, x], y=[y, y, y], z=[z, z, z, z]}
英文:

I think it's easier to pull out the element to a Map, then you can take advantage of the Collectors.toMap third parameter that combines the values of the same key.

  1. public void tryIt() {
  2. // given
  3. class Foo {
  4. String x;
  5. Foo(String x) {
  6. this.x = x;
  7. }
  8. @Override
  9. public String toString() {
  10. return this.x;
  11. }
  12. }
  13. List&lt;Foo&gt; foos = new ArrayList&lt;&gt;();
  14. IntStream.range(0, 4).forEach(i -&gt; {
  15. foos.add(new Foo(&quot;x&quot;));
  16. foos.add(new Foo(&quot;z&quot;));
  17. });
  18. IntStream.range(0, 3).forEach(i -&gt; foos.add(new Foo(&quot;y&quot;)));
  19. // create a mapping of element, to list of original object, grouping by same element key
  20. Map&lt;String, List&lt;Foo&gt;&gt; collect = foos.stream().collect(Collectors.toMap(
  21. f -&gt; f.x,
  22. Collections::singletonList,
  23. (l1, l2) -&gt; Stream.concat(l1.stream(), l2.stream()).collect(Collectors.toList())));
  24. System.out.println(collect);
  25. }

Output

  1. {x=[x, x, x, x], y=[y, y, y], z=[z, z, z, z]}

huangapple
  • 本文由 发表于 2020年8月2日 11:49:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63212205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定