Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中将数据从长格式更改为宽格式_Java_Transformation - Fatal编程技术网

如何在Java中将数据从长格式更改为宽格式

如何在Java中将数据从长格式更改为宽格式,java,transformation,Java,Transformation,我有一系列投票结果的列表(长格式),例如: List<String[]> results=new ArrayList<>(); Vote | User | Poll Yes | 121 | 1 //each poll is on its own line No | 123 | 1 Yes | 121 | 2 有人能解释一下怎么做吗?这里有一个方法可以重新格式化数据 这是假设您的数据是{[“是”、“121”、“1”]、…} 如果您的数据格式为{[“

我有一系列投票结果的列表(长格式),例如:

List<String[]> results=new ArrayList<>();

Vote | User | Poll
Yes  | 121  | 1     //each poll is on its own line
No   | 123  | 1
Yes  | 121  | 2

有人能解释一下怎么做吗?

这里有一个方法可以重新格式化数据

这是假设您的数据是
{[“是”、“121”、“1”]、…}

如果您的数据格式为
{[“投票”、“用户”、“投票”]、[“是”、“121”、“1”]、…},则可能需要进行一些小的调整


此函数首先计算
用户
轮询
集合

一旦知道总用户数(输出列表长度)和总轮询数(输出数组长度),它就可以将它们成对地匹配在一起并构建输出

List<String[]> format(List<String[]> input)
{
    List<String[]> output = new ArrayList<String[]>();
    Set<String> users = new HashSet<String>();
    Set<String> pollSet = new HashSet<String>();
    Map<String, String> data = new HashMap<String, String>();

    for(String[] row : input) //figure out how many users and polls there are
    {
        users.add(row[1]);
        pollSet.add(row[2]);
        data.put(row[1] + "_" + row[2], row[0]); //link user_poll to Yes/No data
    }

    String[] polls = pollSet.toArray(new String[0]); //make the set be an array for easier access
    Arrays.sort(polls); //sort the polls here if you want to

    for(String user : users) //loop over users, since each row is 1 user
    {
        String[] row = new String[polls.length + 1]; //each row is poll1,poll2,...,pollN,user
        row[row.length - 1] = user;

        for(int i = 0; i < polls.length; i++)
            row[i] = data.get(user + "_" + polls[i]); //retrieve the Yes/No data for user_poll, no data fills in null
            //alternative if you want "NULL" instead of null
            //if((row[i] = data.get(user + "_" + polls[i]) == null)
                //row[i] = "NULL";

        output.add(row); //add completed row to the output
    }

    return output;
}
列表格式(列表输入)
{
列表输出=新的ArrayList();
Set users=new HashSet();
Set pollSet=new HashSet();
映射数据=新的HashMap();
for(String[]row:input)//计算有多少用户和轮询
{
添加(第[1]行);
pollSet.add(第[2]行);
data.put(行[1]+“u”+行[2],行[0]);//将用户_轮询链接到是/否数据
}
String[]polls=pollSet.toArray(新字符串[0]);//将集合设置为数组以便于访问
Arrays.sort(polls);//如果需要,请在此处对轮询进行排序
for(String user:users)//循环用户,因为每行有1个用户
{
String[]行=新字符串[polls.length+1];//每行是poll1,poll2,…,pollN,user
行[row.length-1]=用户;
for(int i=0;i
投票关闭此项的人能否解释为什么不清楚?您是否只是在寻找一种转换特定数据的方法(使用更多字符串[3]条目)?(我没有投票通过)是的,我以前从未尝试过用Java进行统计,而且我在正确格式化数据时遇到问题。您是在寻找一种方法以该格式打印数据,还是更改ArrayList内容?更改ArrayList内容
List<String[]> format(List<String[]> input)
{
    List<String[]> output = new ArrayList<String[]>();
    Set<String> users = new HashSet<String>();
    Set<String> pollSet = new HashSet<String>();
    Map<String, String> data = new HashMap<String, String>();

    for(String[] row : input) //figure out how many users and polls there are
    {
        users.add(row[1]);
        pollSet.add(row[2]);
        data.put(row[1] + "_" + row[2], row[0]); //link user_poll to Yes/No data
    }

    String[] polls = pollSet.toArray(new String[0]); //make the set be an array for easier access
    Arrays.sort(polls); //sort the polls here if you want to

    for(String user : users) //loop over users, since each row is 1 user
    {
        String[] row = new String[polls.length + 1]; //each row is poll1,poll2,...,pollN,user
        row[row.length - 1] = user;

        for(int i = 0; i < polls.length; i++)
            row[i] = data.get(user + "_" + polls[i]); //retrieve the Yes/No data for user_poll, no data fills in null
            //alternative if you want "NULL" instead of null
            //if((row[i] = data.get(user + "_" + polls[i]) == null)
                //row[i] = "NULL";

        output.add(row); //add completed row to the output
    }

    return output;
}