5 very useful ArrayList methods to remember in Java
These 5 methods will be very helpful to do away with Iterators, ConcurrentModificationException and generally tricky collection scenarios.
Let’s create two Arraylist objects to use the methods with;
- List<City> list1 = new ArrayList<>();
- List<City> list2 = new ArrayList<>();
And let’s create a class City and generate the equals, hashcode, toString and a constructor for it.
- class City{
- String name;
- @Override
- public String toString() {
- return "City [name=" + name + "]";
- }
- public Object startsWith(String string) {
- // TODO Auto-generated method stub
- return null;
- }
- public City(String name) {
- super();
- this.name = name;
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((name == null) ? 0 : name.hashCode());
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- City other = (City) obj;
- if (name == null) {
- if (other.name != null)
- return false;
- } else if (!name.equals(other.name))
- return false;
- return true;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
Now fill the 2 lists with City objects,
- list1.add(new City("Berlin"));
- list1.add(new City("London"));
- list1.add(new City("Tokyo"));
- list2.add(new City("New Delhi"));
- list2.add(new City("Moscow"));
- list2.add(new City("Paris"));
- list2.add(new City("Tokyo"));
Lets print the original first list before, the list will show the contained objects in readable format as we have implemented the toString method.
- System.out.println("Orignal List");
- System.out.println(list1);
- Orignal List
- [City [name=Berlin], City [name=London], City [name=Tokyo]]
- retainAll: removes from this list all of its elements that are not contained in the specified collection.
- list1.retainAll(list2);
- System.out.println("\nAfter retainAll:"+list1);
So here we will remove all the element not present in the second list, thus only Tokyo remains in the list.
This will help you in preventing the use of Iterators.
Result: After retainAll:[City [name=Tokyo]]
2) addAll : Appends all of the elements in the specified collection to the end of this list
- System.out.println("\nAfter addAll:"+list1);
- list1.containsAll(list2);
This will add all the elements from the second list to the first one.No need and Iterate and Add.
Result:
After addAll:[City [name=Tokyo], City [name=New Delhi], City [name=Moscow], City [name=Paris], City [name=Tokyo]]
3) containsAll: Returns true if this list contains all of the elements of the specified collection.
- System.out.println("\nResult of containsAll :"+list1.containsAll(list2));
As the first list contains all the elements of the second list it returns true.
Result of containsAll : true
Result of containsAll : true
4) Streams have been much talked addition on java8 ;
- list1.stream().findFirst().ifPresent(System.out::println);
As you can read easily, get me the stream and then the first element if it exits, here is the result;
Stream gets you the first element:City [name=Tokyo]
5) clear() : Removes all of the elements from this list
- list1.clear();
Result: Cleared:[]
I will emphasize that using these methods make tasks easy and bug free, also Exceptions and unnecessary code can be prevented.
Comments
Post a Comment