Get specific objects from an ArrayList when objects were added anonymously?

I created a short example of my problem. I create a list of anonymous objects and add them to the ArrayList . When the elements are in an ArrayList , I come back later and add more information to each object in the list. Is there a way to extract a specific object from a list if you don't know its index?

I only know the name of the object, but you cannot do list.get(ObjectName) or anything else. What is the recommended way to handle this? I would prefer not to iterate over the whole list every time I want to get one specific object.

 public class TestCode{ public static void main (String args []) { Cave cave = new Cave(); // Loop adds several Parties to the cave party list cave.parties.add(new Party("FirstParty")); // all anonymously added cave.parties.add(new Party("SecondParty")); cave.parties.add(new Party("ThirdParty")); // How do I go about setting the 'index' value of SecondParty for example? } } class Cave { ArrayList<Party> parties = new ArrayList<Party>(); } class Party extends CaveElement{ int index; public Party(String n){ name = n; } // getter and setter methods public String toString () { return name; } } class CaveElement { String name = ""; int index = 0; public String toString () { return name + "" + index; } } 
+6
source share
7 answers

Given the use of List , there is no way to "search" a value without repeating through it ...

For instance...

 Cave cave = new Cave(); // Loop adds several Parties to the cave party list cave.parties.add(new Party("FirstParty")); // all anonymously added cave.parties.add(new Party("SecondParty")); cave.parties.add(new Party("ThirdParty")); for (Party p : cave.parties) { if (p.name.equals("SecondParty") { p.index = ...; break; } } 

Now it will take some time. If the item you are looking for is at the end of the list, you will need to iterate to the end of the list before finding a match.

It might be better to use some kind of Map ...

So, if we update Cave to look like ...

 class Cave { Map<String, Party> parties = new HashMap<String, Party>(25); } 

We could do something like ...

 Cave cave = new Cave(); // Loop adds several Parties to the cave party list cave.parties.put("FirstParty", new Party("FirstParty")); // all anonymously added cave.parties.put("SecondParty", new Party("SecondParty")); cave.parties.put("ThirdParty", new Party("ThirdParty")); if (cave.parties.containsKey("SecondParty")) { cave.parties.get("SecondParty").index = ... } 

Instead of...

Ultimately, all this will depend on what you want to achieve ...

+11
source

List.indexOf() will give you what you want, if you know exactly what you need, and provided that the equals() for Party is clearly defined.

 Party searchCandidate = new Party("FirstParty"); int index = cave.parties.indexOf(searchCandidate); 

That's where it is interesting: subclasses should not study the private properties of their parents, so we define equals() in the superclass.

 @Override public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof CaveElement)) { return false; } CaveElement that = (CaveElement) o; if (index != that.index) { return false; } if (name != null ? !name.equals(that.name) : that.name != null) { return false; } return true; } 

It is also recommended that you override hashCode if you are overriding equals - the general contract for hashCode means that if x.equals(y) , then x.hashCode () == y.hashCode () .

 @Override public int hashCode() { int result = name != null ? name.hashCode() : 0; result = 31 * result + index; return result; } 
+5
source

If you want to search for objects based on their String name, this is a tutorial case for Map , say a HashMap . You can use LinkedHashMap and convert it to List or Array later (Chris described it well in the comments below).

LinkedHashMap because it allows you to access the elements in the order you insert them if you want to do this. Otherwise, a HashMap or TreeMap will be executed.

You can make it work with List , as others suggest, but it seems to Hacky to me .. and it will be cleaner both in the short and long term.

If you SHOULD use a list for an object, you can still save the Map name of the object in the index in the array. It's a little uglier, but you get pretty much the same performance as a regular Map .

+5
source

You can use the list.indexOf(Object) error, to be honest that you are describing as if you were better off using Map .

Try the following:

 Map<String, Object> mapOfObjects = new HashMap<String, Object>(); mapOfObjects.put("objectName", object); 

Then when you want to get the object use

 mapOfObjects.get("objectName"); 

Assuming you know the name of the object, as you stated, it will be both cleaner and faster, especially if the map contains a large number of objects.

If you want the objects in the Map remain in order, you can use

 Map<String, Object> mapOfObjects = new LinkedHashMap<String, Object>(); 

instead

+2
source

According to your question, I would suggest that the Card solve your problem very efficiently and without any hassle.

In Map, you can specify the name as a key, and your source object as a value.

  Map<String,Cave> myMap=new HashMap<String,Cave>(); 
+1
source

I would suggest redefining the equals(Object) your Party class. It might look something like this:

 public boolean equals(Object o){ if(o == null) return false; if(o instanceof String) return name.equalsIgnoreCase((String)o); else if(o instanceof Party) return equals(((Party)o).name); return false; } 

After that, you can use the indexOf(Object) method to retrieve the index of the participant specified by its name, as shown below:

 int index = cave.parties.indexOf("SecondParty"); 

Would SecondParty Party index named SecondParty .

Note. This only works because you override the equals(Object) method.

0
source

You can simply create a method to get an object by its name.

 public Party getPartyByName(String name) { for(Party party : parties) { if(name.equalsIgnoreCase(party.name)) { return party; } } return null; } 
0
source

Source: https://habr.com/ru/post/952555/


All Articles