All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.datastax.util.collection.CollectionUtil Maven / Gradle / Ivy

The newest version!
package com.datastax.util.collection;

import com.datastax.collection.KeyValue;

import java.util.*;

/**
 * UserItem: Anders Hong
 * Date: 10/9/13
 * Time: 10:40 AM
 */
public class CollectionUtil {
    public static List> countListKV(List list){
        List> kvList=new ArrayList<>();
        Map map=countList(list);
        for(String key : map.keySet()){
            kvList.add(new KeyValue<>(key, map.get(key).longValue()));
        }

        return kvList;
    }

    public static Map countList(List list){
        Map map=new HashMap();
        for(String value : list){
            if(map.containsKey(value)){
                int count=map.get(value);
                count++;
                map.remove(value);
                map.put(value,count);
            }else {
                map.put(value,1);
            }
        }

        return map;
    }

    public static Map countListPercent(List list){
        Map map=countList(list);
        return countMapPercent(map);
    }

    public static Map countMapPercent(Map map){
        Map perMap=new HashMap();
        double total=0;
        for(String key : map.keySet()){
            int weight=map.get(key);
            total+=weight;
        }

        for(String key : map.keySet()){
            double weight=map.get(key);
            perMap.put(key,weight * 100 / total);
        }
        return perMap;
    }

    /**
     * map 按照value排序
     *
     * @return
     */
    public static  List> sortMapByValue(Map map, final int sort) {
        List> orderList = new ArrayList>(map.entrySet());
        Collections.sort(orderList, new Comparator>() {
            @Override
            @SuppressWarnings("unchecked")
            public int compare(Map.Entry o1, Map.Entry o2) {
                return (((Comparable) o2.getValue()).compareTo(o1.getValue())) * sort;
            }
        });
        return orderList;
    }

    public static  Map> listToMap(List> list,Object idKey){
        Map> map=new HashMap>();
        for(Map item : list){
            map.put(item.get(idKey),item);
        }
        return map;
    }

    public static String listToString(List list,String delimiter){
        if(list==null || list.size()==0) return null;

        StringBuffer sb=new StringBuffer();
        for(int i=0;i removeDuplicate(List list){
        List tmpList=new ArrayList<>();
        for(String str : list){
            if(!tmpList.contains(str)){
                tmpList.add(str);
            }
        }
        return tmpList;
    }

    //全组合
    public static  List> combine(List list){
        List> retList=new ArrayList<>();
        int count=list.size();
        int nbit=1< itemList=new ArrayList<>();
            for(int j=0;j0) {
                retList.add(itemList);
            }
        }
        return retList;
    }

    //全组合,返回组合项为num的组合列表
    public static  List> combine(List list,int num){
        List> retList=new ArrayList<>();
        int count=list.size();
        int nbit=1< itemList=new ArrayList<>();
            for(int j=0;j List join(List listOne,List listTwo){
        List list=new ArrayList<>();
        for(T t1 : listOne){
            for(T t2 : listTwo){
                if(t1.equals(t2) && !list.contains(t1)){
                    list.add(t1);
                }
            }
        }
        return list;
    }

    public static  List union(List listOne,List listTwo){
        List list=new ArrayList<>();
        for(T t1 : listOne){
            if(!list.contains(t1)){
                list.add(t1);
            }
        }
        for(T t2 : listTwo){
            if(!list.contains(t2)){
                list.add(t2);
            }
        }
        return list;
    }

    public static void main(String[] args){
        combineTest();
    }

    private static void combineTest(){
        List items=new ArrayList<>();
        items.add("a");
        items.add("b");
        items.add("c");
        List> retList=combine(items);
        for(List itemList : retList){
            for (String item : itemList){
                System.out.print(item);
            }
            System.out.println();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy