The Artima Developer Community
Sponsored Link

Java Answers Forum
How can i order in decending order Using hash set

1 reply on 1 page. Most recent reply: Dec 19, 2002 7:54 AM by Oliver

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Lena Kokoromiti

Posts: 4
Nickname: mike0303
Registered: Dec, 2002

How can i order in decending order Using hash set Posted: Dec 18, 2002 12:01 AM
Reply to this message Reply
Advertisement
I want to order the hashset in descending order but i dont know the actuall code to do that! i am new in Java can anyone help me please
public class TestSaveNumbers{ //implements Comparator{


public static void main(String args[]) {

Numbers nu = new Numbers();
Numbers nu1 = new Numbers();

Set a = new HashSet();
Set b = new HashSet();
Set c= new HashSet();
int max_range=30;
int total=4;
Integer number1;

Integer number2;
int sequence=1;
int quantity;
// for (int i=0;i<sequence;i++){
quantity=0;


do{
number1 = new Integer((int)(max_range*Math.random()+1));
number2 = new Integer((int)(max_range*Math.random()+1));


//if (!a.contains(number1)){
a.add(number1);
//System.out.println("The Elements of First set:"+a);
//String astring= String.valueOf(a);
//String.valueOf(astring);
//st.addState(astring);
b.add(num ber2);
//set.add(number1);


Oliver

Posts: 12
Nickname: lazycat
Registered: Dec, 2002

Re: How can i order in decending order Using hash set Posted: Dec 19, 2002 7:54 AM
Reply to this message Reply
import java.util.*;

class DescendHashSet {
public static void main(String [] args){
String [] items = {"3", "1", "4", "1", "5","9"};

DescendComp descendSort = new DescendComp();

Set ascendSet = new TreeSet();
Set descendSet = new TreeSet(descendSort);

for(int i = 0; i < items.length; i++){
ascendSet.add(new Integer(items));
descendSet.add(new Integer(items));
}


System.out.println(ascendSet);
System.out.println(descendSet);
}
}

class DescendComp implements Comparator {
public int compare(Object str1, Object str2){
Integer tmp1 = (Integer)str1;
Integer tmp2 = (Integer)str2;

return -(tmp1.compareTo(tmp2));
}
}

Flat View: This topic has 1 reply on 1 page
Topic: New code with bugs, can someone help me Previous Topic   Next Topic Topic: Menus in Applets

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use