[go: up one dir, main page]

Skip to content

Commit

Permalink
Added collections classes
Browse files Browse the repository at this point in the history
  • Loading branch information
syndersage committed Aug 5, 2022
1 parent 9e98963 commit 37cd55e
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
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 src/main/java/com/collections/Comparator/ComparatorDemo.java
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));
}
}

0 comments on commit 37cd55e

Please sign in to comment.