This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SetsAndMaps.java

 
Alec Balles
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Greg Charles
Sheriff
Posts: 2987
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alec, welcome to JavaRanch!

First some boring admin type stuff. Please use code tags when posting code, and also use proper indentation. That makes your code much easier to read. (I've added the code tags for you here.) Second, choose the right forum. You posted this in our Applets forum, but really it belongs in one of the regular Java forums. I'll move it to Beginning Java for you. Finally, you need to explain your problem. All you say is you think it's not correct, but why do you think that? Does it not compile? Is the output wrong? Be as specific as possible.

All these things will make it much more likely that the other ranchers here will read and respond to your question. Thanks!
 
Alec Balles
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean the tags?

and the output is
[7, 4, -9, 4, 15, 8, 27, 7, 11, -5, 32, -9, -9]
[-9, -5, 4, 7, 8, 11, 15, 27, 32]
true
it's returning false, when it should be returning true. Maybe i'm using == when I should be using ()? because I am comparing two things.
I'll repost this in the right section.
 
Darryl Burke
Bartender
Posts: 5132
11
Java Netbeans IDE Opera
 
Greg Charles
Sheriff
Posts: 2987
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first output shows your unsorted and unfiltered ArrayList, because your sortAndRemove... method creates a new object and returns that, but you don't do anything with the return value. The second time you call it, you do save the return value, a TreeSet, and print that out, so you get sorted and filtered results.

For your is1to1() method, you should be using equals(), not ==, because you're comparing objects. Also, you need to compare one value with all subsequent values, not just one. Don't use two while loops with the same iterator. Also, reverse your logic; you should return false if you find a match.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic