0

With reference to the similar question. How to dynamically create Lists in java? I have quite a similar problem to the person asked.

In my project I have airplanes, airports and passengers for PNR record

The passenger destination airport is exported from database, but then I have to add it to a List of destination with name,airplanes.

So the database will contain like

A380, JFK, PersonA

A380, JFK, PersonB

A330, LHR, PersonC

My plan is to create the list of airport dynamically from the database and add those passenger with the same destination to it.

So currently I have peronsA and personB with JFK they will be in the same list. While in the LHR list only personC will be in LHR list, there will be hundreds of thousand record of such and hundreds of airport list to create, so I have to create such list dynamically.

But question is how do I determine if the the airport(JFK) list already exists and not create another list for that particular airport? Or is there another way to store information?

Pseudo code:

while (dbrecord =true){
    if(passenger.destination == "JFK"){
        List<passenger> JFK(dynamic)= new ArrayList<passenger>();
        JFK(dynamic).add(passenger)
    }
2
  • 2
    Looking at your code, I hope that this is for some "education assignment"; and not for a real product. Your code is buggy (are you sure that it should read = true, and not == true there, and worse you never ever compare strings using ==; you always use equals); and you are violating java coding style guidelines. Honestly: if this "for real"; than you better step back and spent the next weeks learning java basics; before you should even consider to touch a line of production code! Commented Aug 31, 2016 at 10:23
  • 1
    What is this mean if(passenger.destination == "JFK") it can be replaced by equals() Commented Aug 31, 2016 at 10:25

4 Answers 4

3

I'm not 100% sure if I understood the scenario, but the best way to avoid duplication is to use a Set structure such as HashSet

Sign up to request clarification or add additional context in comments.

2 Comments

Good to have this line in comment rather than as answer
I completely agree dear Jekin, but due to this points policy I'm not allowed to publish comments just answers :/
0

You could that by having a Map<String, List<Passenger>>.

Meaning: whenever you process a destination, you ask the map if it contains that destination. If yes, it already as a value which is a list; of not; you put a new List for that key, like:

Map<String, List<Passenger>> listsByDestination = new HashMap<>;

String destination = ... wherever
if (listsByDestination.contains(destination)) {
  ... already known 
} else {
  listsByDestination.put(destination, new ArrayList<>());
}

and so on.

Side not: I uppercased Passenger, because, well class names start UpperCase in Java.

2 Comments

the String destination can be like a loop from a list so that the list for each airport is create dynamically as well ?
That is exactly what my code does when you put it in a Loop.
0

Something like this might help you

Map<String, List<Passenger>> pasengerMaps =  new HashMap();
if(pasengerMaps.contains("JFK")){

pasengerMaps.get("JFK").add(passenger)

}else{

pasengerMaps.put("JFK",new ArrayList<Passenger>());

}

Comments

-1

I would use MultiMap from Google's guava library.

ListMultimap<String, Passenger> multimap = ArrayListMultimap.create();
multimap.put("destination",passenger1);
multimap.put("destination",passenger2);

You can retrive passenger list of some specific destination like this:

List<Passenger> passengerList = multimap.get("destination");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.