如何"分组" Java中类属性的对象列表?

时间:2016-07-08 07:42:29

标签: java data-structures linked-list

我有一个包含节点的链表,每个节点都包含Flight对象 它有几个属性(Origin city,Destination city等)。

我想以类似于SQL的小组的方式构建一个列表;对于每个起源城市所有的航班及其起源

我已尝试将城市存储在列表中,但我不知道如何初始化它,NetBeans说我不能,而且我不知道该怎么做。

这是我到目前为止所做的,最后一部分是评论,因为我不知道如何正确实施它。

public void PrintList() {
    NodeFlight aux = start;
    List<String> Ciudades;

    while (aux != null) {
        //add to the list cities if current.getcity isn't in the list already??
        //current=next;
    }

    //loop for each element in the city list
    //current=start;

    //loop if current equals to the first element of the list?
    //print each element which origin city is equals to the city list current index
    //close first loop

    //next city? i++? whatever
    //close bigger loop?
}

我正在寻找的格式如下:

"Origin city"
    1 - "Flight1"
    2 - "Flight2"
    ...
    N - "FlightN"
"Origin city"
    1.........

等等。

2 个答案:

答案 0 :(得分:2)

使用 Java8 Streams API 这是一项非常简单的任务:流式传输您的航班,并使用Collectors.groupingBy收集它们。

我们假设您的航班如下:

List<Flight> flights = new LinkedList<>();
flights.add(new Flight("Zurich", "London"));
flights.add(new Flight("Madrid", "London"));
flights.add(new Flight("Madrid", "Rome"));
flights.add(new Flight("Zurich", "New York"));
flights.add(new Flight("New York", "London"));

查找往/返每个城市的航班:

Map<String, List<Flight>> flightsFromEachCity = 
    flights.stream().collect(Collectors.groupingBy(Flight::getCityFrom));

Map<String, List<Flight>> flightsToEachCity = 
    flights.stream().collect(Collectors.groupingBy(Flight::getCityTo));

测试:打印每个城市的出境和入境航班

System.out.println("OUTGOING:");
for (String city : flightsFromEachCity.keySet()) {
    String cities = flightsFromEachCity.get(city).stream().map(Flight::getCityTo).collect(Collectors.joining(" and "));
    System.out.println("- from " + city + " to " + cities);
}
System.out.println("INCOMING:");
for (String city : flightsToEachCity.keySet()) {
    String cities = flightsToEachCity.get(city).stream().map(Flight::getCityFrom).collect(Collectors.joining(" and "));
    System.out.println("- from " + cities + " to " + city);
}

输出:

  

OUTGOING:
   - 从纽约到伦敦
   - 从马德里到伦敦和罗马    - 从苏黎世到伦敦和纽约   来电:
   - 从苏黎世到纽约    - 从马德里到罗马    - 从苏黎世和马德里到纽约到伦敦

答案 1 :(得分:-1)

看起来您想要对自定义对象的列表进行排序。我做了一些假设我没有创建任何节点。我刚刚在一个简单的飞行物上做了我的操作。

public class Flight  implements Comparable<Flight>{
    private String sourceCity;
    private String destinationCity;

    public Flight(String sourceCity,String destinationCity)
    {
        this.sourceCity=sourceCity;
        this.destinationCity=destinationCity;
    }

    public String getSourceCity() {
        return sourceCity;
    }

    public String getDestinationCity() {
        return destinationCity;
    }


    @Override
    public int compareTo(Flight o) {
        return (this.sourceCity.compareTo(o.getSourceCity()));
    }


}



public static void main(String[] args) {

    Flight f1=new Flight("A","B");
    Flight f2=new Flight("B","C");
    Flight f3=new Flight("D","F");
    Flight f4=new Flight("AB","BC");
    Flight f5=new Flight("ZA","B");
    Flight f6=new Flight("YB","C");
    Flight f7=new Flight("C","ZA");
    Flight f8=new Flight("B","BC");
    Flight f9=new Flight("A","BB");
    Flight f10=new Flight("B","CE");
    Flight f11=new Flight("D","ZSA");
    Flight f12=new Flight("B","BVC");

    List<Flight> flightList=new ArrayList<Flight>();
    flightList.add(f1);
    flightList.add(f2);
    flightList.add(f3);
    flightList.add(f4);
    flightList.add(f5);
    flightList.add(f6);
    flightList.add(f7);
    flightList.add(f8);
    flightList.add(f9);
    flightList.add(f10);
    flightList.add(f11);
    flightList.add(f12);

    Collections.sort(flightList);

    for(Flight f:flightList)
    {
        System.out.println("Source city= "+f.getSourceCity()+" Destination city= "+f.getDestinationCity());
    }


}

代码的输出将是

Source city= A Destination city= B
Source city= A Destination city= BB
Source city= AB Destination city= BC
Source city= B Destination city= C
Source city= B Destination city= BC
Source city= B Destination city= CE
Source city= B Destination city= BVC
Source city= C Destination city= ZA
Source city= D Destination city= F
Source city= D Destination city= ZSA
Source city= YB Destination city= C
Source city= ZA Destination city= B

我认为它会对你有帮助..