Alternative addition of elements from two ArrayList
public static List<String> addAlternateList() {
// TODO Auto-generated method stub
List<String> list1 = Arrays.asList("data01", "data02", "data03", "data04", "data05", "data06", "data07",
"data08");
List<String> list2 = Arrays.asList("John", "Peter", "Dave", "Amanda", "Kay", "Scott", "Atkingson", "Bob");
Iterator<String> iterator1 = list1.iterator();
Iterator<String> iterator2 = list2.iterator();
List<String> newList = new ArrayList<>();
while (iterator1.hasNext() || iterator2.hasNext()) {
if (iterator1.hasNext()) {
newList.add(iterator1.next());
}
if (iterator2.hasNext()) {
newList.add(iterator2.next());
}
}
return newList;
}
OUTPUTdata01
John
data02
Peter
data03
Dave
data04
Amanda
data05
Kay
data06
Scott
data07
Atkingson
data08
Bob
Adding two list
public static ArrayList<String> mergeList() {
List<String> list1 = Arrays.asList("data01", "data02", "data03", "data04", "data05", "data06", "data07",
"data08");
List<String> list2 = Arrays.asList("John", "Peter", "Dave", "Amanda", "Kay", "Scott", "Atkingson", "Bob");
ArrayList<String> newList = new ArrayList<>(list1);
newList.addAll(list2);
return newList;
}
OUTPUTdata01
data02
data03
data04
data05
data06
data07
data08
John
Peter
Dave
Amanda
Kay
Scott
Atkingson
Bob
Main Method Implementation
public static void main(String[] args) {
List<String> alternateList = addAlternateList();
Iterator<String> iterator1 = alternateList.iterator();
while(iterator1.hasNext())
{
System.out.println(iterator1.next());
}
System.out.println("******************************");
ArrayList<String> mergeList = mergeList();
Iterator<String> iterator2 = mergeList.iterator();
while(iterator2.hasNext())
{
System.out.println(iterator2.next());
}
}
List sorting
public static void sortList()
{
List<String> list2 = Arrays.asList("John", "Peter", "Dave", "Amanda", "Kay", "Scott", "Atkingson", "Bob");
System.out.println("Before Sorting " + list2);
Collections.sort(list2);
System.out.println("After Sorting " + list2);
//sorting
Collections.sort(list2,String.CASE_INSENSITIVE_ORDER);
}
No comments:
Post a Comment