Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: vector

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question vector

    Hai

    Can anybody help me out to write a java program which identifies a duplicate value in a Vector
    please send me the code along with the examples.

    links are also welcomed

    Thanks in Advance
    Srinivas Kundan


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: vector

    Vector as in java.util.Vector<E> ?

    Have a google at it, its not that hard.

    // Json

  3. #3
    Junior Member
    Join Date
    Aug 2009
    Location
    San Fransisco
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: vector

    Yeah pretty simple, still

    Vector<String> strVect = new Vector<String>();

    for(String str : strVect) {
    if(strVect.contains(str)) {
    return true;
    }
    }

    return false;
    Acumen
    Lucid forums

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: vector

    That for loop seems redundant to me, you might as well remove that. By the way Vectors are more or less also redundant as you have ArrayList these days. Only reason for the Vector might be concurrency but then again it's not fully thread safe using Vector anyways.

    // Json

  5. #5
    Junior Member
    Join Date
    Aug 2009
    Location
    San Fransisco
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: vector

    Hi Json,

    Appreciate you pointing that. I totally understand that I am doing in O(n^2) and we can do using hashing in O(n) space and O(n) time OR O(nlogn) time and constant space by sorting. But that is all algorithms. Looking at the question, it clearly shows the person who asked question must be new to Java, so I replied trivial solution.
    In fact I think you can do it by just exchanging across the data structures in Java also I guess.

    Nice to met you Json.

    Acumen
    Lucid forums

  6. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: vector

    Nice to meet you too.

    I understand what you did but as well as teaching people how to do things its also nice to comment on bad practices I believe.

    // Json

  7. #7
    Junior Member
    Join Date
    Aug 2009
    Location
    San Fransisco
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: vector

    Totally correct, please don't get me wrong Just explaining reason why I replied that way. Feedback of any kind is always welcome, that is the way we grow as people

  8. #8
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: vector

    Agree

    // Json

  9. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: vector

    whenever you try to add an element to this hash table, it will return false if a duplicate is found. Note that a duplicate is compared by .equals method of Object. Should be almost O(n) running time, unless there are large clusters.
    public class HashTable<E>
    {
        int                     size;
        ArrayList<ArrayList<E>> hash;
     
        /**
         * Creates an empty hash table with a given size
         * 
         * @param size
         */
        public HashTable (int size)
        {
            this.size = size;
            hash = new ArrayList<ArrayList<E>>();
            hash.ensureCapacity(size);
            for (int i = 0; i < size; i++)
            {
                hash.add(new ArrayList<E>());
            }
        }
     
        /**
         * Adds an element to the hash table.<br>
         * 
         * @param element
         * @return true if successfully added, false otherwise (duplicate)
         */
        public boolean add (E element)
        {
            if (hash.contains(element))
            {
                    return false;
            }
            hash.get(hashFunction(element.toString())).add(element);
            return true;
        }
     
        /**
         * Determines if an element is in the hash table.
         * 
         * @param element
         * @return true if found, false otherwise
         */
        public boolean contains (E element)
        {
            // get a list of those that hash to the same value
            ArrayList<E> list = hash.get(hashFunction(element.toString()));
            for (int i = 0; i < list.size(); i++)
            {
                // try to find the element from the list
                if (list.get(i).equals(element))
                {
                    return true;
                }
            }
            // didn't find element
            return false;
        }
     
        /**
         * Gets an element from the hash, if it exists.<br>
         * If duplicates (toString() matches), the first is returned.
         * 
         * @return the item (null if none)
         */
        public E get (E element)
        {
            // get a list of those that hash to the same value
            ArrayList<E> list = hash.get(hashFunction(element.toString()));
            for (int i = 0; i < list.size(); i++)
            {
                // find one who's to string matches, remove and return
                if (list.get(i).equals(element))
                {
                    return list.get(i);
                }
            }
            // didn't find any, return
            return null;
        }
     
        /**
         * Removes an element from the hash, if it exists.<br>
         * If duplicates (toString() matches), the first is removed.
         * 
         * @return the removed item (null if none)
         */
        public E remove (E element)
        {
            // get a list of those that hash to the same value
            ArrayList<E> list = hash.get(hashFunction(element.toString()));
            for (int i = 0; i < list.size(); i++)
            {
                // find one who's to string matches, remove and return
                if (list.get(i).equals(element))
                {
                    return list.remove(i);
                }
            }
            // didn't find any, return
            return null;
        }
     
        /**
         * @return the size of the hash table array
         */
        public int size ()
        {
            return size;
        }
     
        /**
         * Takes a string representation of an object and hashes it.
         * 
         * @param str
         * @return
         */
        private int hashFunction (String str)
        {
            int hashval = 0;
            for (int i = 0; i < str.length(); i++)
            {
                hashval = 37 * hashval + str.charAt(i);
            }
            hashval %= size;
            if (hashval < 0)
                hashval += size;
            return hashval;
        }
    }

  10. The Following User Says Thank You to helloworld922 For This Useful Post:

    JavaPF (August 12th, 2009)