-
Notifications
You must be signed in to change notification settings - Fork 0
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
9e98963
commit 37cd55e
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
src/main/java/com/collections/CollectionInterface/CollectionInterfaceDemo.java
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,30 @@ | ||
package com.collections.CollectionInterface; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Iterator; | ||
|
||
public class CollectionInterfaceDemo { | ||
public static void main(String[] args) { | ||
Collection<String> collection = new ArrayList<>(); | ||
String str0 = "Hello2"; | ||
collection.add(str0); | ||
Collection<String> collection2 = new ArrayList<>(); | ||
String str = "Hello"; | ||
collection.add(str); | ||
collection2.add("w"); | ||
collection.addAll(collection2); | ||
collection2.clear(); | ||
collection2.add("w"); | ||
Iterator<String> iterator = collection.iterator(); | ||
while (iterator.hasNext()) { | ||
iterator.next(); | ||
//iterator.remove(); | ||
} | ||
collection.remove(str); | ||
System.out.println(collection.contains("Hello2")); | ||
System.out.println(Arrays.toString(collection.toArray())); | ||
System.out.println(Arrays.toString(collection2.toArray())); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/com/collections/Comparator/ComparatorDemo.java
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,50 @@ | ||
package com.collections.Comparator; | ||
|
||
import java.util.Arrays; | ||
import java.util.Comparator; | ||
|
||
interface CompareById<T extends Student> extends Comparator<T> { | ||
int compare(T o1, T o2); | ||
} | ||
|
||
class Student implements Comparable<Student> { | ||
public String name; | ||
public int age; | ||
public int group; | ||
private final int id; | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public Student(String name, int age, int group, int id) { | ||
this.name = name; | ||
this.age = age; | ||
this.group = group; | ||
this.id = id; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Student{" + | ||
"name='" + name + '\'' + | ||
", age=" + age + | ||
", group=" + group + | ||
'}'; | ||
} | ||
|
||
@Override | ||
public int compareTo(Student o) { | ||
if (this.group != o.group) return this.group - o.group; | ||
if (this.name.compareTo(o.name) != 0) return this.name.compareTo(o.name); | ||
return this.age - o.age; | ||
} | ||
} | ||
|
||
public class ComparatorDemo { | ||
public static void main(String[] args) { | ||
Student[] students = {new Student("Ivan", 17, 10, 5), new Student("Petr", 21, 13, 1), new Student("Ivan", 25, 10, 1)}; | ||
Arrays.sort(students, Comparator.comparingInt(Student::getId)); | ||
System.out.println(Arrays.toString(students)); | ||
} | ||
} |