Jump to content

Java 8 need help with a program!

dronikal
Go to solution Solved by SSL,
1 hour ago, dronikal said:

I'm afraid I'm too new to java 8 to know what that is;)

Then look it up in the Java docs. It sounds like exactly what you need. As the comment above describes, use the random number as the key and the position in your list the value. When the number of elements in your TreeMap exceeds 10, just kick out the element with the smallest key (the first in the tree). I also recommend that you read about Minimum Heaps and eventually Red-Black trees, which are the implementation used for Java TreeMaps.

Hello!I have ArrayList full of random Integers between 0-1000.And i need to get the index of the element with the highest value under 800 and the 10 highest values under 800.And I have to do that without sorting the list.

All I have until now what i could make by myself is this:

public static void main(String[] args) {
        ArrayList<Integer> list=new ArrayList<>(); 
        Random gen=new Random();                                                                
        for (int i = 0; i <100; i++) {
            int counter=gen.nextInt(1000);                                
            list.add(counter);                        
        }
        int maxindex=0;    
        for (int i = 1; i <list.size(); i++) {
            int number1=list.get(maxindex);
            int number2=list.get(i);
            if (number1<800) {
                if(number2<800){
                    if(number2>number1){
                         maxindex=i;
                    }    
                }
            }
            else {
                if(number2<800){
                    maxindex=i;
                }
            }
        }
    }

Link to comment
Share on other sites

Link to post
Share on other sites

Are you allowed to sort your 10 maximum values? If so, there is an easy way to maintain a sorted data structure. I will so no more in the interests of not giving away a possible solution.

Link to comment
Share on other sites

Link to post
Share on other sites

No sorting at all.I know the collection.sort easy way but I'm not allowed to use it.

Link to comment
Share on other sites

Link to post
Share on other sites

You could just push all of the indexes you find to a stack with a maximum size of 10. 

There are 10 types of people in this world, those who can read binary and those who can't.

There are 10 types of people in this world, those who can read hexadecimal and F the rest.

~Fletch

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, dronikal said:

No sorting at all.I know the collection.sort easy way but I'm not allowed to use it.

Are you allowed to use a TreeMap?

Link to comment
Share on other sites

Link to post
Share on other sites

11 minutes ago, SSL said:

Are you allowed to use a TreeMap?

I'm afraid I'm too new to java 8 to know what that is;)

Link to comment
Share on other sites

Link to post
Share on other sites

The idea behind the program is that every random number is the length of a gene so i need to find the position of the 10 longest genes under 800 bases.So any sorting will result in me losing the position of the gene in the chromosome.

Link to comment
Share on other sites

Link to post
Share on other sites

Off the top of my head I'd think using a tree map, the position could be used as the key value. Sort by the random number. It'd be out of order, but the key value would keep track of the order they were in.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, dronikal said:

I'm afraid I'm too new to java 8 to know what that is;)

Then look it up in the Java docs. It sounds like exactly what you need. As the comment above describes, use the random number as the key and the position in your list the value. When the number of elements in your TreeMap exceeds 10, just kick out the element with the smallest key (the first in the tree). I also recommend that you read about Minimum Heaps and eventually Red-Black trees, which are the implementation used for Java TreeMaps.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×