Saturday 15 February 2014

java - Reducing the number of comparisons performed when matching between two String arrays -



java - Reducing the number of comparisons performed when matching between two String arrays -

i comparing 3 arrays of strings using 2 classes below. without using hash maps or changing construction of code much (i can't alter signature of findmatchingelements()), there way minimize number of comparisons method makes, in order build new array of shared elements?

in testrun.java tested code on 3 arrays 8 elements each, resulted in 46 comparisons made. want accomplish lower number of comparisons. there way?

i tried using remove() method remove string collection 1 time compared matching element query collection. prevented redundant comparisons, did not result in important reduction.

import java.util.*; public class commonelements { int originalcollectioncount = 0; object[] originalcollections; int listcount = 1; int matchcount; int comparisoncount = 0; public comparable[] findmatchingitems(object[] collections) { string[] queryarray = (string[])collections[0]; string[] secondaryarray = (string[])collections[1]; arraylist<string> querylist = new arraylist(arrays.aslist(queryarray)); arraylist<string> secondarylist = new arraylist(arrays.aslist(secondaryarray)); arraylist<string> commonlist = new arraylist(); int = 0; if(listcount == 1){ originalcollectioncount = collections.length; originalcollections = collections; } listcount ++; for(string x:querylist) { for(string y:secondarylist) { comparisoncount++; if(x.compareto(y) == 0) { commonlist.add(x); //add mutually shared item commonlist secondarylist.remove(y); //remove mutually shared item consideration if(originalcollectioncount == listcount) //if every list has been examined { system.out.println(commonlist.get(i)); } i++; break; } } } string[] commonlistresult = new string[commonlist.size()]; commonlist.toarray(commonlistresult); if(originalcollectioncount > listcount){ findmatchingitems(new object[] {commonlistresult,originalcollections[listcount]});} if (collections.length == 0) { homecoming new comparable[0]; } else if (collections.length == 1) { homecoming (comparable[]) collections[0]; } homecoming commonlistresult; } public int getcomparisons(){ homecoming comparisoncount;} } public class testrun { private final static string[] collection_5_1 = {"pittsburgh", "new york", "chicago", "cleveland", "miami", "dallas", "atlanta", "detroit"}; private final static string[] collection_5_2 = {"dallas", "atlanta", "cleveland", "chicago", "washington", "houston", "baltimore", "denver"}; private final static string[] collection_5_3 = {"chicago", "kansas city", "cleveland", "jacksonville", "atlanta", "tampa bay", "dallas", "seattle"}; public static void main(string[] args) { new testrun(); } public testrun() { commonelements commonelements = new commonelements(); object[] input = new object[3]; input[0] = collection_5_1; input[1] = collection_5_2; input[2] = collection_5_3; system.out.println("matching items:"); commonelements.findmatchingitems(input); system.out.println(commonelements.comparisoncount + " comparisons made."); } }

you run single advanced loop below provide length both array same if not run throug accordingly.

for(string str:arraystr1){ if(arraystr2.contains(str)){ newarray.add(str); } }

java arrays string performance comparison

No comments:

Post a Comment