O
Ook
(newbie question thread consolidation thread)
Let me preface by saying I'm taking a java class, and my instructor likes to
see us maximize the usage of the collections framework whenever possible. He
wants an efficient oop design. I can get my code to run and do what I want,
but I'm not sure if I'm maximizing the usage of the collections framework,
and sometimes I make oop blunders. Hence my questions:
1) I have an ArrayList of Strings. I want to form a String that contains all
of the Strings, comma delimited. This is what I do. It works, but is there
are better or more correct way to do this?:
String returnValue = "";
for( String keyWord : _keywords )
returnValue = returnValue + keyWord + ", ";
// Strip off the last ", "
return returnValue.substring( 0, returnValue.length()-2 );
2) I have an ArrayList of Items, Item being a class with Strings and
ArrayLists or TreeSets of Strings. I need to search for and create a list of
items that have certain text in either the Strings, or the
ArrayList/TreeSets of Strings. I can do this, my code works, but again I'm
not sure I'm doing this most efficiently.
public class Item
{
private String _title1;
private String _title2;
private String _title3;
private TreeSet<String> list1 = new TreeSet<String>();
....
}
It was suggested I use a HashMap, but how would I implement that with the
above class, where I would have to search on different items in the class at
different times? My brute force code works, where I iterate through the list
with a for-each loop, checking each instance as I go through, but again may
not be the most efficient way.
Let me preface by saying I'm taking a java class, and my instructor likes to
see us maximize the usage of the collections framework whenever possible. He
wants an efficient oop design. I can get my code to run and do what I want,
but I'm not sure if I'm maximizing the usage of the collections framework,
and sometimes I make oop blunders. Hence my questions:
1) I have an ArrayList of Strings. I want to form a String that contains all
of the Strings, comma delimited. This is what I do. It works, but is there
are better or more correct way to do this?:
String returnValue = "";
for( String keyWord : _keywords )
returnValue = returnValue + keyWord + ", ";
// Strip off the last ", "
return returnValue.substring( 0, returnValue.length()-2 );
2) I have an ArrayList of Items, Item being a class with Strings and
ArrayLists or TreeSets of Strings. I need to search for and create a list of
items that have certain text in either the Strings, or the
ArrayList/TreeSets of Strings. I can do this, my code works, but again I'm
not sure I'm doing this most efficiently.
public class Item
{
private String _title1;
private String _title2;
private String _title3;
private TreeSet<String> list1 = new TreeSet<String>();
....
}
It was suggested I use a HashMap, but how would I implement that with the
above class, where I would have to search on different items in the class at
different times? My brute force code works, where I iterate through the list
with a for-each loop, checking each instance as I go through, but again may
not be the most efficient way.