Home > front end >  How to shrink a Map in Java after deleting one of its elements (objects)?
How to shrink a Map in Java after deleting one of its elements (objects)?

Time:12-24

Let's assume I'm given a map represented as {[A, 0], [B, 1] [C, 2], [D, 3], [E, 4], [F, 5], [G, 6]} and were to delete one of its elements, i.e [E, 4], then my new map would become {[A, 0], [B, 1] [C, 2], [D, 3], [F, 5], [G, 6]}. How would I resize or shrink this map such that the elements whose index is greater than the index of the deleted element, would decrement (shrink) their index (Value) by one, like this {[A, 0], [B, 1] [C, 2], [D, 3], [F, 4], [G, 5]}? In addition to this, how would I adjust the size of ArrayList<ArrayList<GraphEdge<L>>> matrix?

Note: The assignment of the index to the nodes must follow the order of insertion, therefore the first node inserted in the graph must have assigned the index 0, the second the index 1 and so on.

The Java code:

@Override
public void removeNode(GraphNode<L> node) {
    if (node == null) {
        throw new NullPointerException();
    }
    if (!this.nodesIndex.containsKey(node)) {
        throw new IllegalArgumentException();
    }

   // Implement here

}

@Override
public boolean addNode(GraphNode<L> node) {
    if (node == null) {
        throw new NullPointerException();
    }
    if (this.nodesIndex.containsKey(node)) {
        return false;
    }

   // Implement here

}

The map which contains its elements (Keys) associated to their indexes (Values) is represented as following:

/* 
 * A set of nodes in which every node is associated to its index in the adjacency 
 * matrix
 */
protected Map<GraphNode<L>, Integer> nodesIndex = new HashMap<GraphNode<L>, Integer>();

Whereas the matrix is represented like this:

/*
 * The adjacency matrix in which its elements are null or objects of the class 
 * GraphEdge<L>. The use of ArrayList allows the matrix to resize 
 * gradually whenever a new object (element) is added or removed 
 * (deleted).
 */
protected ArrayList<ArrayList<GraphEdge<L>>> matrix = new ArrayList<ArrayList<GraphEdge<L>>>();

CodePudding user response:

You should not be using a Map for your purpose. The value part of a map’s entry pairing has nothing to do with the entry’s position within the map. Indeed, entries have no position within a Map!

If you want to track objects by a zero based index into the collection, use a List. Every list tracks additions by such an index.

List< Car > favoriteCars = new ArrayList<>(4) ;
favoriteCars.add( subaruBrz ) ;
favoriteCars.add( hondaElement ) ;
favoriteCars.add( jeepRenegade ) ;
favoriteCars.add( kiaSoul ) ;

Get second favorite car.

Car secondFavoriteCar = favoriteCars.get( 1 ) ;  // hondaElement.

But then we realize the Honda Element is no longer manufactured. So drop from our list.

favoriteCars.remove( hondaElement ) ; 

The index positions of the remaining elements move up to close the gap. Asking for second favorite car will now show the jeepRenegade object.

Car secondFavoriteCar = favoriteCars.get( 1 ) ;  // jeepRenegade. 

CodePudding user response:

Use a LinkedHashMap. LinkedHashMap are maps that also keep insertion order by instead of hashing the value, hashing a node in a linked list of values. You could then iterate over all keys in the map until you find the matching key and alter all values after that in the iteration.

  • Related