// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MovingAround {
    private static void move(List<Stuff> biglst, Stuff stf, int n, List<Stuff> newlst) throws Exception {
        for(int i = 0; i < n; i++) {
            int ix = biglst.indexOf(stf);
            if(ix >= 0) {
                newlst.add(biglst.remove(ix));
            } else {
                throw new Exception("Not enough of " + stf.getName());
            }
        }
    }
    private static void move(Map<Stuff, Integer> bigmap, Stuff stf, int n, Map<Stuff, Integer> newmap) throws Exception {
        int bign = bigmap.get(stf);
        if(bign >= n) {
            bigmap.put(stf, bign - n);
            if(newmap.containsKey(stf)) {
                newmap.put(stf, newmap.get(stf) + n);
            } else {
                newmap.put(stf, n);
            }
        } else {
            throw new Exception("Not enough of " + stf.getName());
        }
    }
    public static void main(String[] args) throws Exception {
        List<Stuff> biglst = new ArrayList<Stuff>();
        biglst.add(new Stuff("A"));
        biglst.add(new Stuff("BB"));
        biglst.add(new Stuff("BB"));
        biglst.add(new Stuff("CCC"));
        biglst.add(new Stuff("CCC"));
        biglst.add(new Stuff("CCC"));
        List<Stuff> newlst = new ArrayList<Stuff>();
        move(biglst, new Stuff("CCC"), 2, newlst);
        System.out.println("Big list:");
        for(Stuff stf : biglst) {
            System.out.println(stf);
        }
        System.out.println("New list:");
        for(Stuff stf : newlst) {
            System.out.println(stf);
        }
        Map<Stuff,Integer> bigmap = new HashMap<Stuff,Integer>();
        bigmap.put(new Stuff("A"), 1);
        bigmap.put(new Stuff("BB"), 2);
        bigmap.put(new Stuff("CCC"), 3);
        Map<Stuff,Integer> newmap = new HashMap<Stuff,Integer>();
        move(bigmap, new Stuff("CCC"), 2, newmap);
        System.out.println("Big map:");
        for(Map.Entry<Stuff,Integer> e : bigmap.entrySet()) {
            System.out.println(e.getKey() + " : " + e.getValue());
        }
        System.out.println("New map:");
        for(Map.Entry<Stuff,Integer> e : newmap.entrySet()) {
            System.out.println(e.getKey() + " : " + e.getValue());
        }
    }
}

class Stuff {
    private String name;
    public Stuff(String name) {
        super();
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public boolean equals(Object obj) {
        if (getClass() == obj.getClass()) {
            Stuff obj2 = (Stuff)obj;
            return name.equals(obj2.getName());
        } else {
            return false;
        }
    }
    @Override
    public int hashCode() {
        return (name == null) ? 0 : name.hashCode();
    }
    @Override
    public String toString() {
        return "[" + name + "]";
    }
}
