bigdata

9. 二次排序

2018-12-28  本文已影响0人  百炼
package mapreduce;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;

public class DateTemperatureTest {
    public List<DateTemperature> dateTemperatureList;

    @Test
    public void test() {
        Map<String, List<DateTemperature>> result = new HashMap<>();
        dateTemperatureList.forEach(
                v -> {
                    List<DateTemperature> dateTemperatureList = result.get(v.getYearOfMonth());
                    if (dateTemperatureList == null) {
                        dateTemperatureList = new ArrayList<>();
                    }
                    dateTemperatureList.add(v);
                    result.put(v.getYearOfMonth(), dateTemperatureList);
                }
        );
        List<String> list = new ArrayList<>();
        Map<String,List<Integer>> tempMap = new HashMap<>();
        result.forEach((k, v) -> {
            v.sort(Comparator.comparing(DateTemperature::getYearOfMonth).thenComparing(DateTemperature::getTemperature));
            tempMap.put(k,v.stream().map(DateTemperature::getDay).collect(Collectors.toList()));
            list.add(k);
        });

        list.sort(String::compareTo);
        list.forEach(
                v -> {
                    System.out.println(v + ":" + tempMap.get(v));
                }
        );
    }

    @Before
    public void getWordsAsList() {
        String content;
        try {
            content = new String(Files.readAllBytes(Paths.get(this.getClass().getResource("/temp.txt").toURI())), StandardCharsets.UTF_8);
            final List<String> lines = Arrays.asList(content.split("\r\n"));
            dateTemperatureList = lines.stream().map(DateTemperature::form).collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @After
    public void tearDown() {

    }
}

上一篇下一篇

猜你喜欢

热点阅读