guyanqing 1 year ago
parent
commit
dc92fa53ec

+ 2 - 2
src/main/java/com/sf/day01/Test01.java

@@ -8,7 +8,7 @@ import java.sql.SQLOutput;
  * 1 - 在day01包下创建一个类    类名Test01
  * 1 - 在day01包下创建一个类    类名Test01
  * 2 - 当前类   class
  * 2 - 当前类   class
  * 3 - public  公共的  公开的     - 权限修饰符中的一种
  * 3 - public  公共的  公开的     - 权限修饰符中的一种
- * 4 - Test01  标识符
+ * 4 - Student  标识符
  *   标识符和关键字不同 :
  *   标识符和关键字不同 :
  *      关键字   - Java给我们定义好的   我们不能用关键字作为标识符
  *      关键字   - Java给我们定义好的   我们不能用关键字作为标识符
  *      标识符: 凡是自己起名字的都可以称之为标识符
  *      标识符: 凡是自己起名字的都可以称之为标识符
@@ -16,7 +16,7 @@ import java.sql.SQLOutput;
  *  com.sf.day01     包名   通常 包名定义  : 公司域名的倒叙
  *  com.sf.day01     包名   通常 包名定义  : 公司域名的倒叙
  *  www.baidu.com     -- 域名    196.168.xx.xx  ip
  *  www.baidu.com     -- 域名    196.168.xx.xx  ip
  *  47.96.190.116  --    www.guyanqing.com
  *  47.96.190.116  --    www.guyanqing.com
- *  Test01   类名
+ *  Student   类名
  */
  */
 public class Test01 {
 public class Test01 {
     String name = "zhangdan";
     String name = "zhangdan";

+ 1 - 1
src/main/java/com/sf/day07/getset/Test01.java

@@ -48,7 +48,7 @@ public class Test01 {
 
 
     @Override
     @Override
     public String toString() {
     public String toString() {
-        return "Test01{" +
+        return "Student{" +
                 "name='" + name + '\'' +
                 "name='" + name + '\'' +
                 ", id=" + id +
                 ", id=" + id +
                 ", age=" + age +
                 ", age=" + age +

+ 15 - 0
src/main/java/com/sf/day16/MyUserCompare.java

@@ -0,0 +1,15 @@
+package com.sf.day16;
+
+import java.util.Comparator;
+
+public class MyUserCompare implements Comparator {
+    @Override
+    public int compare(Object o1, Object o2) {
+        if(o1 instanceof User && o2 instanceof User){
+            User user1 = (User) o1;
+            User user2 = (User) o2;
+                return user1.age - user2.age;
+        }
+        throw new RuntimeException("类型不匹配");
+    }
+}

+ 135 - 0
src/main/java/com/sf/day16/Test01.java

@@ -0,0 +1,135 @@
+package com.sf.day16;
+
+import com.sf.day10.integerfacetest01.manyinterface.C;
+import org.junit.Test;
+
+import java.nio.channels.Pipe;
+import java.util.*;
+
+
+public class Test01 {
+
+    @Test
+    public void t1(){
+        LinkedList<String> list = new LinkedList<>();
+        list.add("a");
+        list.add("b");
+        list.add("c");
+        //调用addFirst();
+        list.addFirst("AAA");
+        System.out.println(list);
+        list.addLast("WWW");
+        System.out.println(list);
+        String first = list.getFirst();
+        System.out.println(first);
+        String last = list.getLast();
+        System.out.println(last);
+        boolean ee = list.add("ee");
+        System.out.println(list.getLast());
+
+        //for循环和foreach循环
+        //迭代器
+
+
+        System.out.println("=========");
+        Iterator<String> iterator = list.iterator();
+        while (iterator.hasNext()){
+            String next = iterator.next();
+            System.out.println(next);
+        }
+    }
+
+    @Test
+    public void t2(){
+        Vector<String> vector = new Vector<>();
+        vector.add("zhangsan");
+        vector.add("lisa");
+        vector.add("wangwu");
+//        for (int i = 0;i<=vector.size()-1;i++){
+//            String s = vector.get(i);
+//            System.out.println(s);
+//        }
+        for (String s : vector) {
+            System.out.println(s);
+        }
+
+        //迭代器
+        Iterator<String> iterator = vector.iterator();
+        while (iterator.hasNext()){
+            System.out.println(iterator.next());
+        }
+
+        //:Enumeration循环:
+        Enumeration<String> elements = vector.elements();
+        while (elements.hasMoreElements()){
+            System.out.println(elements.nextElement());
+        }
+    }
+
+    @Test
+    public void t3(){
+        Vector vc = new Vector();
+        vc.add("1");
+        vc.add("2");
+        vc.add("3");
+        for (Object o : vc) {
+            System.out.println(o);
+        }
+        vc.set(0,"100");
+        for (Object o : vc) {
+            System.out.println(o);
+        }
+    }
+
+    /**
+     * hashset
+     */
+    @Test
+    public void t4(){
+        HashSet<Integer> hashSet = new HashSet<>();
+        hashSet.add(1);
+        hashSet.add(2);
+        hashSet.add(3);
+        hashSet.add(3);  //去重
+        System.out.println(hashSet);
+
+
+        /**
+         * 去重
+         */
+        HashSet<ArrayList<String>> arrayLists1 = new HashSet<>();
+        ArrayList<String> strings = new ArrayList<>();
+        ArrayList<String> strings2 = new ArrayList<>();
+        strings.add("zhangsan");
+        strings.add("lisa");
+
+        strings2.add("zhangsan");
+        strings2.add("lisa");
+        arrayLists1.add(strings);
+        arrayLists1.add(strings2);
+
+        System.out.println(arrayLists1);
+
+
+    }
+
+
+    @Test
+    public void t5(){
+        /**
+         * 创建一个arraylist集合<Integer>   {1,2,3,4,5,6,6,7,7,8,8,9}  如何实现快速去重
+         * 将list集合转换成set集合
+         */
+        List<Integer> list = new ArrayList<>();
+        list.add(1);
+        list.add(2);
+        list.add(3);
+        list.add(3);
+        list.add(3);
+        list.add(4);
+        list.add(5);
+        HashSet<Integer> hashSet = new HashSet<>(list);
+        System.out.println(hashSet);
+
+    }
+}

+ 40 - 0
src/main/java/com/sf/day16/TestUser.java

@@ -0,0 +1,40 @@
+package com.sf.day16;
+
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.TreeSet;
+
+public class TestUser {
+
+    @Test
+    public void t1(){
+        TreeSet<User> treeSet = new TreeSet<User>(new Comparator<User>() {
+            @Override
+            public int compare(User o1, User o2) {
+                if(o1 instanceof User && o2 instanceof User){
+                    User user1 = (User) o1;
+                    User user2 = (User) o2;
+                    return user1.age - user2.age;
+                }
+                throw new RuntimeException("类型不匹配");
+            }
+        });
+        treeSet.add(new User("Tom",12));
+        treeSet.add(new User("Rose",23));
+        treeSet.add(new User("Jerry",2));
+        System.out.println(treeSet);
+    }
+
+//    @Override
+//    public int compare(Object o1, Object o2) {
+//        if(o1 instanceof User && o2 instanceof User){
+//            User user1 = (User) o1;
+//            User user2 = (User) o2;
+//            return user1.age - user2.age;
+//        }
+//        throw new RuntimeException("类型不匹配");
+//    }
+}

+ 24 - 0
src/main/java/com/sf/day16/User.java

@@ -0,0 +1,24 @@
+package com.sf.day16;
+
+import java.util.Comparator;
+
+public class User {
+    String name;
+    int age;
+
+    public User() {
+    }
+
+    public User(String name, int age) {
+        this.name = name;
+        this.age = age;
+    }
+
+    @Override
+    public String toString() {
+        return "User{" +
+                "name='" + name + '\'' +
+                ", age=" + age +
+                '}';
+    }
+}

+ 103 - 0
src/main/java/com/sf/day16/homework/Library.java

@@ -0,0 +1,103 @@
+package com.sf.day16.homework;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * **.现有若干图书信息(包含名称title、作者author、定价price)需要存储到set集合中,保证集合中无重复元素,并遍历查看。可以认为所有信息都相同的图书为重复数据。**
+ */
+public class Library {
+    /**
+     * 图书标题
+     */
+    private String title;
+    /**
+     * 图书作者
+     */
+    private String author;
+    /**
+     * 图书价格
+     */
+    private int price;
+
+
+    public Library() {
+    }
+
+    public Library(String title, String author, int price) {
+        this.title = title;
+        this.author = author;
+        this.price = price;
+    }
+
+    public String getTitle() {
+        return title;
+    }
+
+    public void setTitle(String title) {
+        this.title = title;
+    }
+
+    public String getAuthor() {
+        return author;
+    }
+
+    public void setAuthor(String author) {
+        this.author = author;
+    }
+
+    public int getPrice() {
+        return price;
+    }
+
+    public void setPrice(int price) {
+        this.price = price;
+    }
+
+    @Override
+    public String toString() {
+        return "title=" + title +
+                ", author=" + author +
+                ", price=" + price ;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+
+        Library library = (Library) o;
+
+        if (price != library.price) return false;
+        if (title != null ? !title.equals(library.title) : library.title != null) return false;
+        return author != null ? author.equals(library.author) : library.author == null;
+
+    }
+
+    @Override
+    public int hashCode() {
+        int result = title != null ? title.hashCode() : 0;
+        result = 31 * result + (author != null ? author.hashCode() : 0);
+        result = 31 * result + price;
+        return result;
+    }
+
+
+    public static void main(String[] args) {
+        Set<Library> set = new HashSet<>();
+
+        Library library1 = new Library("语文","鲁迅",20);
+        Library library2 = new Library("活着","余华",25);
+        Library library3 = new Library("恶意","东野圭吾",18);
+        Library library4 = new Library("活着","余华",25);
+
+        set.add(library1);
+        set.add(library2);
+        set.add(library3);
+        set.add(library4);
+
+        for (Library library : set){
+            System.out.println(library);
+        }
+    }
+}

+ 91 - 0
src/main/java/com/sf/day16/homework/Student.java

@@ -0,0 +1,91 @@
+package com.sf.day16.homework;
+
+import java.util.Set;
+import java.util.TreeSet;
+
+public class Student implements Comparable<Student>{
+    /**
+     * 学生姓名
+     */
+    private String name;
+    /**
+     * 学生年龄
+     */
+    private int age;
+    /**
+     * 学生成绩
+     */
+    private int score;
+
+    public Student() {
+    }
+
+    public Student(String name, int age, int score) {
+        this.name = name;
+        this.age = age;
+        this.score = score;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public int getAge() {
+        return age;
+    }
+
+    public void setAge(int age) {
+        this.age = age;
+    }
+
+    public int getScore() {
+        return score;
+    }
+
+    public void setScore(int score) {
+        this.score = score;
+    }
+
+    @Override
+    public String toString() {
+        return "name='" + name +
+                " age=" + age +
+                " score=" + score;
+    }
+
+    //如果成绩一样,那在成绩排序的基础上按照年龄由小到大排序,
+    // 成绩和年龄都一样,则按照姓名的字典顺序排序。
+    @Override
+    public int compareTo(Student o) {
+        int num = this.score - o.score;
+        int num1 = num == 0 ? this.age - o.age : num;
+        int num2 = num1 == 0 ? this.name.compareTo(o.name) : num1;
+        return num2;
+    }
+
+
+    public static void main(String[] args) {
+        //Comparable方式接口对以上同学的成绩做降序排序
+
+        //创建TreeSet集合对象
+        Set<Student> treeSet = new TreeSet<Student>();
+
+        //创建学生对象
+        treeSet.add(new Student("Tom",20,90));
+        treeSet.add(new Student("Jerry",22,95));
+        treeSet.add(new Student("John",20,100));
+        treeSet.add(new Student("Lily",22,100));
+        treeSet.add(new Student("Lucy",22,90));
+        treeSet.add(new Student("Kevin",22,90));
+
+        //遍历数组
+        for (Student student : treeSet){
+            System.out.println(student);
+        }
+
+    }
+    }

+ 89 - 0
src/main/java/com/sf/day16/homework/Student2.java

@@ -0,0 +1,89 @@
+package com.sf.day16.homework;
+
+import java.util.Comparator;
+import java.util.Set;
+import java.util.TreeSet;
+
+public class Student2 {
+    /**
+     * 学生姓名
+     */
+    private String name;
+    /**
+     * 学生年龄
+     */
+    private int age;
+    /**
+     * 学生成绩
+     */
+    private int score;
+
+    public Student2() {
+    }
+
+    public Student2(String name, int age, int score) {
+        this.name = name;
+        this.age = age;
+        this.score = score;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public int getAge() {
+        return age;
+    }
+
+    public void setAge(int age) {
+        this.age = age;
+    }
+
+    public int getScore() {
+        return score;
+    }
+
+    public void setScore(int score) {
+        this.score = score;
+    }
+
+    @Override
+    public String toString() {
+        return "name='" + name +
+                " age=" + age +
+                " score=" + score;
+    }
+
+    public static void main(String[] args) {
+        //Comparator方式接口对以上同学的成绩做降序排序
+
+        //创建TreeSet集合对象
+        Set<Student> treeSet = new TreeSet<Student>(new Comparator<Student>() {
+            @Override
+            public int compare(Student o1, Student o2) {
+                int num = o1.getScore() - o2.getScore();
+                int num1 = num == 0 ? o1.getAge() - o2.getAge() : num;
+                int num2 = num1 == 0 ? o1.getName().compareTo(o2.getName()) : num1;
+                return num2;
+            }
+        });
+
+        //创建学生对象
+        treeSet.add(new Student("Tom",20,90));
+        treeSet.add(new Student("Jerry",22,95));
+        treeSet.add(new Student("John",20,100));
+        treeSet.add(new Student("Lily",22,100));
+        treeSet.add(new Student("Lucy",22,90));
+        treeSet.add(new Student("Kevin",22,90));
+
+        //遍历数组
+        for (Student student : treeSet){
+            System.out.println(student);
+        }
+
+    }
+}

BIN
target/classes/com/sf/day07/getset/Test01.class


BIN
target/classes/com/sf/day16/MyUserCompare.class


BIN
target/classes/com/sf/day16/Test01.class


BIN
target/classes/com/sf/day16/TestUser$1.class


BIN
target/classes/com/sf/day16/TestUser.class


BIN
target/classes/com/sf/day16/User.class


BIN
target/classes/com/sf/day16/homework/Library.class


BIN
target/classes/com/sf/day16/homework/Student.class


BIN
target/classes/com/sf/day16/homework/Student2$1.class


BIN
target/classes/com/sf/day16/homework/Student2.class