-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?

Mohammed
  • 195
  • 1
  • 3
  • 12
  • 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` – Jarlik Stepsto Oct 20 '20 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 type = new Arraylist(); . 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. – Mohammed Oct 20 '20 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. – GhostCat Oct 20 '20 at 11:09
  • I recommend this [tutorial](https://docs.oracle.com/javase/tutorial/collections/) – Abra Oct 20 '20 at 11:19
  • 1
    Does this answer your question? [Searching in a ArrayList with custom objects for certain strings](https://stackoverflow.com/questions/12496038/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? – Abra Oct 20 '20 at 11:24

2 Answers2

0
type.stream()
  .filter(c -> c.getType().equals("test"))
  .collect(Collectors.toList());
  • [Is it my duty to check for duplicate questions before answering?](https://meta.stackoverflow.com/questions/326622/is-it-my-duty-to-check-for-duplicate-questions-before-answering) – Abra Oct 20 '20 at 11:24
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.

Hasindu Dahanayake
  • 1,344
  • 2
  • 14
  • 37