-3

I need to extract and use the departureTime and arrivalTime values stored inside of a linkedlist inside of a map for the next step in my assignment but I don't know the syntax or if it is even possible to extract just that set of information from the specific value in the map and store them as values or a new list. Is this possible or should I abandon this method and take a different route?

public Info (String destination, double departureTime, double arrivalTime){
        this.destination = destination;
        this.departureTime = departureTime;
        this.arrivalTime = arrivalTime;
}

public class Route {
    Map<String,LinkedList<Info>> airportFlightMap = new HashMap<>();
    LinkedList<Info> destinations;
    private int array[];
    private int arraySize;
    private int heapSize;

    public Route(int arraySize) {
        array = new int [arraySize];
        this.arraySize = arraySize;
        heapSize = 0;
    }

    public void fillHash(String origin, String destination, double departureTime, double arrivalTime) {
        if(airportFlightMap.containsKey(origin)){
            destinations = airportFlightMap.get(origin);
            destinations.add(new Info(destination, departureTime/100, arrivalTime/100));
        }
        else{
            destinations = new LinkedList<>();
            destinations.add(new Info(destination, departureTime/100, arrivalTime/100));
            airportFlightMap.put(origin, destinations);
        }
    }

2 Answers2

1

Entering something like this should fix it:

@Listener
public void sendPacket(EventSendPacket event) {
    if (event.getStage() == EventStageable.EventStage.PRE) {
        if (event.getPacket() instanceof CPacketPlayerDigging) {
            final Minecraft mc = Minecraft.getMinecraft();

            final CPacketPlayerDigging packet = (CPacketPlayerDigging) event.getPacket();
            if (packet.getAction() == CPacketPlayerDigging.Action.RELEASE_USE_ITEM) {
                if (mc.player.inventory.getCurrentItem().getItem() instanceof ItemBow && mc.player.getItemInUseMaxCount() >= 20) {
                    if (!mc.player.onGround) {
                        mc.player.connection.sendPacket(new CPacketPlayer.Position(mc.player.posX, mc.player.posY - 0.1f, mc.player.posZ, false));
                        mc.player.connection.sendPacket(new CPacketPlayer.Position(mc.player.posX, mc.player.posY - 10000f, mc.player.posZ, true));
                    }
                }
            }
        }
    }
}
Ryan M
  • 18,333
  • 31
  • 67
  • 74
SkylineXM
  • 11
  • 1
0

To iterate over a map you can check this question. There are multple ways to do it.

Now, your structure is like this:

|------|-------|
| key1 | value |
|------|-------|
| key2 | value |
|------|-------|
| ...  |  ...  |
|------|-------|
| keyN | value |
|------|-------|

Where key is an String and value is a LinkedList<Info>. So you can think in this:

|------|-------|
| key1 | value | --> [][][][]...[]
|------|-------|
| key2 | value | --> [][][][]...[]
|------|-------|
| ...  |  ...  | -->   . . .
|------|-------|
| keyN | value | --> [][][][]...[]
|------|-------|

Iterating through the map is like iterate over every key. And then you can get the key or the value.
At this point, with this code:

airportFlightMap.forEach((k,v)->System.out.println("k = "+k));

You will print the key in console. Also, note that forEach has two values: k and v, that are key and value. So you can get each value for each key. That's mean you can iterate over each item into the map.

And, how to get an specific value? Is as easy as call get() method:

LinkedList<Info> value = airportFlightMap.get("key1");

Using get() you can retrieve the value asociated with the key passed into paramater.

So you can using this to get the value you want.

Example here:

airportFlightMap.forEach((key,value)->{
    // key is an string
    System.out.println("k = "+key);
    // value is the LinkedList associated with the key
    System.out.println("v = "+value.size());
});

To get the values departureTime and arrivalTime you only have to iterate over every list looking for that.

airportFlightMap.forEach((key,value)->{
    for(Info i : value) {
        // Do whatever you want with these values
        i.getArrivalTime());
        i.getDepartureTime());
    }
});

Also you can use lambda expresions if you want, but I suposse these high order functions are not easy to understand for a begginer.

But you can check this

As code example, you can use filter to search for a value in the whole list without iterate over every position:

airportFlightMap.forEach((key,value)->{
    Optional<Info> i = value.stream().filter(v -> v.getDepartureTime()==3).findFirst();
    if(i.isPresent()) {
        //Exists into key
    }else {
        //No exists into key
    }
});
J.F.
  • 13,927
  • 9
  • 27
  • 65
  • Thank you. This seems to be returning every value chained after the key but is there any way to get inside of each index in the linked list and specifically take out the 2 values I need? – Ikaika Lee Nov 16 '20 at 21:56
  • Updated answer. – J.F. Nov 16 '20 at 22:10