Jump to content

Java 8 need help with a program!

Go to solution Solved by Guest,
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
https://linustechtips.com/topic/573498-java-8-need-help-with-a-program/
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 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 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

×