-2

Lets say I have a class

public class Ttype{
    
    private String type = "";

    public Ttype(String type) {
        
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}

and I have arraylist of this class

ArrayList<Ttype> type = new ArrayList<Ttype>();

I have added some elements to the arraylist

type.add( new new Ttype("Hello"));
type.add( new new Ttype("Bye"));
type.add( new new Ttype("Hi"));

I want to be able to return a string when I search for specefic string in the arraylist. What I mean by that is:

Ttype t = type.get("Hello"); //t will be set to "hello" if hello is in the arraylist.

How can I do that?

5
  • could it be, that you are looking for a map instead of an arraylist? map.put("hello", new Ttype("Hello")); and map.get("Hello") //returns a object of type Ttype Commented Oct 20, 2020 at 10:49
  • I have never actually heard of map before. I had enums before with different elements in it. Public Types Enum{ Hi, Hello, Bye}. Before I had like this Types t = Types.Bye; and then they can t value whenever then want eg. if( t != types.bye) t = types.hello; I want to do something similar but with arraylist. Like instead I have arraylist<Types> type = new Arraylist<Types>(); . I want to change my whole enum structur with arraylist instead. So Ttypes t = type.get("bye") and so on. I do not know if I am thinking wrong or if is it a way to go. Commented Oct 20, 2020 at 11:03
  • Simple: java collections have lists, sets, and maps. And you better learn what these are about. Jarlik is fully correct. You want to use a simple map for this task. Commented Oct 20, 2020 at 11:09
  • I recommend this tutorial Commented Oct 20, 2020 at 11:19
  • 1
    Does this answer your question? Searching in a ArrayList with custom objects for certain strings Did you try searching the Internet for the words java search arraylist before posting your question? Commented Oct 20, 2020 at 11:24

2 Answers 2

0
type.stream()
  .filter(c -> c.getType().equals("test"))
  .collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

1 Comment

0

Well as others suggested in comments this will be much easy when you use a Map rather than ArrayList. But in your case to achieve what you need you can follow the below steps.This will be much easy when you use streams in Java 8.But I will provide a simple solution which you can achieve without streams.

Ttype result = null;//To store if an object found 
String searchTxt = "Hello";

for(Ttype temp:type){//Iterating through the list find a match

   if(temp.type.equlas(searchTxt)){
       result = temp;
   }

}

Now based on the value which contains in the result you can continue your work.If result is null after the iteration it means there is no matching item found.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.