aspose file tools*
The moose likes Java in General and the fly likes JDK 1.5 Collections.sort Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "JDK 1.5 Collections.sort" Watch "JDK 1.5 Collections.sort" New topic
Author

JDK 1.5 Collections.sort

David Garratt
Ranch Hand

Joined: Aug 08, 2003
Posts: 198

Having recently upgraded my application to JDK 1.5 with Eclipse 3.1. I've been going through all my code making it a more compliant with the new standards but I'm left with one problem which has me foxed.

Below are two almost identical code segments, the top one generates a warning, the bottom one does not and I'm not sure what to do about it.


public void AddtoList(LinkedList<JDBModuleListData> list, JDBModuleListData newValue, boolean sort) {
list.add(newValue);
if (sort == true) Collections.sort(list);
}

Type safety: Unchecked invocation sort(List<JDBModuleListData> of the generic method sort
(List<T> of type Collections


This method does not create any warnings.

public void AddtoList2(LinkedList<String> list, String newValue, boolean sort) {
list.add(newValue);
if (sort == true) Collections.sort(list);
}

I've been looking into using Collections.checkedlist as the parameter to the sort method without any success.

Any advice appreciated

Dave
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
just a guess: String implements Comparable; does JDBModuleListData implements it too?


java amateur
David Garratt
Ranch Hand

Joined: Aug 08, 2003
Posts: 198

This is my class, so I think my answer is yes.

package com.commander4j.sys;


import javax.swing.Icon;


public class JDBModuleListData implements Comparable {
protected Icon m_icon;
protected int m_index;
protected boolean m_selectable;
protected Object m_data;

public JDBModuleListData(Icon icon, int index, boolean selectable, Object data)
{
m_icon = icon;
m_index = index;
m_selectable = selectable;
m_data = data;
}

public Icon getIcon() { return m_icon; }
public int getIndex() { return m_index; }
public boolean isSelectable() { return m_selectable; }
public Object getObject() { return m_data; }
public String toString() { return m_data.toString(); }

public int compareTo(Object anotherModuleListData) throws ClassCastException {
if (!(anotherModuleListData instanceof JDBModuleListData))
throw new ClassCastException("A JDBModuleListData object expected.");
return m_data.toString().compareTo(anotherModuleListData.toString());

}

}
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
It looks like your class needs to implement Comparable<JDBModuleListData>, not just Comparable. Note that String implements Comparable<String> - that's why your second code example compiles without warnings.


"I'm not back." - Bill Harding, Twister
David Garratt
Ranch Hand

Joined: Aug 08, 2003
Posts: 198

Being a relative newbie and stuggling with the new syntax of jdk 1.5 could I trouble you for a quick example of how I would do this.

Sorry again

Dave
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
this fragment is taken from Agile Java (by J Langr):


hope this helps
David Garratt
Ranch Hand

Joined: Aug 08, 2003
Posts: 198

I've updated the class as per your guidelines as shown below :-

package com.commander4j.sys;

import javax.swing.Icon;

public class JDBModuleListData implements Comparable {
protected Icon m_icon;
protected int m_index;
protected boolean m_selectable;
protected Object m_data;

public JDBModuleListData(Icon icon, int index, boolean selectable, Object data)
{
m_icon = icon;
m_index = index;
m_selectable = selectable;
m_data = data;
}

public Icon getIcon() { return m_icon; }
public int getIndex() { return m_index; }
public boolean isSelectable() { return m_selectable; }
public Object getObject() { return m_data; }
public String toString() { return m_data.toString(); }

public int compareTo(Object anotherModuleListData) throws ClassCastException {
if (!(anotherModuleListData instanceof JDBModuleListData))
throw new ClassCastException("A JDBModuleListData object expected.");
return m_data.toString().compareTo(anotherModuleListData.toString());

}

public int compareTo(JDBModuleListData anotherModuleListData) {
return m_data.toString().compareTo(anotherModuleListData.toString());

}

}

However the original code below is still flagged with a warning message within eclipse, does this mean I'm using the sort method wrongly.

"Type safety: Unchecked invocation sort(List<JDBModuleListData> of the generic method sort
(List<T> of type Collections"

public void AddtoList(LinkedList<JDBModuleListData> list, JDBModuleListData newValue) {
list.add(newValue);
Collections.sort(list);
}
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
If you look at the API for String, it says it implements Comparable<String>. Your class currently says it implements Comparable; instead it should say that it implements Comparable<JDBModuleListData>. If you do this, you will be able to delete the compareTo(Object) method entirely. The only one you need is compareTo(JDBModuleListData).
[ July 10, 2005: Message edited by: Jim Yingst ]
David Garratt
Ranch Hand

Joined: Aug 08, 2003
Posts: 198

Many many many thanks.

That's sorted it and I've learned something to boot !

Dave
 
 
subject: JDK 1.5 Collections.sort