Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TreeSet class 1

Status
Not open for further replies.

sensory21

Programmer
Jan 27, 2004
79
GB
Hi,

I am using this class for the first time and I am confused as I thought that SortedSet will sort the content of ts but it doesn't?
If I print out ts or orderComplete the content will be in the same order. Any advice available?

TreeSet ts = new TreeSet ();
ts.add (roundPercentage.format(pc));
SortedSet orderComplete = ts;

Thank you
 
Works for me :

Code:
TreeSet ts = new TreeSet();
ts.add("ddd");
ts.add("ccc");
ts.add("bbb");
ts.add("aaa");
SortedSet ss = Collections.synchronizedSortedSet(ts);
System.out.println(ss);

--------------------------------------------------
Free Database Connection Pooling Software
 
Ok, I've tried your method and it effectively works so I guess
I am not using the right method, roundPercentage.format(pc) is inside a while loop and this method doesn't sort all the elements? Am I missing something or should I use another method?

TreeSet ts = new TreeSet ();
ts.add (roundPercentage.format(pc));
SortedSet ss = Collections.synchronizedSortedSet(ts);
out.println(ss);
 
What is "pc" ?
What is "roundPercentage" ?

Some example data would be helpful ...

--------------------------------------------------
Free Database Connection Pooling Software
 
sorry... how can you guess!

DecimalFormat roundPercentage = new DecimalFormat("0");
...
...
Iterator i = items.iterator();
while(i.hasNext()) {
double pc;
int k =0;

Item item = (Item) i.next();
if(item.getNextStage().getStageId() == Stage.DONE) {
pc = 100.0;
}
else {
int done = item.getHistory().size();
int total = item.getItemType().getStages().size();
pc = 100.0 * done / total;
}

TreeSet ts = new TreeSet ();
ts.add (roundPercentage.format(pc));
SortedSet ss = Collections.synchronizedSortedSet(ts);
out.println(ss);

%>

<tr>
<td class=columnwhite><%=roundPercentage.format(pc)%>%</td>
<td class=columnwhite width="75">

</td>
</tr>
<%
}
%>

output:
[36]
[36]
[45]
[45]
[45]
[38]
[72]
[7]
[0]
[24]
[6]
[18]
[64]
[45]
[64]
[64]
[64]
[55]

That list is what I would like to have sorted, actually before the while loop I will need to format the column's title that if we click on it, it does sort the list in ascending or descending order.
 
Works for me :
Code:
		TreeSet ts = new TreeSet();
		ts.add(new Integer("15"));
		ts.add(new Integer("5"));
		ts.add(new Integer("25"));
		ts.add(new Integer("20"));
		SortedSet ss = Collections.synchronizedSortedSet(ts);
		System.err.println(ss);

I think you are getting a bit confused with your loops and how you are adding data to your Set.
Try this :

Code:
DecimalFormat roundPercentage = new DecimalFormat("0");
...
...
TreeSet ts = new TreeSet ();
Iterator i = items.iterator();
while(i.hasNext()) {
    double pc;
    int k  =0;
    
    Item item = (Item) i.next();
    if(item.getNextStage().getStageId() == Stage.DONE) {
        pc = 100.0;
    }
    else {
        int done = item.getHistory().size();
        int total = item.getItemType().getStages().size();
        pc = 100.0 * done / total;
    }

ts.add (roundPercentage.format(pc));
}
%>

SortedSet ss = Collections.synchronizedSortedSet(ts);
out.println(ss);

<%
Iterator ssIt = ss.iterator();
while (ssIt.hasNext()) {
%>
<tr>
<td class=columnwhite><%=ssIt.next()%> %</td>
    <td class=columnwhite width="75"> 
      
    </td>
</tr>
<%
}
%>

--------------------------------------------------
Free Database Connection Pooling Software
 
Ooops :

Code:
DecimalFormat roundPercentage = new DecimalFormat("0");
...
...
TreeSet ts = new TreeSet ();
Iterator i = items.iterator();
while(i.hasNext()) {
    double pc;
    int k  =0;
    
    Item item = (Item) i.next();
    if(item.getNextStage().getStageId() == Stage.DONE) {
        pc = 100.0;
    }
    else {
        int done = item.getHistory().size();
        int total = item.getItemType().getStages().size();
        pc = 100.0 * done / total;
    }

ts.add (roundPercentage.format(pc));
}

SortedSet ss = Collections.synchronizedSortedSet(ts);
out.println(ss);

Iterator ssIt = ss.iterator();
while (ssIt.hasNext()) {
%>
<tr>
<td class=columnwhite><%=ssIt.next()%> %</td>
    <td class=columnwhite width="75"> 
      
    </td>
</tr>
<%
}
%>

--------------------------------------------------
Free Database Connection Pooling Software
 
sorry Sedj,

but I didn't leave the other columns as I didn't want to confuse you but I need to stay in the same while loop for the other results like item.get......


<td class=columnwhite><%=item.getItemType().getDescription()%></td>
<td class=columnwhite><%=item.getSystem().getName()%></td>
<td class=columnwhite><%=item.getJtd()%></td>
<td class=columnwhite><%=item.getTag()%></td>
<td class=columnwhite><%=item.getStage().getStageTemplate().getDescription()%></td>
<td class=columnwhite><%=item.getNextStage().getStageTemplate().getDescription()%></td>
<td class=columnwhite><%=roundPercentage.format(pc)%></td>
 
Will it be possible to have a request at the top of the page and retrieve the sorted order?
 
Look, you are going around this in a mad way.

I take it you have some data, that probably looks something like this :

20% dfsdfsd dfdsfsdf
50% rrgbbhb qqqwesee
35% wwewwww ghghghh
10% retrett ppppppss

and you want to display it like this yeah ?

10% retrett ppppppss
20% dfsdfsd dfdsfsdf
35% wwewwww ghghghh
50% rrgbbhb qqqwesee

Is this what you are trying to achieve ?!


--------------------------------------------------
Free Database Connection Pooling Software
 
Sorry, just seen your last reply, but yes that's what I want, even a bit more difficult as when I click on the top of the column it should display the contents as you say:
10% retrett ppppppss
20% dfsdfsd dfdsfsdf
35% wwewwww ghghghh
50% rrgbbhb qqqwesee

in ascending or descinding order.

in the meantime I was working on the SortedMap class, I get some results but not quite what I want and that's in the while loop:


while(i.hasNext()) {
double pc;
int k = 0;

Item item = (Item) i.next();
if(item.getNextStage().getStageId() == Stage.DONE) {
pc = 100.0;
k++;
}
else {
int done = item.getHistory().size();
int total = item.getItemType().getStages().size();
pc = 100.0 * done / total;
k++;
}

Map map = new HashMap();
map.put(String.valueOf(k), roundPercentage.format(pc));
SortedMap sortedMap = new TreeMap();
sortedMap.putAll(map);
out.println("SortedMap: " + sortedMap);
%>

the output of this looks like:
SortedMap: {1=91}
SortedMap: {1=91}
SortedMap: {1=36}
SortedMap: {1=91}
SortedMap: {1=25}
SortedMap: {1=38}
SortedMap: {1=38}
SortedMap: {1=0}
SortedMap: {1=12}
SortedMap: {1=38}
SortedMap: {1=25}
SortedMap: {1=25}
SortedMap: {1=25}
SortedMap: {1=38}

so the int k is definitely no good there (should be an object) and the list is not ordered!
 
OK, hopefully this will clear things up.

Code:
Iterator i = items.iterator();
Hashtable ht = new Hashtable();
while(i.hasNext()) {
	String html = "";
	html += "<td class=columnwhite>" +item.getItemType().getDescription()+  "</td>";
	html += "<td class=columnwhite>" +item.getSystem().getName()+  "</td>";
	html += "<td class=columnwhite>" +item.getJtd()+  "</td>";
	html += "<td class=columnwhite>" +item.getTag()+  "</td>";
	html += "<td class=columnwhite>" +item.getStage().getStageTemplate().getDescription()+  "</td>";
	html += "<td class=columnwhite>" +item.getNextStage().getStageTemplate().getDescription()+  "</td>";
	html += "<td class=columnwhite>" +roundPercentage.format(pc)+  "</td>";

	ht.put("" +roundPercentage.format(pc), html);
}

SortedMap sortedMap = new TreeMap();
sortedMap.putAll(ht);

Iterator iter = sortedMap.keySet().iterator();
while (iter.hasNext()) {
	Object key = iter.next();
	System.err.println(key +"=" +sortedMap.get(key));
}

--------------------------------------------------
Free Database Connection Pooling Software
 
Hi,

I would need to add this in the html just before the ht.put:

<td class=columnwhite width="75">
<table width="100%" border="0" cellspacing="0" cellpadding="0" style="border:1px solid #000000;">
<tr>
<% if (pc > 0.00001) {%>
<td width="<%=pc%>%"><img src="images/bargrph_blue.gif" width="100%" height="10"></td>
<%}%>
<% if (pc < 100.00000) {%>
<td bgcolor="#CCCCFF"><img src="images/bargrph_spacer.gif" width="1" height="10"></td>
<%}%>
</tr>
</table>
</td>

can you help me with this?
ps: how do you put your code in the code box please?

thanks
vero
 
Forget about the previous question it's fine now; thanks a lot for your previous help with SortedMap it effectively works unfortunately that's not what I wanted as now
it displays only the table in that sorted order and the other columns don't sort anymore, what I wanted it to collect the order to use it at the top of the column! maybe SortedMap wasn't the way to go, and Im lost!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top