Results 1 to 4 of 4

Thread: [JAVA] TreeMap

  1. #1
    watle is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default [JAVA] TreeMap

    i need some help please. I write a treemap-code but the JUnit tests are not working.

    'code'
    package jpp.listtree;

    import java.util.Collection;
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.NavigableMap;
    import java.util.NoSuchElementException;
    import java.util.Set;
    import java.util.TreeMap;

    // TODO: Auto-generated Javadoc
    /**
    * The Class ListTree.
    *
    * @param <K> the Key
    * @param <V> the Value into the key
    * @author Sylvain F
    */
    /**
    * @author Sylvain
    *
    * @param <K>
    * @param <V>
    */
    public class ListTree<K extends Comparable<?>, V> {

    /** The data. */
    private TreeMap<K, List<V>> data = null;

    /** The size. */
    private int size = 0;

    /**
    * Constructor ListTree. new treemap
    */
    public ListTree() {

    data = null;
    size = 0;
    data = new TreeMap<K, List<V>>();
    clear();
    }

    /**
    * delete all data.
    */
    public void clear() {
    data.clear();
    size = 0;
    }

    /**
    * containsValue(V value) return value
    *
    * @param value
    * the value
    * @return true, if successful
    */
    public boolean containsValue(V value) {

    if (value == null) {
    return false;
    }
    Collection<List<V>> val = this.data.values();
    for (List<V> list : val) {
    if (list.contains(value)) {
    return true;
    }
    }

    return false;
    // return data.containsValue(value);
    }

    /**
    * containsKey(K k)return true if contain key
    *
    * @param k
    * the k
    * @return true, if successful
    */
    public boolean containsKey(K k) {

    if (k == null) {
    throw new NullPointerException();
    }

    return data.containsKey(k);
    }

    /**
    * public List<V> get (K k) return a list representation
    *
    * @param k
    * the k
    * @return the list
    */
    /**
    * public List<V> get(K k) {
    *
    * try{ if (containsKey(k) == true) return data.get(k); }
    *
    * catch(RuntimeException e){ e.printstacktrace(); }
    *
    * }
    */
    public List<V> get(K k) {

    if (k == null) {
    throw new NullPointerException();
    }

    if (containsKey(k) == true) {
    return data.get(k);
    }

    return null;
    }

    /**
    * getNumberOfEntries(K key) return a number of value into a key
    *
    * @param key
    * the key
    * @return the number of entries
    */
    public int getNumberOfEntries(K key) {

    if (key == null) {
    throw new NullPointerException();
    }

    if (!data.containsKey(key)) {
    throw new IllegalArgumentException();
    }

    return data.get(key).size();
    }

    /**
    * keySize()
    * @return thr number of different keys in the map
    */
    public int keySize() {

    // int keyNumber = 0, result = 0;
    //
    // while (keys().hasNext()) {
    // if (data.isEmpty())
    // keyNumber = 0;
    // keyNumber++;
    // }
    //
    // return result += keyNumber;
    return data.size();
    }

    /**
    * public int valueSize() return a number of values into a treemap
    *
    * @return the int
    */
    public int valueSize() {
    // size = data.size();
    // return size;
    return size;
    }

    /**
    * public V getFirstEntry() returns a firstentry into a tree
    * a minWert. The are many value with the same keys...
    * the value is the same as removeFirstEntry()

    * @return the first entry
    */
    public V getFirstEntry() {

    if (data.isEmpty()) {
    throw new NoSuchElementException();
    }

    NavigableMap<K, List<V>> nav = new TreeMap<K, List<V>>();
    nav = data;
    List<V> liste = nav.firstEntry().getValue();

    return firstIndexOf(liste);
    }

    // private V firstIndexOf(List<V> liste) {
    //
    // V firstIndexListe = firstIndexOf(liste);
    // return firstIndexListe;
    // }
    /**
    * First index of.
    *
    * @param liste
    * the liste
    * @return the v
    */
    private V firstIndexOf(List<V> liste) {

    return liste.get(0);
    }

    //
    // private V lastIndexOf(List<V> liste) {
    //
    // V lastIndexListe = lastIndexOf(liste);
    // return lastIndexListe;
    // }
    /**
    * Last index of.
    *
    * @param liste
    * the liste
    * @return the v
    */
    private V lastIndexOf(List<V> liste) {

    return liste.get(0);
    }

    /**
    * public V getLastEntry() return the last value the tree
    same as getFirstEntry()
    *
    * @return the last entry
    */
    public V getLastEntry() {

    if (data.isEmpty()) {
    throw new NoSuchElementException();
    }

    NavigableMap<K, List<V>> nav = new TreeMap<K, List<V>>();
    nav = data;
    List<V> liste = nav.lastEntry().getValue();

    return lastIndexOf(liste);
    }

    /**
    * public V removeFirstEntry()
    *
    * @return the v
    */
    public V removeFirstEntry() {
    V ergebnis = getFirstEntry();
    removeValue(ergebnis);
    return ergebnis;
    // if (data.isEmpty())
    // throw new NoSuchElementException();

    // return data.remove(getFirstEntry());
    }

    /**
    * public V removeLastEntry()
    *
    * @return the v
    */
    public V removeLastEntry() {
    V ergebnis = getLastEntry();
    removeValue(ergebnis);
    return ergebnis;

    // if (data.isEmpty())
    // throw new NoSuchElementException();
    //
    // return data.remove(getLastEntry());
    }

    /**
    * public Iterator<K> keys() Iterator from the keys into the tree
    *
    * @return the iterator
    */
    public Iterator<K> keys() {

    Iterator<K> it = keySet().iterator();
    return it;
    }

    /**
    * Return a Set
    *
    * @return the sets the
    */
    public Set<K> keySet() {
    return data.keySet();
    }

    /**
    * Add all entries into a map. the key will be overwrite
    *
    * @param t
    * the t
    */
    public void putAll(Map<? extends K, ? extends V> t) {

    for (Entry<? extends K, ? extends V> iterable_element : t.entrySet()) {
    put(iterable_element.getKey(), iterable_element.getValue());
    }
    // if (data.isEmpty())
    // throw new NullPointerException();

    // data.putAll(t);
    }

    /**
    * public void put(K key, V value)
    * @param key
    * the key of the given element
    * @param value
    * the value of the given element
    */
    public void put(K key, V value) {

    if (key == null || value == null) {
    throw new NullPointerException();
    }

    if (containsKey(key)) {
    data.get(key).add(value);
    } else {
    K key2 = key; // Create a key
    List<V> values = new LinkedList<V>(); // create a list values
    values.add(value);
    data.put(key2, values); // add a key-value pair
    }

    size++;
    }

    /**
    * public void remove(K key)
    *
    * @param key
    * the key
    */
    public void remove(K key) {

    if (key == null) {
    throw new NullPointerException();
    }

    while (containsKey(key)) {
    size -= data.get(key).size();
    data.remove(key);
    }

    }

    /**
    * public void removeValue(V value) .
    *
    * @param value
    * the value
    */
    public void removeValue(V value) {

    if (value == null) {
    throw new NullPointerException();
    }

    Collection<List<V>> val = this.data.values();
    for (List<V> list : val) {
    list.remove(value);
    size--;
    }

    Set<K> key = keySet();

    K[] keysarray = (K[]) key.toArray();

    for (int i = 0; i < keysarray.length; i++) {
    if (get(keysarray[i]).isEmpty()) {
    remove(keysarray[i]);
    }
    }

    // while (containsValue(value)) {
    //
    // data.remove(value);
    // }
    }

    /**
    * public void removeValue(K key, V value)
    *
    * @param key
    * the key
    * @param value
    * the value
    */
    public void removeValue(K key, V value) {

    if (key == null || value == null) {
    throw new NullPointerException();
    }

    if (this.containsKey(key)) {
    this.get(key).remove(value);
    size--;
    if (this.get(key).isEmpty()) {
    this.remove(key);
    }
    }

    // while (containsKey(key) && containsValue(value)) {
    // data.remove(value);
    // }
    }

    /**
    * public boolean isEmpty() .
    *
    * @return true if the map is empty, false if not
    */
    public boolean isEmpty() {

    return data.isEmpty();
    }

    /**
    * public void changeKey(K oldKey, K newKey, V value)
    *
    * @param oldKey
    * the old key
    * @param newKey
    * the new key
    * @param value
    * the value
    */
    public void changeKey(K oldKey, K newKey, V value) {

    if (oldKey == null || newKey == null || value == null) {
    throw new NullPointerException();
    }

    changeKey(oldKey, newKey);
    put(newKey, value);
    }

    /**
    * public void changeKey(K oldKey, K newKey)
    *
    * @param oldKey
    * the old key
    * @param newKey
    * the new key
    */
    public void changeKey(K oldKey, K newKey) {

    if (oldKey == null || newKey == null) {
    throw new NullPointerException();
    }

    if (containsKey(oldKey)) {
    return;
    }
    data.put(newKey, get(oldKey));
    data.remove(oldKey);
    }

    /**
    * The main method.
    *
    * @param args
    * the arguments
    */
    public static void main(String[] args) {
    ListTree<Integer, Integer> tree = new ListTree<Integer, Integer>();
    tree.clear();
    tree.clear();
    // 3=TestObject with value 406428945, 4=TestObject with value
    // 1332097454, 5=TestObject with value 1637266086, 11=TestObject with
    // value 811018944}
    // 0=TestObject with value 1562291931, 4=TestObject with value
    // 197135519, 14=TestObject with value 2047328007, 15=TestObject with
    // value 233442575}
    TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
    tree.put(5, 893106500);
    tree.put(17, 513495463);
    tree.put(0, 1344540848);
    tree.put(16, 1826376800);
    tree.put(0, 1863654889);
    tree.put(3, 1721586835);

    System.out.println(tree.valueSize());
    System.out.println(tree.keySize());

    tree.remove(17);
    System.out.println(tree.valueSize());
    System.out.println(tree.keySize());
    tree.remove(3);
    System.out.println(tree.valueSize());
    System.out.println(tree.keySize());
    tree.removeValue(0, 1344540848);
    System.out.println(tree.valueSize());
    System.out.println(tree.keySize());
    tree.removeValue(5, 893106500);
    System.out.println(tree.valueSize());
    System.out.println(tree.keySize());
    tree.removeValue(1826376800);

    System.out.println(tree.valueSize());
    System.out.println(tree.keySize());

    }
    }
    '/code'

    The JUnit advice:
    [echo] compiling functional tests...
    [javac] Compiling 2 source files to /tmp/reactor-a347492d-7358-4d64-b544-42764a506741/build/testclasses
    [javac] Note: Some input files use unchecked or unsafe operations.
    [javac] Note: Recompile with -Xlint:unchecked for details.
    [echo] executing required tests..
    [junit] Running tests.reqTests.TestListTree
    [junit] Testsuite: tests.reqTests.TestListTree
    [junit] Tests run: 2, Failures: 0, Errors: 2, Time elapsed: 0.094 sec
    [junit] Tests run: 2, Failures: 0, Errors: 2, Time elapsed: 0.094 sec
    [junit]
    [junit] Testcase: testMutatorPermutations took 0.081 sec
    [junit] Caused an ERROR
    [junit] [Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;
    [junit] java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;
    [junit] at jpp.listtree.ListTree.removeValue(ListTree.java:42 2)
    [junit] at tests.reqTests.TestListTree.removeValue(TestListTr ee.java:169)
    [junit] at tests.reqTests.TestListTree.performAction(TestList Tree.java:272)
    [junit] at tests.reqTests.TestListTree.testMutatorPermutation s(TestListTree.java:292)
    [junit]
    [junit] Testcase: firstLastEntryTest took 0.001 sec
    [junit] Caused an ERROR
    [junit] [Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;
    [junit] java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;
    [junit] at jpp.listtree.ListTree.removeValue(ListTree.java:42 2)
    [junit] at jpp.listtree.ListTree.removeFirstEntry(ListTree.ja va:277)
    [junit] at tests.reqTests.TestListTree.firstLastEntryTest(Tes tListTree.java:331)
    [junit]
    [junit] Test tests.reqTests.TestListTree FAILED

    BUILD FAILED


    thanks for helping me
    cheers
    Last edited by watle; 03-16-2011 at 10:14 AM.

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

    Default

    First I should you to use tag 'code' for formatter your code.
    Second I can't find a class for testing. Can you show this class?
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by watle View Post
    i need some help please. I write a treemap-code but the JUnit tests are not working.

    not the JUnit test is not working but this line of code:

    K[] keysarray = (K[]) key.toArray();

    the cast above is not possible. but what could work is the following

    Object[] arrObj = key.toArray();

    now you can handle the arrObj like an array.

  4. #4
    watle is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Treemap question
    By dave120 in forum New To Java
    Replies: 0
    Last Post: 10-13-2009, 02:31 AM
  2. Replies: 8
    Last Post: 07-22-2009, 01:16 AM
  3. Sorting Elements in a TreeMap
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 08:47 PM
  4. How to use treemap
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 08:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •