What is the output of the following code import javautil Lis

What is the output of the following code?
import java.util.*;


    List<String> list1 = new ArrayList<>();
    list1.add(\"Atlanta\");
    list1.add(\"Macon\");
    list1.add(\"Savanna\");
    
    List<String> list2 = new ArrayList<>();
    list2.add(\"Atlanta\");
    list2.add(\"Macon\");
    list2.add(\"Savanna\");
    
    List<String> list3 = new ArrayList<>();
    list3.add(\"Macon\");
    list3.add(\"Savanna\");
    list3.add(\"Atlanta\");
    
    System.out.println(list1.equals(list2) + \" \" + list1.equals(list3));

Solution

Ans: true false

list1.equals(list2) this gives--->true

Explanation:In list elements are stored in insertion order.elements are stored in index basis.
in list1 index0-->Atlanta and index1-->Macon and index2-->savanna are stored
in list2 index0-->Atlanta and index1-->Macon and index2-->savanna are stored
.equals method compare the values.as list1 and list2 contains the values in same order and same elemetns
it will give output as \"true\".


list.equals(list3) this give-->flase

Explanation:
in list1 index0-->Atlanta and index1-->Macon and index2-->savanna are stored
in list1 index0-->Macon and index1-->savanna and index2-->Atlanta are stored
here while comparing element on index basis they are not same so this returns false

What is the output of the following code? import java.util.*; List<String> list1 = new ArrayList<>(); list1.add(\

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site