• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Filtering an ArrayList

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22805
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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."

 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much,
Have been able to figure it out
 
My name is Inigo Montoya, you killed my father, prepare to read a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic