-2

I am trying to modify a list of elements in a Map by moving the selected items up or down. My idea is to remember the elements next to selected items. Because the elements are in a map, I find it much difficult to do it though I use a LinkedHashMap I would greatly appreciate if anyone can give me an idea about how to do it.

Note: I have generalized the question and below code is just a reference.

public void moveUp(ActionEvent evt) {
    List<String> selectedItems = myBean().getSelectedItems();
    if(selectedItems.isEmpty()) return;
    Map<String, String> avlbleItems = new LinkedHashMap<String, String>(myBean().getListBoxItems());
    List<String> avlbleItemsList = new ArrayList<String>(myBean().getListBoxItems().values());
    int size = avlbleItems.size();
    List<String> tail = new ArrayList();
    List<String> head = new ArrayList();
    int buf = 0,k = 0,j = 0,m = 0,n = 0;
    try {
        label:
        for (Iterator it = selectedItems.iterator(); it.hasNext();buf++) {
            String st = (String) it.next();
            if(buf==0) {
                for (int i = 0; i < size; i++) {
                    if(avlbleItemsList.get(i).equals(st)) {
                        k = i-1;
                        m = k+2;
                        n = m;
                    }
                }
                if(k < 0) return;
                if(k == 0) continue label; 
            }
            if(n < size && selectedItems.contains(avlbleItemsList.get(n))) {
                n++;
            }
            if(k+2 < size && !selectedItems.contains(avlbleItemsList.get(k+2))) {
                m++;
            }
            if(!it.hasNext()) {
                for (int i = 0; i < size; i++) {
                    if(avlbleItemsList.get(i).equals(st)) {
                        j = i+1;
                        if(j == size) break label;
                        if(j < size) {
                            tail = avlbleItemsList.subList(j, size);
                        }
                    }
                }
            }
        }
        avlbleItems.clear();
        for (Iterator ite = head.iterator(); ite.hasNext();) {
            String st = (String) ite.next();
            avlbleItems.put(st, st);
        }
        for (Iterator it = selectedItems.iterator(); it.hasNext();) {
            String st = (String) it.next();
            avlbleItems.put(st, st);
        }
        avlbleItems.put(avlbleItemsList.get(k), avlbleItemsList.get(k));
        for (int i = n; i < j-1; i++) {
            avlbleItems.put(avlbleItemsList.get(i), avlbleItemsList.get(i));
        }
        for (Iterator it = tail.iterator(); it.hasNext();) {
            String st = (String) it.next();
            avlbleItems.put(st, st);
        }
    } catch (IndexOutOfBoundsException e) {
        handleException();
        return;
    } catch (NoSuchElementException e) {
        handleException();
        return;
    } catch (IllegalArgumentException e) {
        handleException();
        return;
    }
}
Narayana Nagireddi
  • 729
  • 1
  • 13
  • 33

2 Answers2

2

Well, what you are trying to do with the Map is really best done as a Linked List (where you can move an element trivially up or down). I would suggest instead of all of this complicated machinery you make a simple class that extends a Linked List and has a HashMap as a member. Then you can override the add, delete, and clear methods so that they also add/delete/clears both the super linked list, and the HashMap.

This will get you both the benefits of the HashMap and a linked list, and conceptually will be easier to deal with.

greedybuddha
  • 7,488
  • 3
  • 36
  • 50
1

A Map has no concept of order. There is no "next to", "up" or "down" in a Map.

If you want an order-preserving Map you can use LinkedHashMap, which is based on insertion order, or define your own implementation that imposes whatever order you want. If you want order to be defined by the keys, then a TreeMap may be appropriate, but then if you change an item's key you would have to remove it and reinsert it.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190