Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Flex and the fly likes Unique Values in ArrayCollection? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Flex
Bookmark "Unique Values in ArrayCollection?" Watch "Unique Values in ArrayCollection?" New topic
Author

Unique Values in ArrayCollection?

Robert Jones
Greenhorn

Joined: Apr 27, 2010
Posts: 2
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

Joined: Apr 14, 2004
Posts: 10336

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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Robert Jones
Greenhorn

Joined: Apr 27, 2010
Posts: 2
Hi - thanks for the quick reply and pointing out that ActionScript class.

I'll check on implementing the Set you referred to.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Unique Values in ArrayCollection?
 
Similar Threads
reading excel xlsx files to convert into CSV format
checking for neighbours in array
different JComboBoxes as editors on multiple JTable rows
Need help with Array (setting variable)
Trouble with model for calculating and adding table values dependent on multiple rows