1

我有一个机场的乘客列表,每个乘客都有一个目的地,我想知道出现频率更高的目的地。

该列表是java中的一个ArrayList。

4

2 回答 2

1

类似于(在伪代码中)的东西:

Map<Destination, Integer> map = new HashMap<>();
for(Passenger passenger : list) {
  Destination dest = passenger.getDestination();
  int times = 0;
  if(map.hasKey(dest)) {
    times = map.get(dest);
  }
  map.put(dest, ++times);
}
Collections.sort(map.values());
于 2012-12-16T20:35:06.300 回答
0

试试下面只用一个 for 循环优化的代码。

 package com.rais;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.rais.vo.Passenger;

/**
 * @author Rais.Alam
 * @date Dec 17, 2012
 */
public class Client
{

    public static void main(String[] args)
    {
        Client client = new Client();

    Passenger p1 = new Passenger("A", "USA");
    Passenger p2 = new Passenger("B", "USA");
    Passenger p3 = new Passenger("C", "UK");
    Passenger p4 = new Passenger("D", "UK");
    Passenger p5 = new Passenger("E", "UK");
    Passenger p6 = new Passenger("F", "UK");

    List<Passenger> passengers = new ArrayList<Passenger>();
    passengers.add(p1);
    passengers.add(p2);
    passengers.add(p3);
    passengers.add(p4);
    passengers.add(p5);
    passengers.add(p6);

    System.out.println(client.getMostVistedDestination(passengers));

}

public String getMostVistedDestination(List<Passenger> passengers)
{

    Map<String, Integer> tempMap = new HashMap<String, Integer>();

    int maxCount = 0;
    String dest = "";

    for (Passenger passenger : passengers)
    {
        int count = 1;
        if (tempMap.containsKey(passenger.getDestination()))
        {
            count = tempMap.get(passenger.getDestination()) + 1;
            if (maxCount < count)
            {
                maxCount = count;
                dest = passenger.getDestination();
            }

        }
        else if (maxCount == 0)
        {
            maxCount = count;
            dest = passenger.getDestination();

        }

        tempMap.put(passenger.getDestination(), count);

    }

    return dest;

}

}

这是Passenger.java

package com.rais.vo;

/**
 * @author Rais.Alam
 * @date Dec 17, 2012
 */
public class Passenger
{
    private String name;
    private String destination;

    /**
     * @param name
     * @param destination
     */
    public Passenger(String name, String destination)
    {
        super();
        this.name = name;
        this.destination = destination;
    }

    /**
     * @return the name
     */
    public String getName()
    {
        return name;
    }

    /**
     * @param name
     *            the name to set
     */
    public void setName(String name)
    {
        this.name = name;
    }

    /**
     * @return the destination
     */
    public String getDestination()
    {
        return destination;
    }

    /**
     * @param destination
     *            the destination to set
     */
    public void setDestination(String destination)
    {
        this.destination = destination;
    }

}
于 2012-12-17T10:03:54.140 回答