Map vector in Java

Asked

Viewed 115 times

0

I have a data structure work in which I need to create an array and implement the interface Map of Java and, inside it, I store the ordered maps. I’ve already created the vector, but now I don’t know how to implement the method put (and others).

package vetor;

import java.util.Collection;
import java.util.Map;
import java.util.Set;
import estudante.Estudante;

public class Vetor_map implements Map<Object, Object> {


private int nElementos;
private Map mapa[];

public Vetor_map(int max) {
    nElementos = 0;
}

// PUT
@Override
public Object put(Object key, Object value) {
    if (!isFull()) {
        mapa[nElementos].put(key, value);
        nElementos++;
        return true;
    }
    return false;
}

public boolean isEmpty() {
    // TODO Auto-generated method stub
    if (nElementos == 0)
        return true;
    return false;
}

public boolean isFull() {
    if (nElementos == mapa.length) {
        return true;
    }
    return false;
}
//tem outros metodos abaixo mas nao implementei ainda

Down with main:

package main;


import estudante.Estudante;
import vetor.Chave;
import vetor.Vetor_map;

public class Main {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    
    Vetor_map vm = new Vetor_map(10);
    Chave ch = new Chave();
    Estudante es = new Estudante();
    System.out.println("Chave: " + ch + ", Estudante: "+ es);
    
    vm.put(ch, es);
    System.out.println(vm.get(ch));
    
}

}

Ao executar retorna NullPointer

I don’t find any example of similar implementation on the internet.

  • this occurs pq inside its Vetor_map class the map variable is not initialized

  • I fixed that part in the constructor and it follows the same error, I believe it is the put, but I do not know how to solve

  • you initialized the array maps as well? if you do for example Map[] = new Map[10], the array will be ok but the internal maps will still be null, you need then initialize the index map too, for example map[0] = new Hashmap(); and then yes give the put

1 answer

1


I don’t find any example of similar implementation on the internet

It’s because it doesn’t make any sense.

I want to make a few points here before I reply:

What you are doing (or asked to do) makes no sense, you are implementing the interface Map Java (will have to implement several methods) and is using a variable to control the index.
How you will get the value when passing a certain key?
At each insertion, you will have to initialize a new value-key structure. So, each key and value will have only one element internally.

But anyway, if it’s a schoolwork, it should be done, but I suggest reading the statement again and questioning the teacher.

Corrections you need to make.

public Vetor_map(int max) {
    nElementos = 0;
   // precisa inicializar o vetor
   // estamos alocando a quantidade de bytes necessária para um objeto do tipo hash map * 2
   mapa = new HashMap[2]; // pode ser qualquer tamanho
}


@Override
public Object put(Object key, Object value) {
    if (!isFull()) {
        // Você inicializou o vetor, mas não inicializou o que tem dentro do vetor
        mapa[nElementos] = new HashMap<>();
        mapa[nElementos].put(key, value);
        // Se precisa ordenar, poderá fazer após a inserção, sugiro o algoritmo INSERTION SORT.
        nElementos++;
        return true;
    }
    return false;
}
  • Thanks for the comments, I understand that does not make sense, and that maybe I did not understand the statement right, follows the same: Develop an application to manipulate students stored in a Map, where the number plate is used as key. The map TAD is represented by the interface Map available at https://docs.oracle.com/javase/7/docs/api/java/util/Map.html. This interface must be implemented by the pair or trio using three data structures: Vector (sorted), Double-chained list (sorted) and Binary Tree. How could I better interpret this advertised?

  • You need to implement a value key structure, and use a vector, chained list or binary tree to handle the collisions that will occur.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.