Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unique Values in ArrayCollection?

 
Robert Jones
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, I am new to Java so this may seem like a basic question. Second, I am new to JavaRanch and this may not be the correct forum. With that...

I have a need to export data to a CSV file and have been able to find a small servlet that does the trick. However I need to find the unique values in one of the columns. Specifically one of the columns is "Timeline" and it displays the date. But I would like to find the unique values of the timeline column in order to change the format of the output.

So, what's the best approach for finding the unique values? I have to use ArrayCollection (I think) because this is being passed from a Flex application. Based on the code snippet below, is there a way to get the unique values for Timeline based on the HashMap that is created? I get confused on all the different types such as HaspMap, Set, etc.

Thanks,
Rob



Code Snippet:

public String[] getData(ArrayCollection arrayCollection) {
Object[] elements = arrayCollection.toArray();
int rows = elements.length;
String[] results = new String[rows + 1];
StringBuffer result = new StringBuffer(1000);

// Loop through the array
for (int i = 0; i < rows; i++) {
//The column names and values are stored in a HashMap for each row
map = (HashMap)elements[i];

< AND SO ON to loop through and build a CSV string>
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayCollection is an ActionScript class, there is no class of that name in Java. The equivalents in Java would be an array, or some suitable Collection (such as an ArrayList).

One of the easiest ways of removing dublicates is to add them to a Set. Sets don't support duplicates, so these will go. And its easy to transfer from one Collection to another. You just need to make sure your possible unique object has an equals() method that matches your uniqueness requirement.

 
Robert Jones
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi - thanks for the quick reply and pointing out that ActionScript class.

I'll check on implementing the Set you referred to.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic