aspose file tools*
The moose likes Beginning Java and the fly likes how to compare elements of List Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to compare elements of List" Watch "how to compare elements of List" New topic
Author

how to compare elements of List

Kalabaw moo
Ranch Hand

Joined: May 26, 2009
Posts: 61
I am doing a homework, I created a program that will ask the user to input the elements of set A and B and provided a feature that will display the cartesian product of the two sets as well as the elements of both sets. I understand question four and five but I'm having a hard time thinking of an idea to do it in my program. I know the concept to determine if the given set is a function or not.


if I input:
set A = [ A, B, C ] are the elements
set B = [ 1, 2 ] are the elements

if I input this like what is asked in question 4:
(a, 1), ( b, 1), ( c, 2 ) - this is a function.

if I input this:
(a, 1), ( a, 2), (b,2) - this is not a function

If user chooses[4], the program asks the user to inputs a set and the program determines if it is a function. If a set has not been defined yet, the program should display the message “Sets are still undefined”.


The problem is how will I scan those elements to check if it's a function or not, what i'm tinking is to use a string tokenizer or just put them into an array and scan them one-by-one to determine if it's a function or not or is there any package in java that can compare the elements of list 1 to list 2?



Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

well,please search for java.lang.Comparable or java.util.Comparator also about HashMap keys and immutable objects
Kalabaw moo
Ranch Hand

Joined: May 26, 2009
Posts: 61
thanks, I'll check on that..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
To elaborate on what you have been told. The advice to use a Map sounds helpful. Maps don't use Comparators or Comparable, but do require equals() and hashCode() be correctly overridden. You can find out more about Maps if you look in the Java™ Tutorials; that section has at least two pages about Maps in. As previously suggested, HashMap is probably the best general-purpose Map going.
[edit]Additional: look at the links to "interfaces" and "implementations".[/edit]
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

true Campbell Ritchie
Kalabaw moo
Ranch Hand

Joined: May 26, 2009
Posts: 61
Wow, after reading the documentation of Hashmap I already formulated an idea on how to solve my problem. I'll try to read Map as well maybe it'll be easier to what I'm thinking right now... thanks everybody..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
I can think of any easy way to do it, too. Wonder whether we had the same idea.
Kalabaw moo
Ranch Hand

Joined: May 26, 2009
Posts: 61
I hope
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Campbell Ritchie wrote:I can think of any easy way to do it, too


what is the way?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
seetharaman venkatasamy wrote:what is the way?
The way Kalabaw Moo does it

And sorry for a misspelling earlier: I wrote "any easy" when I should have written "an easy".
Kalabaw moo
Ranch Hand

Joined: May 26, 2009
Posts: 61


This is the method I created using hashmap I guess this could be minimized but it's working as I expected. I can't use Map because I can't do duplication so I just used HashMap.
[edit]I didn't read the tutorial about HashMap I just looked on the documentation and used it. I presume I used HashMap the wrong way?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Never say == true or == false. It is if (foo.contains(...)) .... Saying == true or == false can cause nasty errors if you write = by mistake.

Look at the return value of the put method. I suggest your function should not permit null values; maybe you could use the older Hashtable class because it throws an Exception if you pass null.
Pass the Set to the method which tests it, create a new Mapand test the Set to see whether it is empty.
Put each member of the Set into the Map, and see what is returned. If you get a certain return value, you are all right. If you get the wrong return value, you know you are not dealing with a function, and can stop.

By the way, the Cartesian product of two sets where the domain Set has more than one member can never be a function.
Kalabaw moo
Ranch Hand

Joined: May 26, 2009
Posts: 61


I've changed the code a bit and followed what you've advised ( I don't know if I followed it 100% ). I guess the method I created is not smart enough to check if the sets input by the user is a function or not.


If user chooses [1], the program prompts for the elements of the sets:

How many elements for set A? 3
Element 1: A
Element 2: B
Element 3: C

How many elements for set B? 2
Element 1: 1
Element 2: 2

The sets are:
A= {A,B,C}
B={1,2}

If user chooses[2], the program displays the sets. If a set has not been defined yet, the program should display the message “Sets are still undefined”.

If user chooses[3], the program displays the Cartesian Product of set A and B. If a set has not been defined yet, the program should display the message “Sets are still undefined”.

If user chooses[4], the program asks the user to inputs a set and the program determines if it is a function. If a set has not been defined yet, the program should display the message “Sets are still undefined”.

If user chooses[5], the program asks the user to inputs a set and the program determines if it is a relation. If a set has not been defined yet, the program should display the message “Sets are still undefined”.


I was able to answer Question 1, 2 and 3.

I am assuming in Question 4 that the elements to be inserted by the user will come from the sets I defined in question 1 ( or maybe not? ) and it should display if it's a function. According to the article I've read I can only determine if it is a function if in one input there is only one output.

e.g. { A, 1 }, { A, 2 }, { B, 1 }, { C, 1 } <- I understand this is not a function because A has 2 output 1 and 2.



I created this condition to check if there is a duplicate input so if there's a duplicate input the program will assume that it's not a function otherwise a function but I don't know if what I thought is correct. ( I'm totally confused now haha! I guess I just have to clarify Question 4 where does the user will get the input? the user will choose from the sets defined in Question 1 or the user will just enter what he/she wants to form a set to know if it's a function or not?)

If the the user's input will depend on the set defined in Question 1 now I can't think of any idea on how to make my method smart enough to check if it's a function or not. any idea? I'll be reading more about Maps and HashTable while waiting for a reply here.. thanks..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Your question 4 only makes sense if you already have a Set available, so assume you are using a Set provided, or a Set created in an earlier question.
You are right; the Set you describe (or more precisely, the relation), is not a function because it contains (a, 1) and (a, 2).
Your method looks complicated, and I get suspicious whenever I see anything which looks complicated.
You are creating a new Map every time you call that method, so it will only never contain two pairs. I think the best way to sort out that problem is to pass the entire Set to the methodNote there is a fault in the Ranch causing < to be displayed as >< inside the CODE tags. I think I have corrected that misprint, however.

Don't write if (setsDefined() == false). Write if (!setsDefined()). I suspect the ! would be incorrect to test for an empty Set. Better option: set "isFunction" to be equal to a boolean value indicating whether the Set contains any members at all. You might find methods like "isFull" or "isEmpty" in the Set interface
As I already suggested, iterate through the Set, try adding the two halves of the Pair to the Map, then see what the Map#put method returns when you try entering pairs. If the key is already in the Map, you get a different value from when you add a new Key. You can test for those values and alter that value of isFunction accordingly.
If you include isFunction in the continuation condition for your loop, you will get better performance because the loop can be stopped if you encounter a key with two "values" anywhere. And once you find that, you know it is not a function. In the example you quoted, if you add (c, 3) as an additional pair at the end, you don't need to check that because you already have (a, 1) and (a, 2).

Note this method would not print out error messages. You simply return the boolean value and let some other method print the results.
Kalabaw moo
Ranch Hand

Joined: May 26, 2009
Posts: 61


I'm sorry, where did you get the Pair class?...

edit: oh, I guess I'm going to creat my own generic class Pair with two parameter type.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
I didn't create a Pair class, but thought a class for that sort of thing might well be called Pair.
Kalabaw moo
Ranch Hand

Joined: May 26, 2009
Posts: 61
Ok thanks, I'll be finishing my program tomorrow I have to finish something at work.
Kalabaw moo
Ranch Hand

Joined: May 26, 2009
Posts: 61
I have created a Pair class on my own but I don't have an idea on how to use it in my program.


Anyway, The program I created would ask me to input elements for Set A and Set B then it will produce the Cartesian product of the two sets

How many elements for Set A? 3
Enter element for Set A: A
Enter element for Set A: B
Enter element for Set A: C

How many elements for Set B? 2
Enter element for Set B: 1
Enter element for Set B: 2

Sets are now defined.
The cartesian product is: AXB = { A, 1}, { A, 2 }, { B, 1 }, { B, 2 }, { C, 1 }, { C, 2 }
==========================================================


Now I'm stuck with question 3: where will I get the set I'm going to input to determine the set if it is a function or not? from the cartesian product? or from Set A? or from Set B? same question with question 4.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You want the Pair to encapsulate the two bits of data you are passing. Good idea to make it a parameterised class, but I think it would be better as an immutable class.

Delete all the "set" methods.
Make both your fields "final".
Make the class "final".
Set both fields in the constructor.
Delete the no-args constructor.
Throw a NullPointerException if you try to pass a null argument to the constructor.Note I have added the three methods usually overridden from java.lang.Object. Also you have a nice immutable class, with one exception: Because you are using boxed classes or Strings, these are all immutable themselves, and can safely be returned. If you used mutable S and F, the "get" methods would have to return "defensive copies" of the S and F.

Now you were trying to put the Pairs into a Map, and get them from a Set. So you want a method something like thisNote the wonderful new feature of the Ranch software: it changes the names of classes to URLs, so if I write NullPointerException you get an automatic link to the API. Note it has started doing it in the code tags . Note I have forgotten the syntax for declaring a parameterised method, but is obvious from your earlier posts that you know enough generics to correct any such errors.

Now all you need to check is: What is the return type from the Map when you add a pair and it is a function, and what if it isn't a function.
Kalabaw moo
Ranch Hand

Joined: May 26, 2009
Posts: 61

This is my answer to question 1 - 3 can you please check if there's any correction.. I'm still working out on question 4 and 5..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Don't use == true or == false. I have already explained that.

No need to check whether a Set contains an element before adding it. Look at the Set#add(E) method, and note its return value carefully. Note you will have to get rid of the System.exit call if you get rid of the contains() calls.

You can add the entire contents of a List to a Set, I think. Look through the Set interface (same link as previous line) for methods.
You have some confusing names for your fields: a List called setA.

With those changes, I think your Cartesian product method looks correct. I presume you have tried it. Remember that you don't get the pairs in any particular order, unless you use something like a linked set or a set based on a tree, which do preserve ordering or sorting.

What follows depends on my having understood the question correctly: I might be mistaken about the question however.
You are going to have to create a Set representing the relation C×I, so you might do well to populate a Set as you iterate through the two for-each loops. You populate that Set with Pair objects (as previously seen) then use the Pair's toString method when printing the results.

I would remind you of what I said last week
By the way, the Cartesian product of two sets where the domain Set has more than one member can never be a function.
I still think you can never get a function out of a Cartesian product. But I made a dreadful mistake which nobody has noticed. I wrote "domain" when I ought to have written "range".
Kalabaw moo
Ranch Hand

Joined: May 26, 2009
Posts: 61
Sorry I forgot to edit that code I actually did your advice from your previous replies but that was applied to a different class not to that class. I wasn't using the Pair class just yet, I just got that idea in your previous post but don't know how to use it just yet. I don't actually need to sort the elements in any particular order I just want them to appear as they were input by the user so I used List. My friend told me that the elements of SET A must be unique so I can use the Set class but the problem it's not displaying the order the way the user entered the elements so I ended up using List. I edited the processCartesian to this:



I will update the code after work.

I'm confused about the question in question 4: What makes me confused is the part: ask the user to input a set isn't it referring to the defined set I just created in question 1 or I have to input ( define ) a new set?

If user chooses[4], the program asks the user to inputs a set and the program determines if it is a function. If a set has not been defined yet, the program should display the message “Sets are still undefined”.



I don't know if I understood Question 4 correctly, I was thinking that I will input a new set except the set I defined from question 1 to determine if it's a function, I guess it's wrong?

Or I will just pass the Set I defined from Question 1 to the method isFunction to determine if it's a function and I don't need to input a new set, I guess this is what the question really meant?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
I think you will have to define a Set to pass to question 4. As I said before, if you create a relation with the × operator (Cartesian product) and the domain or range or whichever I got wrong or got right has a cardinality > 1, then it won't be a function.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to compare elements of List