-
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d64cbef
commit 7b11488
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
title: Remove duplicates | ||
description: Removes duplicate elements from an list | ||
author: Mcbencrafter | ||
tags: list,duplicates,unique | ||
--- | ||
|
||
```java | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public static <T> List<T> removeDuplicates(List<T> list) { | ||
return list.stream() | ||
.distinct() | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
// Usage: | ||
List<Integer> list = List.of(1, 2, 3, 4, 5, 1, 2, 3, 4, 5); | ||
List<Integer> result = removeDuplicates(list); | ||
System.out.println("List with duplicates removed: " + result); // [1, 2, 3, 4, 5] | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
title: Reverse list | ||
description: Reverses the contents of an list | ||
author: Mcbencrafter | ||
tags: list,reverse | ||
--- | ||
|
||
```java | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public static <T> List<T> reverseList(List<T> list) { | ||
List<T> reversedList = new ArrayList<>(list); | ||
Collections.reverse(reversedList); | ||
return reversedList; | ||
} | ||
|
||
// Usage: | ||
List<Integer> list = List.of(1, 3, 5, 7, 5); | ||
List<Integer> result = reverseList(lst); | ||
System.out.println("List reversed: " + result); // [5, 7, 5, 3, 1] | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
title: Sort list | ||
description: Sorts the contents of an numeric list | ||
author: Mcbencrafter | ||
tags: list,sorting,numberic,order,ascending,descending | ||
--- | ||
|
||
```java | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public static <T extends Number & Comparable<T>> List<T> sortNumericList(List<T> list, boolean ascending) { | ||
return list.stream() | ||
.sorted(ascending ? Comparator.naturalOrder() : Comparator.reverseOrder()) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
// Usage: | ||
List<Integer> list = List.of(5, 3, 1, 4, 2); | ||
List<Integer> result = sortNumericList(list, true); | ||
System.out.println("List sorted in ascending order: " + result); // [1, 2, 3, 4, 5] | ||
result = sortNumericList(list, false); | ||
System.out.println("List sorted in descending order: " + result); // [5, 4, 3, 2, 1] | ||
``` |