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: Method Help?

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Method Help?

    How can I create a method that uses a HashSet and will return the unique characters in a string?
    for example
    unique("racecar") ----> "e"
    unique("confuse") ----> "confuse"


    Thank you.


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

    Default Re: Method Help?

    hmm... I don't think a HashSet is the best way to do this. Rather, I would use a Map (preferably a TreeMap). Use characters as the key, and an integer counter of how many times that character occurred. Then, iterate through all the characters of the String. See if the character is already in the Map. If it is, then simply increase the associated count. Otherwise, add a new key/value pair containing that character and an initial count of zero. At the end, simply iterate through all the items and throw out all characters which have a count not equal to 1.

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Method Help?

    Thank you for the input, however, I am still unsure as to what the actual code for this would look like. Can anyone provide some code for me?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Method Help?

    Look at the HashSet add method for one tool to use.

  5. #5
    Junior Member
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Method Help?

     
    import java.util.TreeMap;
     
     
    public class IFailAtJava {
    	public static void main(String[] args){
     
    	}
    	public void unique(String[] s){
    		String[] word = s;
    		TreeMap<String, Integer> map = new TreeMap<String, Integer>();
    		for(int i = 0; i < word.length; i++){
    			String key = word[i];
    			if(map.get(key) == null){
    				map.put(key, 1);
    			}
    			else{
    				int value = map.get(key).intValue();
    				value++;
    				map.put(key, value);
     
     
    			}
     
     
     
     
     
     
     
     
    			}
    		System.out.println(map);
     
     
    		}
     
     
     
    	}
    This is what I have so far, however I cannot figure out how to actually get the code to return the unique characters.

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

    Default Re: Method Help?

    Follow the last sentence:

    At the end, simply iterate through all the items and throw out all characters which have a count not equal to 1.
    Hint: use the iterator of the the TreeMap and a while loop. You can keep the same collection, or place all items left into a new collection (e.g. a List or a Set).

  7. #7
    Junior Member
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Method Help?

     
    import java.util.TreeMap;
     
    public class IFailAtJava {
    	public static void main(String[] args) {
     
    	}
     
    	public String[] unique(String[] s) {
    		String[] word = s;
    		TreeMap<String, Integer> map = new TreeMap<String, Integer>();
    		for (int i = 0; i < word.length; i++) {
    			String key = word[i];
    			if (map.get(key) == null) {
    				map.put(key, 1);
    			} else {
    				int value = map.get(key).intValue();
    				value++;
    				map.put(key, value);
     
    			}
    			for (int j = 0; j < word.length; j++) {
    				if (map.get(key) > 1) {
    					map.remove(key);
     
    				}
     
    			}
     
    		}
    		return word;
     
    	}
     
    }

    This is what I have added to the method, however I cannot get it to return anything without causing return type problems.

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

    Default Re: Method Help?

    Are you trying to find a unique set of strings, or a unique set of characters within a single string?

    Currently it looks like you're trying to do the first. Also, you're returning the original list of strings, not the left-over set. Also, you want the second loop to occur after the first, not inside. Use iterators to loop through the resultant map. If you want an array at the end, create an ArrayList and rather than explicitly removing items, just add the items with a count of 1 to that list, and call the toArray() method for the ArrayList (or change the return type of unique to return an ArrayList).

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Method Help?

    Look at using a second HashSet to save the dup entries discovered when adding to a first HashSet. After all strings added to the first, use the removeAll method to generate a unique list.

Similar Threads

  1. Can you pass parameters from one method into another method?
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 2
    Last Post: April 14th, 2011, 07:46 AM
  2. Help with toString method and an addObject method?
    By Camisado in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2011, 07:00 AM
  3. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM