vendredi 18 août 2017

Java - If statement in foreach not called

I am trying to check if a certain string is in a collection of strings. (The strings in this case are Topics from a activemq broker) So basically I iterate through a topic list and compare those topics with the searched one (save in variable "compare"). "topic.getTopicName()" definately returns a string so I do not get why the varaible count is not set to 1 eventhough the condition is true in one case. So the statement in the IF clause is never executed. Am I overlooking something?

 public ArrayList<String> getTopics() throws RemoteException {



            try {

                 // get Topics as Strings from Broker

                ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
                ActiveMQConnection connection;
                connection = (ActiveMQConnection) connectionFactory.createConnection();
                connection.start();
                DestinationSource ds = connection.getDestinationSource();

                Set<ActiveMQTopic>  topics = ds.getTopics();
                String compare = "Physical";
                int count = 0;


                for(ActiveMQTopic topic : topics){


                        System.out.println(topic.getTopicName());
                        if(compare == topic.getTopicName()) {

                            System.out.println("Found " + topic.getTopicName());
                            count = count + 1;


                        }

                } 
                    if(count == 0){


                        System.out.println("No topic found");

                    }

                    else    System.out.println("Topic found");







            } catch (JMSException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }







            return null;

 }

Aucun commentaire:

Enregistrer un commentaire