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 12 of 12

Thread: Java SetsAndMaps.java

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Java SetsAndMaps.java

    I was looking for some help on one of my projects. I believe the first half is correct, but there are some complications in my second one. My professor is not giving me the help I need, any and all help will be much appreciated. It must be something small i'm missing.


    1. Write a static method sortAndRemoveDuplicates that accepts a list of integers as its parameter and
    rearranges the list's elements into sorted ascending order, as well as removing all duplicate values from
    the list. For example, the list {7, 4, -9, 4, 15, 8, 27, 7, 11, -5, 32, -9, -9} would become {-9, -5, 4, 7, 8,
    11, 15, 27, 32} after a call to your method. Use a Set as part of your solution.
    2. Write a static method is1to1 that accepts a Map<String, String> as its parameter and returns true if the
    two keys map to the same value. For example, {Darwin=206-9024, Hawking=123-4567, Newton=123-
    4567, Smith=949-0504} should return false, but {Darwin=206-9024, Hawking=555-1234, Newton=123-
    4567, Smith=949-0504} should return true. The empty map is considered 1-to-1 and returns true.


    < My code:
     
    import java.util.*;
    public class SetsAndMaps {
    	public static void main(String[] args) {
     
    		// Set <Integer>object2 = new TreeSet <Integer>();
     
    		ArrayList<Integer> object1 = new ArrayList<Integer>();
     
    		object1.add(7);
     
    		object1.add(4);
     
    		object1.add(-9);
     
    		object1.add(4);
     
    		object1.add(15);
     
    		object1.add(8);
     
    		object1.add(27);
     
    		object1.add(7);
     
    		object1.add(11);
     
    		object1.add(-5);
     
    		object1.add(32);
     
    		object1.add(-9);
     
    		object1.add(-9);
     
    		sortAndRemoveDuplicates(object1);
     
    		System.out.println(object1);
     
    		Set<Integer> object = sortAndRemoveDuplicates(object1);
     
    		System.out.println(object);
     
    		Map<String, String> people = new TreeMap<String, String>();
     
    		people.put("Darwin", "206-9024");
     
    		people.put("Hawking", "123-4567");
     
    		people.put("Newton", "123-4567");
     
    		people.put("Smith", "949-0504");
     
    		is1to1(people);
     
    		System.out.println(is1to1(people));
     
    	}
    	public static Set<Integer> sortAndRemoveDuplicates(
    			ArrayList<Integer> object1) {
    		Set<Integer> object = new TreeSet<Integer>(object1);
    		return object;
    	}
    	public static boolean is1to1(Map<String, String> people) {
    		Collection<String> names = people.values();
    		Iterator<String> iter = names.iterator();
    		while (iter.hasNext()) {
    			String element = iter.next();
    			while (iter.hasNext())
    			{
    				String nextElement = iter.next();
    				if (element == nextElement || names.isEmpty() == true) {
    					return true;
    				} else {
    					element = nextElement;
    				}
    			}
    		}
    		return false;
    	}
    }
    >
    Last edited by CrimaChronic; June 6th, 2012 at 08:35 PM. Reason: Formatting


  2. #2
    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: Java SetsAndMaps.java

    Please edit your post and wrap the code in code tags to preserve its formatting.

    Can you explain your problem and/or ask a specific java programming question?
    Show the programs output and explain what is wrong with it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java SetsAndMaps.java

    [7, 4, -9, 4, 15, 8, 27, 7, 11, -5, 32, -9, -9]
    [-9, -5, 4, 7, 8, 11, 15, 27, 32]
    true
    is the output.

    it's returning true when it should be returning false.
    For example, {Darwin=206-9024, Hawking=123-4567, Newton=123-
    4567, Smith=949-0504} should return false, but {Darwin=206-9024, Hawking=555-1234, Newton=123-
    4567, Smith=949-0504} should return true
    .

  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: Java SetsAndMaps.java

    Please edit your post, format the code and wrap the code in code tags to preserve its formatting. Also get rid of the extra blank lines.

    One problem with the code is that it uses == to compare Strings instead of the equals() method.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java SetsAndMaps.java

    I'm not very good with html stuff :C idk how to use tags
    I need == because I am comparing two things

  6. #6
    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: Java SetsAndMaps.java

    == compares reference pointers and primitives.
    equals() compares the contents of objects.

    Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting. It will look like this:
    <YOUR CODE HERE>
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java SetsAndMaps.java

    I've edited the post, so I should replace () with all the ==?

  8. #8
    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: Java SetsAndMaps.java

    I should replace () with all the ==
    Can you give an example of what you mean?

    You should use the equals() method to compare Strings, not the == operator.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java SetsAndMaps.java

    My goal: Write a static method is1to1 that accepts a Map<String, String> as its parameter and returns true if the
    two keys map to the same value. I believe I have this but it is returning true when it should be returning false. I do not know what I did wrong.

  10. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Java SetsAndMaps.java

    This thread has been cross posted here:

    http://www.java-forums.org/java-applets/60516-setsandmaps-java.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  11. #11
    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: Java SetsAndMaps.java

    returns true if the two keys map to the same value.
    I don't understand what that means, can you explain? A Map can have more than one key.

    What are the steps the code needs to take to make that decision?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Java SetsAndMaps.java

    This thread has been cross posted here:

    http://www.coderanch.com/t/583324/java/java/SetsAndMaps-java

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


Similar Threads

  1. Replies: 1
    Last Post: January 17th, 2012, 10:25 AM
  2. Java Web Start app exiting with access denie (java.lang.RuntimePermission
    By sonaljain in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 13th, 2011, 08:43 PM
  3. Replies: 10
    Last Post: April 5th, 2011, 09:09 AM
  4. Replies: 2
    Last Post: August 1st, 2010, 06:29 AM
  5. AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
    By nasi in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 25th, 2010, 10:37 PM