This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.ArrayList; | |
| import java.util.Comparator; | |
| import java.util.List; | |
| public class SortListUsingComparatorNullsLast { | |
| public static void main(String[] args) { | |
| List<Integer> list = new ArrayList<>(){{ | |
| add(6); | |
| add(null); | |
| add(8); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.ArrayList; | |
| import java.util.Comparator; | |
| import java.util.List; | |
| public class SortListUsingComparatorReverseOrder { | |
| public static void main(String[] args) { | |
| List<Integer> list = new ArrayList<>(List.of(3, 6, 8, 2)); | |
| list.sort(Comparator.reverseOrder()); | |
| System.out.println(list);//output [8, 6, 3, 2] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.ArrayList; | |
| import java.util.Comparator; | |
| import java.util.List; | |
| public class SortListUsingComparatorNaturalOrder { | |
| public static void main(String[] args) { | |
| List<Integer> list = new ArrayList<>(List.of(3, 6, 8, 2)); | |
| list.sort(Comparator.naturalOrder()); | |
| System.out.println(list);//output [2, 3, 6, 8] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.ArrayList; | |
| import java.util.Iterator; | |
| import java.util.List; | |
| import java.util.ListIterator; | |
| public class IteratingOverList { | |
| public static void main(String[] args) { | |
| List<String> list = new ArrayList<>(){{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.ArrayList; | |
| import java.util.List; | |
| public class RemoveElementsFromList { | |
| public static void main(String[] args) { | |
| List<String> list = new ArrayList<>(List.of("java","is", "is", "awesome")); | |
| // remove element at index 2 | |
| list.remove(2); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.ArrayList; | |
| import java.util.List; | |
| public class UpdateElementsInList { | |
| public static void main(String[] args) { | |
| List<Integer> intList = new ArrayList<>(List.of(1, 2, 3)); | |
| // all elements will be updated | |
| // output: [2, 4, 6] | |
| intList.replaceAll(e -> e * 2); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.ArrayList; | |
| import java.util.List; | |
| public class FindingElementInList { | |
| public static void main(String[] args) { | |
| List<Integer> list1 = new ArrayList<>(List.of(6, 1, 2, 3, 4, 5, 6)); | |
| List<Integer> list2 = new ArrayList<>(List.of(1, 2, 3)); | |
| // output: contains 1: true | |
| System.out.println("contains 1: " + list1.contains(1)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.ArrayList; | |
| import java.util.List; | |
| public class ListInterfaceAddElements { | |
| public static void main(String[] args) { | |
| List<Integer> list = new ArrayList<>(); | |
| list.add(1); | |
| list.add(3); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Collection; | |
| import java.util.HashSet; | |
| import java.util.Set; | |
| public class CollectionInterfaceMethodsToPerformSetOperations { | |
| static Collection<Integer> firstSet = new HashSet<>(Set.of(1, 2, 3, 4, 5, 6)); | |
| static Collection<Integer> secondSet = new HashSet<>(Set.of(4, 5, 6, 7, 8, 9)); | |
| static Collection<Integer> smallSet = new HashSet<>(Set.of(1, 2, 3)); | |
| public static void subSet(){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.ArrayList; | |
| import java.util.Collection; | |
| import java.util.Iterator; | |
| import java.util.List; | |
| public class CollectionInterfaceTraversalMethods { | |
| public static void main(String[] args) { | |
| Collection<Integer> collection1 = new ArrayList<>(List.of(1, 2, 3, 4, 5, 6)); |