javascript - Is there any simpler way to distribute the elements of a collection equally to another array object?
phpcn_u1582
phpcn_u1582 2017-05-17 10:09:17
0
2
633
  1. Array A=["G","D","B","H"], the number of elements is variable, and the element content can be any character

  2. Set B=[{"id":"a",item:""},{"id":"a=b",item:""}], variable quantity, fixed structure

  3. Now we need to distribute the elements in A equally to the items in B. If there are multiple ones, separate them with commas.

  4. When the number of elements of A is less than or greater than the length of B, it only requires that all A appear in B. The items of set B must be allocated to at least one element, preferably evenly, but the set Each item in B cannot have duplicate elements

Is there any easier way?

phpcn_u1582
phpcn_u1582

reply all(2)
世界只因有你

Already solved


import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by YSTYLE on 2017-04-17 0017.
 */
public class TEst {

    private static List<String> list = Lists.newArrayList();
    private static List<Map> los = Lists.newArrayList();

    public static void main(String[] args) {
        init();
        List<String> augmented = list;
        int groupCount = los.size();
        if (list.size() < groupCount){
            augmented = Augmented(list, groupCount);
        }
        List<List<String>> chunk = chunk2(augmented, groupCount);
        for (int i = 0; i < los.size(); i++) {
            los.get(i).put("item", StringUtils.join(chunk.get(i),",") );
        }
        System.out.println(los);
    }
    // 初始化测试数据
    private static void init (){
        int losCount = RandomUtils.nextInt(1,10);
        int listCount = RandomUtils.nextInt(1,10);
        for (int i = 0; i < listCount ; i++) {
            list.add(RandomStringUtils.randomAlphabetic(4));
        }
        for (int i = 0; i <  losCount; i++) {
            Map<String,Integer> map = new HashMap<String, Integer>();
            map.put("id",RandomUtils.nextInt(10000,99999));
            los.add(map);
        }
        System.out.println("生成的数组: " + list+"  数量: "+listCount);
        System.out.println("生成的对象数量: " + los.size());
    }

    // 分组数据
    public static <T> List<List<T>> chunk2(List<T> list, int group){
        if (CollectionUtils.isEmpty(list)){
            return Lists.newArrayList();
        }
        List<List<T>> result = Lists.newArrayList();
        Map<Integer,Set<T>> temp = Maps.newHashMap();
        for (int i = 0; i < list.size(); i++) {
            if (temp.containsKey(i%group)) {
                Set<T> ts = temp.get(i % group);
                ts.add(list.get(i));
                temp.put(i%group,ts);
            }else {
                Set<T> ts = Sets.newHashSet();
                ts.add(list.get(i));
                temp.put(i % group,ts);
            }
        }
        for (Set<T> ts : temp.values()) {
            result.add(Lists.newArrayList(ts));
        }
        return result;
    }

    // 填充数据
    public static <T> List<T> Augmented(List<T> list ,int size){
        int length = CollectionUtils.isEmpty(list)?0:list.size();
        if (length<1){
            return Lists.newArrayList();
        }
        List<T> result = Lists.newArrayList(list);
        if (length > size){
            return result;
        }
        int count = size - length;
        for (int i = 0; i < count; i++) {
            result.add(list.get(RandomUtils.nextInt(0, length)));
        }
        return result;
    }
}
阿神
1.A.length<=B.length \对A循环,直接赋值

2.A.length>B.length\对B循环
let size = Math.floor(A.length/B.length)\取整
for(let i in B){\数组划分
   if(i==B.lenght-1){
       B[i].item = A.splice(Start)
   }else{
    let start = 0;
    B[i].item = A.splice(start,start + size))
    start = start + 4;
    }
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template