I'm trying to solve a problem from a textbook I'm using that has to do with cartesian products and sets without using built-in java APIs or any fancy function.
For example
Set A contains = {1,3,4}
Set B contains = {2,5}
their products would yield to this result {(1,2),(1,5),(3,2),(3,5),(4,2),(4,5)}
I have written some methods to perform various functions on each set but here is what I came up with. How could I implement this to the sets?
public String cartesian(Set other)
{
String result = "";
int res;
for ( int i = 0; i < this.size; ++i )
{
for ( int j = 0; j < other.size; ++j )
{
//System.out.println("#####"+ other.size);
//result = data[i] + ""+ other[i] + "";
//res = data[i] *= other.data[j];
}
}
return result;
}
The method returns the result as a string. My logic is to got through each set's element at the same time but I get stuck at thinking up a way to cross them together.
Here is the rest of my code.
public class Sets {
public static void main(String[] args)
{
Set set1;
set1 = new Set();
Set set2 = new Set();
set1.add(1);
set1.add(2);
set1.add(3);
set2.add(3);
set2.add(4);
/*set2.add(4);
set2.add(5);*/
//System.out.println(set1.difference(set2));
System.out.println(set1.cartesianReformed(set2));
}
}
User-defined Set class
class Set
{
private int[] data;
private int size;
public Set()
{
data = new int[20];
size = 0;
}
public void add(int value)
{
int[] copy;
//avoiding duplicates
if ( !in(value) )
{
if ( size > data.length )
{
copy = new int[data.length * 2];
System.arraycopy(data, 0, copy,0,data.length);
data = copy;
}
data[size] = value;
size++;
}
else
{
System.out.println("You are trying to insert a number that's already here ---> " + value);
}
}
public String toString()
{
String result = "{";
for(int i = 0; i < size; i++)
{
result += "" + data[i];
//Add a comma after all but the last item
if ( i < size - 1 )
{
result += ",";
}
}
result += "}";
return result;
}
public boolean in(int value)
{
boolean result = false;
for(int i = 0; i < size; i++)
{
if ( data[i] == value )
{
result = true;
}
}
return result;
}
public Set intersection(Set other)
{
Set result = new Set();
for ( int i = 0; i < size; ++i )
{
if ( other.in(data[i]) )
{
result.add(data[i]);
}
}
return result;
}
public boolean equals(Set other)
{
boolean result = false;
int count = 0;
for ( int i = 0; i < size; ++i ) //iterating over this
{
if ( other.in(data[i]) )
{
count++;
}
if ( count == size )
{
result = true;
}
}
return result;
}
public Set difference(Set other)
{
Set result = new Set();
for(int i = 0; i < size; ++i)
{
if ( !other.in(data[i]) )
{
result.add(data[i]);
}
}
return result;
}
public String cartesian(Set other)
{
String result = "";
int res;
for ( int i = 0; i < this.size; ++i )
{
for ( int j = 0; j < other.size; ++j )
{
//System.out.println("#####"+ other.size);
//result = data[i] + ""+ other[i] + "";
//res = data[i] *= other.data[j];
}
}
return result;
}
public Set union(Set other) {
Set result = (Set)other.clone();
for (int i = 0; i < size; i++) {
result.add(data[i]);
}
return result;
}
public Object clone() {
Set result = new Set();
for (int i = 0; i < size; i++) {
result.add(data[i]);
}
return result;
}
}
Something like this will work:
public String cartesian (Set other)
{
String [] cart = new String [this.size * other.size];
int k = 0;
for (int i : this.data)
{
for (int j : other.data)
{
cart[k++] = "(" + i + "," + j + ")";
}
}
return Arrays.toString(cart);
}
Returns:
[(1,2), (1,5), (3,2), (3,5), (4,2), (4,5)]
Note:
Naming your class Set is a bad idea since it conflicts with java.util.Set.
Let's get to the crux of the matter. You essentially need all the combinations of elements in 2 lists. The following is a very simple way of looking at it, by using nested for-loops to go over the elements in the sets A and B.
Set<Integer> A = new HashSet<Integer>();
Set<Integer> B = new HashSet<Integer>();
for(int i = 1 ; i < 5 ; i++)
A.add(i);
for(int i = 10 ; i < 13 ; i++)
B.add(i);
System.out.println("A: " + A);
System.out.println("B: " + B);
List<Set<Integer>> list = new ArrayList<Set<Integer>>();
for(Integer i: A) {
for(Integer j: B) {
Set<Integer> combination = new HashSet<Integer>();
combination.add(i);
combination.add(j);
list.add(combination);
}
}
System.out.println(list);
Related
In the code below I have a double for loop resulting in a time complexity of O^2 in method getResponse(). This code prompts the user for a 10 integer sequence string and an uppercase sensitive pin. It then converts the pin to numbers on a phone pad ie. [ABC] --> 2, [DEF] --> 3. Lastly a response array is generated with each digit of the new phone pin corresponding to indexes of sequence. So input "0123456789","HAM", response = "426"
import java.util.Scanner;
public class Test {
public static final int SEQ_DIGITS = 10;
public static final String ERR_SEQ = "Invalid sequence";
public static final String ERR_PIN = "Invalid PIN";
public static int letterToPhone(char c) {
int phoneNumber = 0;
if (Character.toString(c).matches("[ABC]")) {
phoneNumber = 2;
} else if (Character.toString(c).matches("[DEF]")) {
phoneNumber = 3;
} else if (Character.toString(c).matches("[GHI]")) {
phoneNumber = 4;
} else if (Character.toString(c).matches("[JKL]")) {
phoneNumber = 5;
} else if (Character.toString(c).matches("[MNO]")) {
phoneNumber = 6;
} else if (Character.toString(c).matches("[PQRS]")) {
phoneNumber = 7;
} else if (Character.toString(c).matches("[TUV]")) {
phoneNumber = 8;
} else if (Character.toString(c).matches("[WXYZ]")) {
phoneNumber = 9;
}
return phoneNumber;
}
public static int[] getResponse(String pin, int[] values) {
int[] response = new int[pin.length()];
for(int i = 0; i < pin.length(); i++) {
for (int j = 0; j < values.length; j++) {
int x = letterToPhone(pin.charAt(i));
if(x == j) {
response[i] = values[j];
}
}
}
return response;
}
public static boolean stringIsLengthK(String s, int k) {
boolean isLength = false;
if (s.length() == k) {
isLength = true;
}
return isLength;
}
public static boolean allDigits(String s) {
boolean isDigit = true;
for (int i = 0; i < s.length(); i++) {
if (!(Character.isDigit(s.charAt(i)))) {
isDigit = false;
break;
}
}
return isDigit;
}
public static boolean allUppercaseLetters(String s) {
boolean isUpper = true;
for (int i = 0; i < s.length(); i++) {
if (!(Character.isUpperCase(s.charAt(i)))) {
isUpper = false;
break;
}
}
return isUpper;
}
public static int[] digitStringToIntArray(String s) {
int[] arrayS = new int[s.length()];
for(int i = 0; i < arrayS.length; i++) {
for(int j = 0; j < SEQ_DIGITS; j++) {
if (((int) s.charAt(i) - 48) == j) {
arrayS[i] = j;
}
}
}
return arrayS;
}
public static int countValues(int value, int[] values) {
int count = 0;
for(int i = 0; i < values.length; i++) {
if(value == values[i]) {
count++;
}
}
return count;
}
public static int numPossible(int[] response, int[] values) {
int product = 1;
int[] count = new int[response.length];
for (int i = 0; i < count.length; i++) {
count[i] = countValues(response[i], values);
}
for(int i=0; i<response.length; i++){
product = product * count[i];
}
return product;
}
public static void main(String[] args) {
try (Scanner in = new Scanner(System.in)) {
System.out.printf("Enter value sequence: ");
final String seq = in.nextLine();
System.out.printf("Enter PIN: ");
final String pin = in.nextLine();
if (!(allUppercaseLetters(pin))) {
throw new AssertionError(ERR_PIN);
} else if (!(allDigits(seq)) || !(stringIsLengthK(seq, SEQ_DIGITS))) {
throw new AssertionError(ERR_SEQ);
}
int[] seqArray = new int[SEQ_DIGITS];
seqArray = digitStringToIntArray(seq);
int[] response = new int[SEQ_DIGITS];
response = getResponse(pin, seqArray);
System.out.printf("Response: ");
for (int i = 0; i < response.length; i++) {
System.out.printf("%d", response[i]);
}
System.out.printf("%n");
numPossible(response, seqArray);
} catch (Error e) {
System.out.println(e.getMessage());
}
}
}
I want to be to able to accommodate larger sequence numbers without a scaling of n^2. Is there a way to change the for loop to instead compare the int x = letterToPhone(pin.charAt(i)); value in getResponse() to a range of integers such as "[0-9]"
One easy optimization of constant factors is to move the call to letterToPhone() out of the inner loop.
And yes, you can compare the x value to a range, eliminating the need for the inner loop.
for(int i = 0; i < pin.length(); i++) {
int x = letterToPhone(pin.charAt(i));
if ( (0 <= x) && (x < values.length)) {
response[i] = values[x];
}
}
Another optimization of constant factors would be to replace all the function calls in letterToPhone() with a switch statement. The compiler may choose to optimize that into a table lookup.
I have to write a program that sorts names alphabetically while removing duplicates and counting the amount of times the names appear and capitalizes all of it. My partner and I have been working on this and have found no way to have the sorting method work properly and have the program find and count the times the names appear. We have to use certain methods to do this...which I linked the pdf down at the bottom. I really want to understand what's wrong and why the output is not coming out right.
public class Names {
/**
* #param args the command line arguments
*/
static ArrayList<String> fnArray = new ArrayList<String>();
static ArrayList<String> lnArray = new ArrayList<String>();
public static void main(String[] args) throws IOException {
// TODO code application logic here
getNames(fnArray, lnArray);
sort(lnArray);
find(fnArray,1);
capitalize(fnArray,lnArray);
}
public static void getNames(ArrayList<String> fn, ArrayList<String> ln) throws IOException {
Scanner kb = new Scanner(System.in);
System.out.println("What file would you like to read from ?: ");
String n = kb.next();
File inputFile = new File(n);
Scanner in = new Scanner(inputFile);
while (in.hasNext()) {
String firstName = in.next();
fn.add(firstName);
String lastName = in.next();
ln.add(lastName);
}
for (int i = 0; i < fnArray.size(); i++) {
System.out.println(lnArray.get(i) + " " + fnArray.get(i));
}
}
public static void capitalize(ArrayList<String> fnArray, ArrayList<String> lnArray) {
String capfn = " ";
String capln = " ";
int i = 0;
int j = 0;
System.out.println("****************Names***************");
while (i < fnArray.size() && j < lnArray.size()) {
capfn = fnArray.get(i);
capln = lnArray.get(j);
String capFname = capfn.substring(0, 1).toUpperCase() + capfn.substring(1).toLowerCase();
String capLname = capln.substring(0, 1).toUpperCase() + capln.substring(1).toLowerCase();
fnArray.set(i, capFname);
lnArray.set(i, capLname);
System.out.println(lnArray.get(j) + ", " + fnArray.get(i));
i++;
j++;
}
}
public static void display(ArrayList<String> names) {
for (int i = 0; i < names.size(); i++) {
System.out.println(names.get(i));
}
}
public static int find(String s, ArrayList<String> a) {
int count = 0;
for (String str : a) {
if (str.equalsIgnoreCase(s))
count++;
}
return count; }
public static void removeDuplicates(ArrayList<String> s) {
for (int j = 0; j < s.size(); j++) {
int i = -1;
while ((i = find(s, j)) >= 0) {
s.remove(i);
}
}
}
public static void backwards(ArrayList<String> names) {
for (int i = names.size() - 1; i > 0; i--) {
names.get(i);
for (int j = 0; j < names.size(); i++) {
if ((names.get(i).equals(names.get(j)))) {
names.remove(i);
}
}
}
}
public static void sort(ArrayList<String> array) {
for (int i = 1; i < array.size(); i++) {
// find the index of the ith smallest value
int s = i - 1;
for (int j = i; j < array.size(); j++) {
if (array.get(j).compareTo(array.get(s)) < 0) {
s = j;
}
}
// swap the ith smallest value into entry i-1
String temp = array.get(i - 1);
array.set(i - 1, array.get(s));
array.set(s, temp);
}
}
public static void showUnique(ArrayList<String> names){
System.out.println("Unique name list contains:");
for(int i=0 ;i< names.size() ;i++){
System.out.println(lnArray.get(i) + " " + fnArray.get(i));
}
}}
You can use the Collections.sort() method to sort an array list; once it is sorted, you will have entries like this:
ArrayList = { "Alpha", "Beta", "Beta", "Gamma", "Theta", "Theta" ... }
The important point to note, however, is that the duplicates will be next to each other in the sorted array.
Finally, if you want to remove duplicates, you can put all the elements of the ArrayList into a Set: set is a data-structure which removes duplicates.
Example:
Set<String> foo = new HashSet<String>( yourArrayList );
EDIT: Use this approach which is both: easy and simple-to-comprehend.
for( int i = 0; i < array.size() - 1; i++ )
{
for( int j = i + 1; j < array.size(); j++ )
{
if( array[i] > array[j] )
{
// Swap the contents of array[i] and array[j].
}
}
}
So I've been working on this BigNum multiplication method (in short the method takes in a BigNum other and is supposed to return the product of two large positive integers without using the bigint class) for a while and I'm almost done however, I am still having issues appending zeroes. My helper method does not seem to be adding correctly either (as an ex. 444*4 should return as "1776" however it returns as "161616".) I need someone to debug this and help me figure out why it isn't working. Any help is appreciated.
Here's the result I get when I try doing 444*444 as an example
the expected output should be:
1776
17760
177600
197136
actual output with my code:
161616
1616160
1616160
3393936
My methods
/**Multiplies two <tt>BigNum<tt> values together and returns a new
*<tt>BigNum<tt> object with the resulting value.
*
*#param other object
*#returns a new BigNum with resulting value
*/
public BigNum mult(BigNum other) {
BigNum tmp = new BigNum();
BigNum acc = new BigNum();
String s="";
int count=0;
for(int i= 0; i < other.num.length() ; i++) { //each digit x of other
tmp = this.mult(Character.getNumericValue(other.num.charAt(i)));
if(i > 0) {
for(int j=0; j < i; j++) {
s = tmp.num + "0";
}
}else {
s = tmp.num;
}
tmp=new BigNum(s);
count++;
acc = acc.add(tmp);
}
return acc;
}
/**Helper method that adds the other value a set of number of times, 0-9
*
*#param and int n and other object
*#returns resulting value
*/
public BigNum mult(int n) {
String result;
int carry;
if(n==0){
result="0";
}
else{
carry =0;
result = "";
}
for(int i=this.num.length()-1; i >=0; i--){
int temp = n * Character.getNumericValue(this.num.charAt(i))
result=(temp%10) + result;
carry = temp/10;
if(carry > 0){
result = carry + result;
}
}
return new BigNum(result);
}
Using String to implement BIGNUM multiply is rather slow, however it works.
Change your code as follows:
public BigNum mult(BigNum other) {
BigNum tmp = new BigNum();
BigNum acc = new BigNum();
String s="";
int count=0;
for(int i= 0; i < other.num.length() ; i++) { //each digit x of other
tmp = this.mult(Character.getNumericValue(other.num.charAt(i)));
if(i > 0) {
s = tmp.num;
for(int j=i; j > 0 ; j--) {
s = s + "0";
}
}else {
s = tmp.num;
}
tmp=new BigNum(s);
count++;
acc = acc.add(tmp);
}
return acc;
}
public BigNum mult(int n) {
String result;
int carry;
if(n==0){
result="0";
}
else{
carry =0;
result = "";
}
for(int i=this.num.length()-1; i >=0; i--){
int temp = n * Character.getNumericValue(this.num.charAt(i));
// add carry first
carry = temp/10;
if(carry > 0){
int lastnum=(result.length()==0)?0:
Character.getNumericValue(result.charAt(result.length()-1));
lastnum=lastnum+carry;
result = (result.length()==0)?"":result.substring(0, result.length()-1); // remove the last num
result = result + lastnum;
}
result= result + (temp%10);
}
return new BigNum(result);
}
Next time you should also paste your add() method. Here's my implementation of add(), if anyone's interested (pretty ugly I have to say):
private BigNum add(BigNum other) {
StringBuilder sb = new StringBuilder();
int sum, carry=0;
String large, small;
if(this.num.length()>=other.num.length()) {
large=this.num; small=other.num;
} else {
large=other.num; small = this.num;
}
int len = Math.min(this.num.length(), other.num.length());
for(int i=len-1; i>=0; i--) {
sum = Character.getNumericValue(large.charAt(large.length()-len+i)) +
Character.getNumericValue(small.charAt(i)) +
carry;
carry=(sum>=10)?1:0;
sb.insert(0, String.valueOf(sum).charAt((sum>=10)?1:0));
}
if(large.length()==small.length()) {
if(carry==1) sb.insert(0, 1);
} else {
sum = Character.getNumericValue(large.charAt(large.length()-(len+1)))+carry;
sb.insert(0, String.valueOf(sum).charAt(0));
}
for(int i=large.length()-(len+2); i>=0; i--) {
sb.insert(0, large.charAt(i));
}
num = sb.toString();
return this;
}
All of these methods goes into this BigNum class:
public class BigNum {
String num;
public BigNum() {
num=new String();
}
public BigNum(String s) {
this.num=s;
}
... methods here...
}
Use the following logic: 5 * 2 = 5 + 5 + 5 + 5 + 5
public class BigNum {
int value;
public BigNum(int value) {
this.value = value;
}
public BigNum mult(BigNum other) {
int result = 0;
for (int i = 0; i < value; i++) {
result += other.getValue();
};
return new BigNum(result);
}
public int getValue() {
return value;
}
#Override
public String toString() {
return "BigNum [value=" + value + "]";
}
public static void main(String[] args) {
System.out.println(new BigNum(444).mult(new BigNum(444)));
}
}
So, I made my own composite key in Java with 3 members
public class MyOwnKey{
int location;
int length;
String [] tokens;
}
Now I create two objects using the constructor
String [] tokens = "Stackoverflow is great".split("\\s+");
Object key1 = new MyOwnKey(0,0,tokens)
tokens = "Web is great".split("\\s+");
Object key2 = new MyOwnKey(0,0,tokens)
Now, I add the key in HashMap
HashMap map = new HashMap();
map.put(key1,1);
Now, this is the problem
when I do contains key, it gives false;
**map.containsKey(key2) //returns false whereas it should return true.**
Just so that it makes sense:
key1.equals(key2) returns true
and the hashcode codes are also equal. key1.hashCode() == key2.hashCode().
I have implemeneted my own version of hashCode, toEquals() and toCompare().
Not sure what's the problem.
Here is the code
import java.io.DataOutput;
import java.io.DataInput;
import java.io.IOException;
import org.apache.hadoop.io.WritableComparable;
public class PatternGeneratorKey implements WritableComparable<Object> {
private String [] tokens;
int location;
int length;
StringBuffer _toString = null;
public PatternGeneratorKey(){
tokens = new String[1];
location =0;
length=1;
}
public PatternGeneratorKey(int location, int length, String [] tokens){
this.location = location;
this.length = length;
this.tokens= new String[tokens.length];
for(int i = 0; i < tokens.length;i++){
this.tokens[i] = tokens[i];
}
}
public int compareTo(Object o) {
if (!(o instanceof PatternGeneratorKey))
return -1;
return this.compareTo((PatternGeneratorKey) o);
}
public void write(DataOutput out) throws IOException {
out.writeInt(tokens.length);
for(int i = 0; i<tokens.length;i++){
out.writeUTF(tokens[i]);
}
out.writeInt(location);
out.writeInt(length);
}
public void readFields(DataInput in) throws IOException {
int l = in.readInt();
tokens = new String[l];
for(int i = 0; i < l ; i++){
tokens[i] = in.readUTF();
}
location = in.readInt();
length = in.readInt();
}
public int compareTo(PatternGeneratorKey k) {
if(this.tokens.length - this.length != k.tokens.length - k.length){
return this.tokens.length - this.length -( k.tokens.length - k.length);
}
if(this.location != k.location){
return this.location - k.location;
}
int i = 0 , j= 0;
for(i = 0, j=0 ; i < this.tokens.length && j < k.tokens.length;){
if(i == this.location ){
i = i + length;
continue;
}
if( j == k.location){
j = j + k.length;
continue;
}
if(!this.tokens[i].equalsIgnoreCase(k.tokens[j])){
return this.tokens[i].compareTo(k.tokens[j]);
}else{
i++;
j++;
}
}
//TODO: add comparison on left out phrase
return 0;
}
public int hashCode() {
int hashCode=0;
for(int i = 0; i < tokens.length;){
if(i == location ){
i = i + length;
continue;
}
hashCode += tokens[i++].hashCode();
}
hashCode+= location + tokens.length;
return hashCode;
}
public String toString(){
if(_toString == null){
_toString = new StringBuffer();
for(int k = 0; k < tokens.length ;k++){
if(k==location){
_toString.append(":").append(" ");
k=k+length-1;
}else{
_toString.append(tokens[k]).append(" ");
}
}
}
return _toString.toString();
}
public boolean equals(PatternGeneratorKey k) {
if(this.tokens.length - this.length == k.tokens.length - k.length
&& this.location == k.location){
//assume second one is larger
String tokens[] = k.tokens;
int length = k.length;
int location = k.location;
String [] tokens1 = this.tokens;
int length1 = this.length;
int location1 = this.location;
//make the local variable point to the largest of the two
if( this.tokens.length > k.tokens.length){
tokens = this.tokens;
length = this.length;
location = this.location;
tokens1 = k.tokens;
length1 = k.length;
location1 = k.location;
}
int i = 0 , j= 0;
for(i = 0, j=0 ; i < tokens.length;){
if(i == location ){
i = i + length;
continue;
}
// if( j >= location1 && j<= location1 + length1 -1){
if( j == location1){
j = j + length1;
continue;
}
if(!tokens[i++].equalsIgnoreCase(tokens1[j++])){
return false;
}
}
return true;
}else{
return false;
}
}
}
And, this is the code I am testing on
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.hadoop.io.Text;
public class Test {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String value = "gm used cars";
// Compile all the words using regex
String[] tokens = value.toString().split("\\s+");
//to find pattern we need atleast two words in the query
if(tokens.length <=1){
return;
}
Map<PatternGeneratorKey,List> map = new HashMap<PatternGeneratorKey, List>();
for(int l = 1 ; l < tokens.length; l++){
for(int i = 0 ; i < tokens.length - (l-1); i++){
String hit = new String(getPhrase(l, i, tokens));
PatternGeneratorKey key1 = new PatternGeneratorKey(i, l, tokens);
List list = null;
for(int k = 0;k< tokens.length;k++){
System.out.println("i:" + i + ",l:" + l + ",tokens:" + tokens[k]);
}
System.out.println("hashcode:" + key1.hashCode());
if(!map.containsKey(key1)){
list = new ArrayList<String>();
map.put(key1, list);
}else{
list = (List) map.get(key1);
}
list.add(hit);
}
}
value = "ford used cars";
String[] tokens2= value.toString().split("\\s+");
PatternGeneratorKey key2 = new PatternGeneratorKey(0, 1, tokens);
//run a sliding window for length 1 to tokens length -1
for(int l = 1 ; l < tokens2.length; l++){
//genereate token pairs with sliding window.
for(int i = 0 ; i < tokens2.length - (l-1); i++){
//hit a single token or a + b if there are two.
String hit = new String(getPhrase(l, i, tokens2));
PatternGeneratorKey key1 = new PatternGeneratorKey(i, l, tokens2);
System.out.println();
System.out.println(key1.toString() + "|" + key2.toString() + "|"+ key1.equals(key2));
for(int k = 0;k< tokens2.length;k++){
System.out.println("i:" + i + ",l:" + l + ",tokens:" + tokens2[k]);
}
System.out.println("hashcode:" + key1.hashCode());
List list = null;
if(!map.containsKey(key1)){
list = new ArrayList<String>();
map.put(key1, list);
}else{
list = (List) map.get(key1);
}
list.add(hit);
}
}
value = "ford used cars";
tokens= value.toString().split("\\s+");
PatternGeneratorKey key1 = new PatternGeneratorKey(0,1,tokens);
tokens2 = "gm used cars".split("\\s+");
key2 = new PatternGeneratorKey(0,1,tokens2);
System.out.println(key1.equals(key2));
System.out.println(key2.equals(key1));
System.out.println(key1.hashCode() );
System.out.println(key2.hashCode() );
System.out.println(map);
}
private static String getPhrase(int l, int i, String[] tokens){
StringBuffer strin = new StringBuffer();
int index = 0;
for(index = i ; index < i+l;index++){
if(index < i+l-1){
strin.append(tokens[index]).append("+");
}
else
{
strin.append(tokens[index]);
}
}
return strin.toString();
}
}
Your problem is caused by the fact that equals(PatternGeneratorKey) doesn't override equals(Object) (but it overloads equals(Object), so that key1.equals(key2) returns true when key1 and key2 are variables of type PatternGeneratorKey!).
Since HashMap calls equals(Object) to check keys for equality, your method never gets called, so you need to implement equals(Object) instead.
You have a bug in either hashCode() or equals(). Show us the code.
Wild guess: in your code key1.equals(key2) doesn't mean key2.equals(key1).
You created an overload for equals(MyOwnKey) instead of overriding equals(Object).
Use the #Override annotation on equals() and hashCode(). It will catch this fairly common error at compile time.
HashMap#containsKey overrides AbstractMap#containsKey - so there's a subtle difference in the way that the condition:
"Returns true if and only if this map contains a mapping for a key k such that (key==null ? k==null : key.equals(k))"
is implemented.
For a subclass of AbstractMap which doesn't override containsKey() then you may well be able to get away with just implementing equals() correctly. However, for a HashMap, you need to have the implementation of hashCode() correct and satisfying the appropriate identity as well.
In any case - show us the code.
You haven't actually implemented equals.
public boolean equals(PatternGeneratorKey k) {
is not what HashMap uses. It's looking for public boolean equals(Object obj) {}
Note: This is an assignment.
Hi,
Ok I have this method that will create a supposedly union of 2 sets.
i
mport java.io.*;
class Set {
public int numberOfElements;
public String[] setElements;
public int maxNumberOfElements;
// constructor for our Set class
public Set(int numberOfE, int setE, int maxNumberOfE) {
this.numberOfElements = numberOfE;
this.setElements = new String[setE];
this.maxNumberOfElements = maxNumberOfE;
}
// Helper method to shorten/remove element of array since we're using basic array instead of ArrayList or HashSet from collection interface :(
static String[] removeAt(int k, String[] arr) {
final int L = arr.length;
String[] ret = new String[L - 1];
System.arraycopy(arr, 0, ret, 0, k);
System.arraycopy(arr, k + 1, ret, k, L - k - 1);
return ret;
}
int findElement(String element) {
int retval = 0;
for ( int i = 0; i < setElements.length; i++) {
if ( setElements[i] != null && setElements[i].equals(element) ) {
return retval = i;
}
retval = -1;
}
return retval;
}
void add(String newValue) {
int elem = findElement(newValue);
if( numberOfElements < maxNumberOfElements && elem == -1 ) {
setElements[numberOfElements] = newValue;
numberOfElements++;
}
}
int getLength() {
if ( setElements != null ) {
return setElements.length;
}
else {
return 0;
}
}
String[] emptySet() {
setElements = new String[0];
return setElements;
}
Boolean isFull() {
Boolean True = new Boolean(true);
Boolean False = new Boolean(false);
if ( setElements.length == maxNumberOfElements ){
return True;
} else { return False; }
}
Boolean isEmpty() {
Boolean True = new Boolean(true);
Boolean False = new Boolean(false);
if ( setElements.length == 0 ) {
return True;
} else { return False; }
}
void remove(String newValue) {
for ( int i = 0; i < setElements.length; i++) {
if ( setElements[i] != null && setElements[i].equals(newValue) ) {
setElements = removeAt(i,setElements);
}
}
}
int isAMember(String element) {
int retval = -1;
for ( int i = 0; i < setElements.length; i++ ) {
if (setElements[i] != null && setElements[i].equals(element)) {
return retval = i;
}
}
return retval;
}
void printSet() {
for ( int i = 0; i < setElements.length; i++) {
if (setElements[i] != null) {
System.out.println("Member elements on index: "+ i +" " + setElements[i]);
}
}
}
String[] getMember() {
String[] tempArray = new String[setElements.length];
for ( int i = 0; i < setElements.length; i++) {
if(setElements[i] != null) {
tempArray[i] = setElements[i];
}
}
return tempArray;
}
Set union(Set x, Set y) {
String[] newXtemparray = new String[x.getLength()];
String[] newYtemparray = new String[y.getLength()];
int len = newYtemparray.length + newXtemparray.length;
Set temp = new Set(0,len,len);
newXtemparray = x.getMember();
newYtemparray = x.getMember();
for(int i = 0; i < newYtemparray.length; i++) {
temp.add(newYtemparray[i]);
}
for(int j = 0; j < newXtemparray.length; j++) {
temp.add(newXtemparray[j]);
}
return temp;
}
Set difference(Set x, Set y) {
String[] newXtemparray = new String[x.getLength()];
String[] newYtemparray = new String[y.getLength()];
int len = newYtemparray.length + newXtemparray.length;
Set temp = new Set(0,len,len);
newXtemparray = x.getMember();
newYtemparray = x.getMember();
for(int i = 0; i < newXtemparray.length; i++) {
temp.add(newYtemparray[i]);
}
for(int j = 0; j < newYtemparray.length; j++) {
int retval = temp.findElement(newYtemparray[j]);
if( retval != -1 ) {
temp.remove(newYtemparray[j]);
}
}
return temp;
}
}
// This is the SetDemo class that will make use of our Set class
class SetDemo {
public static void main(String[] args) {
//get input from keyboard
BufferedReader keyboard;
InputStreamReader reader;
String temp = "";
reader = new InputStreamReader(System.in);
keyboard = new BufferedReader(reader);
try
{
System.out.println("Enter string element to be added" );
temp = keyboard.readLine( );
System.out.println("You entered " + temp );
}
catch (IOException IOerr)
{
System.out.println("There was an error during input");
}
/*
**************************************************************************
* Test cases for our new created Set class.
*
**************************************************************************
*/
Set setA = new Set(0,10,10);
setA.add(temp);
setA.add("b");
setA.add("b");
setA.add("hello");
setA.add("world");
setA.add("six");
setA.add("seven");
setA.add("b");
int size = setA.getLength();
System.out.println("Set size is: " + size );
Boolean isempty = setA.isEmpty();
System.out.println("Set is empty? " + isempty );
int ismember = setA.isAMember("sixb");
System.out.println("Element sixb is member of setA? " + ismember );
Boolean output = setA.isFull();
System.out.println("Set is full? " + output );
//setA.printSet();
int index = setA.findElement("world");
System.out.println("Element b located on index: " + index );
setA.remove("b");
//setA.emptySet();
int resize = setA.getLength();
System.out.println("Set size is: " + resize );
//setA.printSet();
Set setB = new Set(0,10,10);
setB.add("b");
setB.add("z");
setB.add("x");
setB.add("y");
Set setC = setA.union(setB,setA);
System.out.println("Elements of setA");
setA.printSet();
System.out.println("Union of setA and setB");
setC.printSet();
}
}
The union method works a sense that somehow I can call another method on it but it doesn't do the job, i supposedly would create and union of all elements of setA and setB but it only return element of setB. Sample output follows:
java SetDemo
Enter string element to be added
hello
You entered hello
Set size is: 10
Set is empty? false
Element sixb is member of setA? -1
Set is full? true
Element b located on index: 2
Set size is: 9
Elements of setA
Member elements on index: 0 hello
Member elements on index: 1 world
Member elements on index: 2 six
Member elements on index: 3 seven
Union of setA and setB
Member elements on index: 0 b
Member elements on index: 1 z
Member elements on index: 2 x
Member elements on index: 3 y
thanks,
lupin
In union(), you have a typo here:
newYtemparray = x.getMember();
should be
newYtemparray = y.getMember();