Wyciągnięcie wartości z Hashmapy

0

Cześć!
Mam taki problem:
stworzyłem hashmapę z odpowiednimi kluczami i przypisanymi do nich losowymi wartościami. Chciałbym zrobić tak, żeby stworzyć metodę na podmienianie wartości po kluczach, ktore będą losowo generowane potem. W internecie znalazłem tylko funkcję map.get(key), ale nie wiem jak to zaimplementować, żeby dobrze działało. Trzeba podać tam obiekt zamiast tego klucza, ale jak?

to fragment kodu:

public class BazaDanych {
HashMap<Integer, Integer> baza = new HashMap<Integer, Integer>();

void setBaza() {
    Random rand = new Random();
    for (int i = 0; i < 1000; i++) {
        baza.put(666666 + i, rand.nextInt(2000));
    }
}

void getBaza() {
    for (Map.Entry<Integer, Integer> entry : baza.entrySet()) {
        System.out.println(entry.getKey() + " " + entry.getValue());
    }
}

void podmienSaldo(int ileWyciagnac) {
    Map.Entry<Integer, Integer> ????????????????;
    baza.replace(?????????);
}

int getValue(){
?????????
}
}

Pomożecie?

2

baza.get(666667); (Czy z innym kluczem ) wyciągnie Ci to co chciałeś

0

Mam taki kod:
Class Customer extends BazaDanych{
@Override
void podmienSaldo(int entr, int ileWyciagnac) {
super.podmienSaldo(entr, ileWyciagnac);
}

@Override
int getSaldo(int klucz) {
    return super.getSaldo(klucz);
}

}
/**

  • Generate a stream of customers for 8.0 time units.
    /
    class Generator extends Event {
    Queue queue;
    /
    *

    • Create a new Customer. Add the customer to the queue and
    • schedule the creation of the next customer
      */
      void execute(AbstractSimulator simulator) {
      Customer customer = new Customer();
      queue.insert(simulator, customer);
      time += Random.exponential(8.0);
      if (time < 10.0) simulator.insert(this);
      }

    static class Random {

     static double exponential(double mean) {
         return - mean * Math.log(random());
     }
     static boolean bernoulli(double p) {
         return random() < p;
     }
    

    /* .. and other distributions */
    }
    }

class Queue {
/**
* Use the Java Vector to implement a FIFO queue.
/
private java.util.Vector customers = new java.util.Vector();
Server server;
/
*
* Add a customer to the queue.
* If the server is available (whic also implies this queue is empty),
* pass the customer on to the server.
* Otherwise add the customer to the queue.
/
void insert(AbstractSimulator simulator, Customer customer) {
if (server.isAvailable()) {
server.insert(simulator,customer);
} else {
customers.addElement(customer);
}
}
// -------------------------------------------------------???????????????????????????????
void podmien(Customer customer,int klucz ,int ileWyc){
customer.podmienSaldo(klucz, customer.getSaldo(klucz)-ileWyc);
}
Customer podmienn(Customer customer, int klucz, int ileWyc){
Customer customer1 = new Customer();
customer1.podmienSaldo(klucz, ileWyc);
//customer.podmienSaldo(klucz, customer.getSaldo(klucz)-ileWyc);
return customer1;
}
/
*
* @return the first customer in the queue
*/
Customer remove() {
Customer customer = (Customer) customers.firstElement();
customers.removeElementAt(0);
return customer;
}
int size() {
return customers.size();
}
}

/**

  • A server that holds a customer for an exponentially distributed amout of time
  • and releases it.
    /
    class Server extends Event {
    private Customer customerBeingServed;
    Queue queue;
    /
    *
    • The customer's service is completed so print a message.
    • If the queue is not empty, get the next customer.
      /
      void execute(AbstractSimulator simulator) {
      System.out.println("Finished serving " + customerBeingServed + " at time " + time);
      customerBeingServed = null;
      if (queue.size()>0) {
      Customer customer = queue.remove();
      insert((Simulator) simulator, customer);
      }
      }
      boolean isAvailable() {
      return (customerBeingServed == null);
      }
      /
      *
    • Start a customer's service. The simulator must be passed in
    • as a parameter so that this can schedule the time
    • when this server will be done with the customer.
      /
      void insert(AbstractSimulator simulator, Customer customer) {
      if (customerBeingServed != null) {
      /
      Should never reach here */
      System.out.println("Error: I am busy serving someone else");
      return;
      }
      customerBeingServed = customer;
      double serviceTime = 1.0;//Random.exponential(1.0);
      time = ((Simulator)simulator).now() + serviceTime;
      simulator.insert(this);
      }
      }

class BankSimulator extends Simulator {
public static void main(String[] args) {
new BankSimulator().start();
}
void start() {
events = new ListQueue();

    /* Create the generator, queue, and simulator */
    Generator generator = new Generator();
    Queue queue = new Queue();
    Server server = new Server();
    BazaDanych bazaDanych = new BazaDanych();

    /* Connect them together. */
    bazaDanych.setBaza(1000);

    generator.queue = queue;
    queue.server = server;
    server.queue = queue;

    /* Start the generator by creating one customer immediately */
    generator.time = 0.0;
    insert(generator);

    doAllEvents();
}

private void doAllEvents() {

}

}

I do tego bazę danych. Gdzie i jak mam wpisać fragment, który pozwalałby na wywołanie funkcji podmiany wartości w bazie danych, żeby to działało?
kompilator pokazuje mi NullPointerException w bazie danych:

public class BazaDanych {
HashMap<Integer, Integer> baza = new HashMap<Integer, Integer>();

void setBaza(int n) {
    Random rand = new Random();
    for (int i = 0; i < n; i++) {
        baza.put(666666 + i, rand.nextInt(2000));
    }
}
void getBaza() {
    for (Map.Entry<Integer, Integer> entry : baza.entrySet()) {
        System.out.println(entry.getKey() + " " + entry.getValue());
    }
}
int getSaldo(int klucz){
    return baza.get(klucz);
}

// static int entr, ileWyciagnac;

void podmienSaldo(int entr, int ileWyciagnac) {
    int saldo = getSaldo(entr);
    baza.replace(entr, saldo-ileWyciagnac);
}

}

1 użytkowników online, w tym zalogowanych: 0, gości: 1