如何)排序使用.stream在库数据中按字母顺序(

约瑟夫·伯克:

我想创建()方法,该方法排序和输出数据我一直在使用.stream存储在库中按字母顺序。目前,我有一个数字顺序,我将在下面添加排序由CityID数据的方法。是否有一个适应它相同的数据,而是由城市名的字母顺序排序的方式?

CityID方法 -

private void listCityDataInCityIdOrder() {
    System.out.format("\033[31m%s\033[0m%n", "City Id Order");
    System.out.format("\033[31m%s\033[0m%n", "=============");
    repository.getItems()
            .stream()
            .sorted()
            .map(c -> c.toString())
            .forEach(str -> System.out.print(str));
}

数据集 -

1,"Cartagena","Spain",3
"2015",0.2,33,26,6,"S"
"2016",0.0,33,24,8,"SSW"
"2017",0.0,32,25,6,"E"
2,"Glasgow","Scotland",3
"2015",0.0,19,8,3,"SE"
"2016",0.1,21,11,6,"SE"
"2017",2.1,19,11,9,"SW"

城市模型类 -

package model;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

/**
 *
 * @author mga
 */
public class City implements Comparable<City>{
    private final int id;
    private String cityName;
    private String country;
    private List<YearData> yearDataCollection;

    private static int lastIdAllocated = 0;

    static final char EOLN='\n';
    static final String QUOTE="\"";

    public City() {
        this.id = ++lastIdAllocated;
        this.cityName = "TBC";
        this.country = "TBC";
        this.yearDataCollection = new ArrayList<>();
    }

    public City(String cityName, String country) {
        this.id = ++lastIdAllocated;
        this.cityName = cityName;
        this.country = country;
        this.yearDataCollection = new ArrayList<>();
    }

    public City(String cityName, String country, List<YearData> yearDataCollection) {
        this.id = ++lastIdAllocated;
        this.cityName = cityName;
        this.country = country;
        this.yearDataCollection = yearDataCollection;
    }

    public City(int id, String cityName, String country, List<YearData> yearDataCollection) {
        this.id = id;
        this.cityName = cityName;
        this.country = country;
        this.yearDataCollection = yearDataCollection;
        if (id > City.lastIdAllocated)
            City.lastIdAllocated = id;
    }


    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    // Methods required:

    public String getCityName() {
        return this.cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getCountry() {
        return this.country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public List<YearData> getYearDataCollection() {
        return this.yearDataCollection;
    }

    public void setYearDataCollection(List<YearData> yearDataCollection) {
        this.yearDataCollection = yearDataCollection;
    }

    public void addYearData(YearData yearData) {
        this.yearDataCollection.add(yearData);
    }


    @Override
    public String toString() {
        return "\nCity Id: " + id + " - City Name: " + cityName +
                " - Country: " + country + "\nData: " + yearDataCollection + "\n";
    }


    public String toString(char delimiter) {
        final char EOLN='\n';
        final String QUOTE="\"";
        String str = Integer.toString(this.id) + delimiter +
                QUOTE + this.cityName + QUOTE + delimiter +
                QUOTE + this.country + QUOTE + delimiter +
                Integer.toString(yearDataCollection.size()) + EOLN;
        for (YearData yearData : yearDataCollection) {
            str += yearData.toString();
        }
        return str;
    }

    public boolean equals(Object object) {
        if (this == object) return true;
        if (!(object instanceof City)) return false;
        if (!super.equals(object)) return false;
        City city = (City) object;
        return getId() == city.getId() &&
                java.util.Objects.equals(getCityName(), city.getCityName()) &&
                java.util.Objects.equals(getCountry(), city.getCountry()) &&
                java.util.Objects.equals(getYearDataCollection(), city.getYearDataCollection());
    }

    public int hashCode() {
        return Objects.hash(super.hashCode(), getId(), getCityName(), getCountry(), getYearDataCollection());
    }

    @Override
    public int compareTo(City compareCity) {

        int cityId =
                ((City) compareCity).getId();

        //ascending order
        return this.id - cityId;

        //descending order
        //return cityId - this.id;
    }


    public static Comparator<City> CityComparator = new Comparator<City>() {
        @Override
        public int compare(City city1, City city2) {

            String cityName1 = city1.getCityName();
            String cityName2 = city2.getCityName();

            //ascending order
            //return cityName1.compareTo(cityName2);

            //descending order
            return cityName2.compareTo(cityName1);
        }
   };

}
奥斯曼·d:

当然,改变你sorted到:

.sorted(Comparator.comparing(City::getCityName))

或使用lambda:

.sorted(Comparator.comparing(c -> c.getCityName()))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用自定义模型类按节按字母顺序对tableView中的数据排序?

如何使用python中的while循环按字母顺序排序?

按字母顺序对Pandas数据框中的数据进行排序

在熊猫数据框中按字母顺序排序

按字母顺序排序熊猫数据帧中列的子集

按字母顺序对结构中的数据进行排序

如何在数据透视表中按字母数字顺序对列数据进行排序?

如何按字母顺序对二维数组中的数据进行排序?

C ++从文件中获取数据时,使用strcpy和strcmp按字母顺序对数组排序?

使用 Django 中的标题按字母顺序对数据库进行排序

按字母顺序使用 JSON 数据对 HTML 表进行排序

如何使用jQuery按字母顺序对列表进行排序?

如何使用antd按字母顺序对表进行排序

如何使用python按给定的字母顺序排序列表?

如何使用Debian按数字(逻辑)升序但字母顺序排序

如何使用 QSortFilterProxyModel 按字母顺序对列表项进行排序?

如何使用Meteor按字母顺序对用户进行排序?

如何按字母顺序对 csv 数据进行排序,然后按列按数字排序?

如何在R中按字母顺序排序?

如何在Laravel中按字母顺序对记录进行排序

如何检查文件中的行是否按字母顺序排序

如何检查WebTable中的Webelements是使用Selenium WebDriver按字母顺序排序的?

如何使用javascript jquery按字母顺序对表中的默认td进行排序

如何使用gedit按字母顺序对文本文件中的行进行排序

如何使用NSArray按字母顺序对NSDictionary中的值进行排序?

如何使用python将保存在.txt文件中的乐谱和名称按字母顺序排序?

如何仅使用代码中的 if 语句将字符串按正确的字母顺序排序?

如何使用 jQuery 按字母顺序对 HTML 中的列表进行排序?

使用索引在 postgresql 中按字母顺序排序。