From 5dcaaffbbc3610a596672b2802684070b50daa4c Mon Sep 17 00:00:00 2001 From: Du Yuanchao Date: Mon, 8 Nov 2021 00:04:29 +0800 Subject: [PATCH] update list and map --- .../basics/collection/CollectionsExample.java | 4 + .../basics/collection/ListExample.java | 4 + .../basics/collection/StackExample.java | 4 + .../examplehub/basics/HashMapExampleTest.java | 28 +++--- .../collection/CollectionsExampleTest.java | 60 ++++++++++++ .../basics/collection/ListExampleTest.java | 70 +++++++++++++ .../basics/set/HashSetExampleTest.java | 44 +++++++++ .../basics/set/LinkedListExampleTest.java | 20 ++++ .../basics/set/TreeSetExampleTest.java | 98 ++++++++++++++++++- 9 files changed, 315 insertions(+), 17 deletions(-) create mode 100644 src/main/java/com/examplehub/basics/collection/CollectionsExample.java create mode 100644 src/main/java/com/examplehub/basics/collection/ListExample.java create mode 100644 src/main/java/com/examplehub/basics/collection/StackExample.java create mode 100644 src/test/java/com/examplehub/basics/collection/CollectionsExampleTest.java create mode 100644 src/test/java/com/examplehub/basics/collection/ListExampleTest.java create mode 100644 src/test/java/com/examplehub/basics/set/HashSetExampleTest.java create mode 100644 src/test/java/com/examplehub/basics/set/LinkedListExampleTest.java diff --git a/src/main/java/com/examplehub/basics/collection/CollectionsExample.java b/src/main/java/com/examplehub/basics/collection/CollectionsExample.java new file mode 100644 index 00000000..ce11136b --- /dev/null +++ b/src/main/java/com/examplehub/basics/collection/CollectionsExample.java @@ -0,0 +1,4 @@ +package com.examplehub.basics.collection; + +public class CollectionsExample { +} diff --git a/src/main/java/com/examplehub/basics/collection/ListExample.java b/src/main/java/com/examplehub/basics/collection/ListExample.java new file mode 100644 index 00000000..6ae58660 --- /dev/null +++ b/src/main/java/com/examplehub/basics/collection/ListExample.java @@ -0,0 +1,4 @@ +package com.examplehub.basics.collection; + +public class ListExample { +} diff --git a/src/main/java/com/examplehub/basics/collection/StackExample.java b/src/main/java/com/examplehub/basics/collection/StackExample.java new file mode 100644 index 00000000..e0981c2e --- /dev/null +++ b/src/main/java/com/examplehub/basics/collection/StackExample.java @@ -0,0 +1,4 @@ +package com.examplehub.basics.collection; + +public class StackExample { +} diff --git a/src/test/java/com/examplehub/basics/HashMapExampleTest.java b/src/test/java/com/examplehub/basics/HashMapExampleTest.java index f219ce92..5098759a 100644 --- a/src/test/java/com/examplehub/basics/HashMapExampleTest.java +++ b/src/test/java/com/examplehub/basics/HashMapExampleTest.java @@ -4,6 +4,7 @@ import java.util.HashMap; import java.util.Map; + import org.junit.jupiter.api.Test; class HashMapExampleTest { @@ -11,15 +12,18 @@ class HashMapExampleTest { @Test void testPut() { HashMap hashMap = new HashMap<>(); - hashMap.put("username", "admin"); - hashMap.put("password", "abc123"); + assertNull(hashMap.put("username", "admin")); + assertNull(hashMap.put("password", "abc123")); assertEquals("{password=abc123, username=admin}", hashMap.toString()); assertNull(hashMap.put("bio", "Github")); assertEquals("Github", hashMap.put("bio", "I love coding")); - hashMap.put("username", "admin_username"); + assertEquals("admin", hashMap.put("username", "admin_username")); assertEquals("admin_username", hashMap.get("username")); + + assertEquals("{password=abc123, bio=I love coding, username=admin_username}", + hashMap.toString()); } @Test @@ -27,7 +31,7 @@ void testPutAll() { Map map = Map.of("username", "admin", "password", "abc123"); HashMap hashMap = new HashMap<>(); hashMap.putAll(map); - System.out.println(hashMap); + assertEquals("{password=abc123, username=admin}", hashMap.toString()); } @Test @@ -118,10 +122,10 @@ void testKeySet() { assertEquals("[password, username]", hashMap.keySet().toString()); String[][] keyValues = - new String[][] { - {"password", "abc123"}, - {"username", "admin"} - }; + new String[][]{ + {"password", "abc123"}, + {"username", "admin"} + }; int index = 0; for (String key : hashMap.keySet()) { assertEquals(keyValues[index][0], key); @@ -153,10 +157,10 @@ void testEntry() { hashMap.put("username", "admin"); hashMap.put("password", "abc123"); String[][] keyValues = - new String[][] { - {"password", "abc123"}, - {"username", "admin"} - }; + new String[][]{ + {"password", "abc123"}, + {"username", "admin"} + }; int index = 0; for (Map.Entry entry : hashMap.entrySet()) { assertEquals(keyValues[index][0], entry.getKey()); diff --git a/src/test/java/com/examplehub/basics/collection/CollectionsExampleTest.java b/src/test/java/com/examplehub/basics/collection/CollectionsExampleTest.java new file mode 100644 index 00000000..771056ee --- /dev/null +++ b/src/test/java/com/examplehub/basics/collection/CollectionsExampleTest.java @@ -0,0 +1,60 @@ +package com.examplehub.basics.collection; + +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class CollectionsExampleTest { + @Test + void testMax() { + List list = Arrays.asList(1, 3, 5, 2, 4, 6); + assertEquals(6, Collections.max(list)); + } + + @Test + void testMin() { + List list = Arrays.asList(1, 3, 5, 2, 4, 6); + assertEquals(1, Collections.min(list)); + } + + @Test + void testReverse() { + List list = Arrays.asList(1, 3, 5, 2, 4, 6); + Collections.reverse(list); + assertEquals("[6, 4, 2, 5, 3, 1]", list.toString()); + } + + @Test + void testSwap() { + List list = Arrays.asList(1, 3, 5, 2, 4, 6); + Collections.swap(list, 0, list.size() - 1); + assertTrue(list.get(0) == 6 && list.get(list.size() - 1) == 1); + } + + @Test + void testSort() { + List list = Arrays.asList(1, 3, 5, 2, 4, 6); + Collections.sort(list); + assertEquals("[1, 2, 3, 4, 5, 6]", list.toString()); + } + + @Test + void testShuffle() { + List list = Arrays.asList(1, 2, 3, 4, 5, 6); + Collections.shuffle(list); + System.out.println(list); + } + + @Test + void testCopy() { + List src = Arrays.asList(1, 2, 3, 4, 5, 6); + List dist = Arrays.asList(new Integer[src.size()]); + Collections.copy(dist, src); + assertEquals("[1, 2, 3, 4, 5, 6]", dist.toString()); + } +} \ No newline at end of file diff --git a/src/test/java/com/examplehub/basics/collection/ListExampleTest.java b/src/test/java/com/examplehub/basics/collection/ListExampleTest.java new file mode 100644 index 00000000..56f7e2bb --- /dev/null +++ b/src/test/java/com/examplehub/basics/collection/ListExampleTest.java @@ -0,0 +1,70 @@ +package com.examplehub.basics.collection; + +import org.junit.jupiter.api.Test; + +import java.util.LinkedList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +public class ListExampleTest { + @Test + void testAdd() { + List list = new LinkedList<>(); + list.add("1"); + list.add("2"); + list.add("3"); + assertEquals("[1, 2, 3]", list.toString()); + } + + @Test + void testAddAll() { + List list = new LinkedList<>(); + list.add("1"); + list.add("2"); + list.add("3"); + + List list2 = new LinkedList<>(); + list2.add("4"); + list2.add("5"); + list2.add("6"); + + list.addAll(list2); + assertEquals("[1, 2, 3, 4, 5, 6]", list.toString()); + } + + @Test + void testGet() { + List list = new LinkedList<>(); + list.add("1"); + list.add("2"); + list.add("3"); + for (int i = 0; i < 3; ++i) { + assertEquals(i + 1 + "", list.get(i)); + } + } + + @Test + void testSet() { + List list = new LinkedList<>(); + list.add("1"); + list.add("2"); + list.add("3"); + + assertEquals("2", list.set(1, "22")); + assertEquals("22", list.get(1)); + } + + @Test + void testRemove() { + List list = new LinkedList<>(); + list.add("1"); + list.add("2"); + list.add("3"); + + for (int i = 1; i <= 3; ++i) { + assertEquals(i + "", list.remove(0)); + } + assertEquals("[]", list.toString()); + } +} diff --git a/src/test/java/com/examplehub/basics/set/HashSetExampleTest.java b/src/test/java/com/examplehub/basics/set/HashSetExampleTest.java new file mode 100644 index 00000000..a735b4a7 --- /dev/null +++ b/src/test/java/com/examplehub/basics/set/HashSetExampleTest.java @@ -0,0 +1,44 @@ +package com.examplehub.basics.set; + +import org.junit.jupiter.api.Test; + +import java.util.HashSet; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.*; + +class HashSetExampleTest { + @Test + void testAdd() { + Set set = new HashSet<>(); + assertTrue(set.add(2)); + assertFalse(set.add(2)); + assertTrue(set.add(3)); + assertTrue(set.add(1)); + assertEquals("[1, 2, 3]", set.toString()); + } + + @Test + void testRemove() { + Set set = new HashSet<>(); + set.add(2); + set.add(3); + set.add(1); + assertTrue(set.remove(2)); + assertTrue(set.remove(1)); + assertFalse(set.remove(1)); + assertEquals("[3]", set.toString()); + } + + @Test + void testClear() { + Set set = new HashSet<>(); + set.add(2); + set.add(3); + set.add(1); + assertEquals("[1, 2, 3]", set.toString()); + + set.clear(); + assertEquals("[]", set.toString()); + } +} \ No newline at end of file diff --git a/src/test/java/com/examplehub/basics/set/LinkedListExampleTest.java b/src/test/java/com/examplehub/basics/set/LinkedListExampleTest.java new file mode 100644 index 00000000..61a886ee --- /dev/null +++ b/src/test/java/com/examplehub/basics/set/LinkedListExampleTest.java @@ -0,0 +1,20 @@ +package com.examplehub.basics.set; + +import org.junit.jupiter.api.Test; + +import java.util.LinkedHashSet; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.*; + +class LinkedListExampleTest { + @Test + void testAdd() { + Set set = new LinkedHashSet<>(); + assertTrue(set.add(2)); + assertFalse(set.add(2)); + assertTrue(set.add(3)); + assertTrue(set.add(1)); + assertEquals("[2, 3, 1]", set.toString()); + } +} \ No newline at end of file diff --git a/src/test/java/com/examplehub/basics/set/TreeSetExampleTest.java b/src/test/java/com/examplehub/basics/set/TreeSetExampleTest.java index 80e4e54d..39785c34 100644 --- a/src/test/java/com/examplehub/basics/set/TreeSetExampleTest.java +++ b/src/test/java/com/examplehub/basics/set/TreeSetExampleTest.java @@ -2,18 +2,106 @@ import static org.junit.jupiter.api.Assertions.*; +import java.util.Comparator; import java.util.Set; import java.util.TreeSet; + import org.junit.jupiter.api.Test; class TreeSetExampleTest { @Test - void testTreeSet() { + void testAdd() { Set set = new TreeSet<>(); - set.add("B"); - set.add("A"); - set.add("C"); - set.add("D"); + assertTrue(set.add("B")); + assertTrue(set.add("A")); + assertTrue(set.add("C")); + assertTrue(set.add("D")); assertEquals("[A, B, C, D]", set.toString()); } + + @Test + void testComparable() { + class Student implements Comparable { + private final String name; + private final int age; + + public Student(String name, int age) { + this.name = name; + this.age = age; + } + + public String getName() { + return name; + } + + public int getAge() { + return age; + } + + @Override + public String toString() { + return "Student{" + + "name='" + name + '\'' + + ", age=" + age + + '}'; + } + + @Override + public int compareTo(Student o) { + return this.getAge() - o.getAge(); + } + } + + Student s1 = new Student("Jack", 23); + Student s2 = new Student("Tom", 22); + Student s3 = new Student("Zara", 21); + + Set set = new TreeSet<>(); + set.add(s1); + set.add(s2); + set.add(s3); + + assertEquals("[Student{name='Zara', age=21}, Student{name='Tom', age=22}, Student{name='Jack', age=23}]" + , set.toString()); + } + + @Test + void testComparator() { + class Student{ + private final String name; + private final int age; + + public Student(String name, int age) { + this.name = name; + this.age = age; + } + + public String getName() { + return name; + } + + public int getAge() { + return age; + } + @Override + public String toString() { + return "Student{" + + "name='" + name + '\'' + + ", age=" + age + + '}'; + } + } + + Student s1 = new Student("Jack", 23); + Student s2 = new Student("Tom", 22); + Student s3 = new Student("Zara", 21); + + Set set = new TreeSet<>((o1, o2) -> o1.getAge() - o2.getAge()); + set.add(s1); + set.add(s2); + set.add(s3); + + assertEquals("[Student{name='Zara', age=21}, Student{name='Tom', age=22}, Student{name='Jack', age=23}]" + , set.toString()); + } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy