当前位置 : 主页 > 编程语言 > java >

集合排列组合

来源:互联网 收集:自由互联 发布时间:2021-06-28
对集合中的数据进行组合排列 /** * 对集合中的数据进行组合排列 * * @param data * @return */ private Collection group(List data) { Collection temps = convertToCollection(data.get(0)); Set groups = new LinkedHashSet();
对集合中的数据进行组合排列
/**
     * 对集合中的数据进行组合排列
     *
     * @param data
     * @return
     */
    private Collection group(List data) {
        Collection temps = convertToCollection(data.get(0));
        Set groups = new LinkedHashSet();
        int size = data.size();
        for (int i = 1; i < size; i++) {
            Collection items = convertToCollection(data.get(i));
            for (Object item : items) {
                for (Object temp : temps) {
                    groups.add(String.valueOf(temp) + String.valueOf(item));
                }
            }
            if (i < (size - 1)) {
                temps.clear();
                temps.addAll(groups);
                groups.clear();
            }
        }
        return groups;
    }
将一个对象转换为集合
private Collection convertToCollection(Object data) {
        if (Collection.class.isAssignableFrom(data.getClass())) {
            return (Collection) data;
        }
        Collection coll = new ArrayList();
        if (data.getClass().isArray()) {
            int length = Array.getLength(data);
            for (int i = 0; i < length; i++) {
                coll.add(Array.get(data, i));
            }
            return coll;
        }
        coll.add(data);
        return coll;
    }
汉子转拼音测试
@Test
    public void test3() throws Exception {
        String text = "你好中国人民";
        List list = new ArrayList();
        char[] chars = text.toCharArray();
        for (char aChar : chars) {
            list.add(PinyinHelper.convertToPinyinArray(aChar));
        }
        Collection group = group(list);
        System.out.println(group);
    }
网友评论