Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Filtering an ArrayList

 
James Dudley
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an Arraylist of Objects (name, value). There can the same name in the ArrayList with different values.

What I am trying to do with create a new ArrayList / HashTable that only contains 1 of each object with distinct name where the value is the most

eg arraylist could hold

Name value
A 10
B 12
A 15
B 9

New ArrayList would have
Name value
A 15
B 12


thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Map<String,Integer> (use HashMap, not Hashtable) seems like the most obvious choice. For an object, get the current value from the map. If there is no current value or the current value is lower than the new value, overwrite the value. Otherwise do nothing.
 
James Dudley
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks

Could I ask what the code would look like to do the "If there is no current value or the current value is lower than the new value, overwrite the value. Otherwise do nothing."

 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iterate the Map instance with the values in the ArrayList then you can check your condition to add them to the map. A sample code you may use:

 
James Dudley
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks

was keeping it simple for question but what if the Integer is an Object with one of the values within the object is the value
I can pull the name and value but having problem with getting the oldvalue
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming you create a map like Map<String, YourClass>. The only difference is you retrieve the entire object then compare against a property of that object... Is this what you are talking about?
 
James Dudley
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes

I worked out how to get the oldvalue out but I never used a ternary operator and it is confusing me (I will ask a java dev on monday at work about them)

how would the if-else statement look like using objects

Thanks
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how would the if-else statement look like using objects

Just think of the expression before the "?" mark as what you put inside the "if" statement and the colon (:) act as a the "else" keyword in that notation.
 
James Dudley
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much,
Have been able to figure it out
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic