Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coverting Set<E> to String[] gives the elements in the wrong order

Tags:

java

I'have simple POJO class as

public class Option {
    String optionText;

    public String getOptionText() {
        return optionText;
    }

    public void setOptionText(String optionText) {
        this.optionText = optionText;
    }

    @Override
    public String toString() {
        return optionText;
    }

}

and there I'm creating a Set object and want to convert that object to String[], I've my logic as

public class Main {
    Set<Option> options = new HashSet<Option>();
    public Main() {
        Option option1 = new Option();
        option1.setOptionText("Option 1");      
        options.add(option1);
        Option option2 = new Option();
        option2.setOptionText("Option 2");      
        options.add(option2);
        Option option3 = new Option();
        option3.setOptionText("Option 3");      
        options.add(option3);
        Option option4 = new Option();
        option4.setOptionText("Option 4");      
        options.add(option4);

        System.out.println("Set<Option> size is " + options.size());


        System.out.println(getCorrectAnswer(options, "a"));
    }

    public static void main(String[] args) {
        new Main();
    }


    public String getCorrectAnswer(Set<Option> options, String selectedOptionAsCorrect) {
        String answerString = null;
        String[] listOfOptions = new String[4];
        int i = 0;
        for (Option option : options) {
            listOfOptions[i] = option.getOptionText();
            System.out.println("At index " + i + " is " + option.getOptionText());
            i++;
        }
        if(selectedOptionAsCorrect.equals("a")){
            answerString = listOfOptions[0];
        }
        else if(selectedOptionAsCorrect.equals("b")){
            answerString = listOfOptions[1];
        }
        else if(selectedOptionAsCorrect.equals("c")){
            answerString = listOfOptions[2];
        }
        else if(selectedOptionAsCorrect.equals("d")){
            answerString = listOfOptions[3];
        }

        System.out.println("Answer is " + answerString);

        return answerString;        

    }

}

at this point I want to get out put on the console as

Set<Option> size is 4
At index 0 is Option 1
At index 1 is Option 2
At index 2 is Option 3
At index 3 is Option 4
Answer is Option 1
Option 1

means as I'm adding Option 1, Option 2,Option 3,Option 4 in sequence I want to copy those Strings to String[] in same order as

listOfOptions[0] should contian Option 1
listOfOptions[1] should contian Option 2
listOfOptions[2] should contian Option 3
listOfOptions[3] should contian Option 4

but I'm getting output as

Set<Option> size is 4
At index 0 is Option 3
At index 1 is Option 1
At index 2 is Option 2
At index 3 is Option 4
Answer is Option 3
Option 3

I want to ask why listOfOptions[0] do contian Option 3
that should contain Option 1, how to fix this are there isn't any solution or what is the logic behind the scenes can any one explain please. t

A ton of thanks in advance

like image 813
Logical Error Avatar asked Dec 06 '22 18:12

Logical Error


2 Answers

Use LinkedHashSet<> implementation instead of HashSet<>/TreeSet<> if you want to get elements in the same order in which they had been added.

Set<Option> options = new LinkedHashSet<>();
...
options.stream().forEach(System.out::println);
// prints all elements in the order which you'll expect

Read more at Oracle Tutorials. Also I together with Willy du Preez suggest you looking at lists. In your case, they may have a place to exist.

like image 120
Andrew Tobilko Avatar answered Dec 08 '22 08:12

Andrew Tobilko


See the Javadoc of HashSet:

It makes no guarantees as to the iteration order of the set

If you want the iteration order to be the insertion order, you can use LinkedHashSet instead:

This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order)

like image 38
Andy Turner Avatar answered Dec 08 '22 07:12

Andy Turner