This file contains 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
public static void convertUsingForLoop(){ | |
List<Integer> inputList = Arrays.asList(2,4,6,8,10); | |
int[] outputArray = new int[inputList.size()]; | |
for(int i = 0; i < inputList.size();i++){ | |
outputArray[i] = inputList.get(i); | |
} | |
//print the elements in the array | |
for (int num : outputArray) { | |
System.out.print(num + " "); | |
} |
This file contains 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
public static void convertUsingStream() { | |
List<Integer> inputList = Arrays.asList(2, 4, 6, 8, 10); | |
Stream<Integer> inputStream = inputList.stream(); | |
Object[] objArray = inputStream.toArray(); | |
//print the elements in the array | |
for (Object num : objArray) { | |
System.out.print(num + " "); | |
} | |
} |
This file contains 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
public static void convertUsingStream2() { | |
List<Integer> inputList = Arrays.asList(2, 4, 6, 8, 10); | |
Stream<Integer> inputStream = inputList.stream(); | |
Integer[] intArray = inputStream.toArray((num) -> new Integer[num]); | |
//print the elements in the array | |
for (int num : intArray) { | |
System.out.print(num + " "); | |
} | |
} |
This file contains 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
public static void convertUsingToArray() { | |
List<Integer> inputList = Arrays.asList(2, 4, 6, 8, 10); | |
Object[] objArray = inputList.toArray(); | |
//print the elements in the array | |
for (Object num : objArray) { | |
System.out.print(num + " "); | |
} | |
} |
This file contains 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
public static void convertUsingToArray2() { | |
List<Integer> inputList = Arrays.asList(2, 4, 6, 8, 10); | |
Integer[] outputArray = inputList.toArray(new Integer[inputList.size()]); | |
//print the elements in the array | |
for (int num : outputArray) { | |
System.out.print(num + " "); | |
} | |
} |
This file contains 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
public static void usingForLoop(){ | |
List<Integer> input = Arrays.asList(5,10,15,20,10,5,35,40,10,25); | |
List<Integer> output = new ArrayList<Integer>(); | |
for(Integer num:input){ | |
if(!output.contains(num)){ | |
output.add(num); | |
} | |
} | |
for(Integer num:output){ | |
System.out.print(num+" "); |
This file contains 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
public static void usingHashSet(){ | |
List<Integer> input = Arrays.asList(5,10,15,20,10,5,35,40,10,25); | |
Set<Integer> output = new HashSet<Integer>(input); | |
for(Integer num:output){ | |
System.out.print(num+" "); | |
} | |
} |
This file contains 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
public static void usingLinkedHashSet(){ | |
List<Integer> input = Arrays.asList(5,10,15,20,10,5,35,40,10,25); | |
Set<Integer> output = new LinkedHashSet<Integer>(input); | |
for(Integer num:output){ | |
System.out.print(num+" "); | |
} | |
} |
This file contains 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
public static void usingSetAddAll(){ | |
List<Integer> input = Arrays.asList(5,10,15,20,10,5,35,40,10,25); | |
Set<Integer> output = new LinkedHashSet<Integer>(); | |
output.addAll(input); | |
for(Integer num:output){ | |
System.out.print(num+" "); | |
} | |
} |
This file contains 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
public static void usingStream(){ | |
List<Integer> input = Arrays.asList(5,10,15,20,10,5,35,40,10,25); | |
Stream<Integer> streamWithDuplicates = input.stream(); | |
Stream<Integer> streamWithoutDuplicates = streamWithDuplicates.distinct(); | |
List<Integer> output = streamWithoutDuplicates.collect(Collectors.toList()); | |
for(Integer num:output){ | |
System.out.print(num+" "); | |
} | |
} |
OlderNewer