• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Need Help Sorting ArrayList of Generic Objects

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello and thanks in advance for your help...

I am running into a problem trying to sort an ArrayList of objects. First the Objects I'm trying to sort look like (paired down to the essentials for this post):


In another class the code that tries to sort this looks like:


where foo.getStuff() returns a collection of Node objects.

The error the compiler is giving me is:



I guess one fix would be to replace <V> with Node, but that defeats the purpose of Generics, right? While in this example, I'm working with Nodes, later I might want to do the same with "Things."

I'm trying to understand Generics put this one has me stumped. Thanks again for any help you can provide.

Todd

[edit]Add newlines to keep width of post on the screen. CR[/edit]
 
Marshal
Posts: 79974
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are setting up a List<V> but you haven't got a V class anywhere.
You ought to parameterise your compareTo method

public class Node implements Comparable<Node>
. . .
public int compareTo(Node nnn)
{
. . .
 
Ranch Hand
Posts: 136
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you say

foo.getStuff() should return a collection of V or any subclass of V
For example, say you have three classes like this

and when you say



 
Todd Bruner
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for both of the posts!

I parameterized the Node object ( public Node implements Comparable<Node> ).

As for the foo.getStuff() it does return a collection of <V>. I guess what I meant to say was that I happen to know that the elements of the arraylist are "Nodes". This is where my understanding and my generic lingo gets in the way. Sorry.

I am still getting the Bound Mismatch error.
 
Campbell Ritchie
Marshal
Posts: 79974
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . And you ought to declare and instantiate the List to take Nodes at present.
List<Node> nodeList = new ArrayList<Node>();

You do at present want to sort nodes. You could of course declare the List to take <T extends Comparable><? super Node>>.

Probably simplest to stick to List<Node> for the time being; if "Thing" extends Node, then you can add it to the same List. If it doesn't extend Node, or if the overridden compareTo method isn't compatible with the Node compareTo method, then you will get nasty Exceptions if you try to sort the Lists.
 
Campbell Ritchie
Marshal
Posts: 79974
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to sort Nodes, and the foo#getStuff method returns <V> then you would have to have Foo as a parameterised class, with a formal type parameter <V> and instantiate that as

private Foo<Node> foo;
. . .
foo = new Foo<Node>();

At least I think so; I am not sure on this point. You might be able to achieve what you want with type inference, which you will find from one of the links below, under "methods and constructors."

The whole idea of generics is to maintain type-safety by restricting the different types permitted in a particular context. I presume you have been through the Java Tutorials, here and here, where you find "methods and constructors". And also do a Google search for "Angelika Langer Java Generics" where you will find lots of useful information.
 
Your mother was a hamster and your father was a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic