patternjavaMinor
Operating sublists efficiently
Viewed 0 times
sublistsoperatingefficiently
Problem
In my Java program, I need to operate on the sublists a of
For
and outputs the following:
Core function:
Currently, the performance is not good. Could you find the bad design or inappropriate usage of data structures/APIs? What are the good common ways of operating sublists?
ArrayList or LinkedList often, like removing a sublist, comparing two sublists.For
ArrayList or LinkedList, I didn't find any good APIs to do these. My current implementation is as below. Basically, it takes an input like:u1234 u1236 u1236 u2def u1236 u1236 u2defand outputs the following:
u1234 ( ( u1236 ) * u2def ) *Core function:
ArrayList toRegex(ArrayList tokenArray)
{
/* check different length of continuous duplication */
for(int len=1; len first = new ArrayList();
ArrayList second = new ArrayList();
for(int j=i; j tokenArray.size())
break;
second.clear();
for(int j=i+len; j<i+2*len; j++)
second.add(tokenArray.get(j));
}
if(match == true) {
tokenArray.add(i, "(");
tokenArray.add(i+1+len, ")");
tokenArray.add(i+2+len, "*");
i = i+3+len;
match = false;
}
}
}
return tokenArray;
}Currently, the performance is not good. Could you find the bad design or inappropriate usage of data structures/APIs? What are the good common ways of operating sublists?
Solution
Yes, this doesn't look pretty. Consider converting the list to sets or using RegEx.
Sublists:
Use retainAll to get intersections:
ArrayList A = new ArrayList();
A.add("Z");
A.add("Z");
A.add("C");
A.add("X");
A.add("Z");
Set set = new HashSet(A);
for(String temp:set)
System.out.println(temp);Sublists:
List subList = alist.subList(2, 4);Use retainAll to get intersections:
listOne.retainAll(listTwo) ;
boolean areEqualNotSamePosition = listeOne.size();Code Snippets
ArrayList<String> A = new ArrayList<String>();
A.add("Z");
A.add("Z");
A.add("C");
A.add("X");
A.add("Z");
Set<String> set = new HashSet<String>(A);
for(String temp:set)
System.out.println(temp);List<String> subList = alist.subList(2, 4);listOne.retainAll(listTwo) ;
boolean areEqualNotSamePosition = listeOne.size();Context
StackExchange Code Review Q#17788, answer score: 2
Revisions (0)
No revisions yet.