# Map

Map接口的实现类主要有：HashMap、TreeMap、Hashtable、ConcurrentHashMap以及Properties等

## HashSet

```java
import java.util.HashSet;
import java.util.Random;

public class HashSetDemo {
    public static void main(String[] args) {
        Random r=new Random();

        HashSet <Integer> hs=new HashSet<Integer>();

        while(hs.size()<10) {
            hs.add((r.nextInt(20)+1));
        }
        for(Integer i:hs) {
            System.out.println(i);
        }
    }

}
```

## TreeSet

```java
//此处实现的为自然排序接口，如果仅仅使用比较器排序此接口可以不实现
public class Student implements Comparable<Student>{
    private int age;

    private String name;

    public Student() {
        super();
    }

    public Student(String name,int age) {
        super();
        this.age = age;
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public int compareTo(Student s) {
        //此处的this和s前后位置改变会影响排序方式
        int num1=this.age-s.age;
        // age相同时，比较字符串大小
        int num2=num1==0?this.name.compareTo(s.name):num1;

        return num2;
    }


}



//--------------------

import java.util.TreeSet;

//TreeSet类存储对象，自然排序
//规定:按照年龄进行排序
public class TreeSetDemo1 {
    public static void main(String[] args) {

        TreeSet<Student> ts=new TreeSet<Student>();

        Student s1=new Student("zfliu",18);
        Student s2=new Student("zfliu",20);
        Student s3=new Student("zfliu",18);
        Student s4=new Student("ZFLIU",18);
        Student s5=new Student("Java",18);

        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        ts.add(s4);
        ts.add(s5);


        for (Student s:ts) {
            System.out.println(s.getName()+s.getAge());
        }

    }
}


//-------------------
package test11_Treeset;

import java.util.Comparator;
import java.util.TreeSet;

//TreeSet类存储对象，比较器排序
//规定:按照年龄进行排序
public class TreeSetDemo2 {
    public static void main(String[] args) {
        TreeSet<Student> ts=new TreeSet<Student>(new Comparator<Student>() {

            //匿名内部类实现比较器排序接口
            public int compare(Student s1, Student s2) {
                int num1=s1.getAge()-s2.getAge();
                int num2=num1==0? s1.getName().compareTo(s2.getName()):num1;
                return num2;
            }

        });

        Student s1=new Student("zfliu",18);
        Student s2=new Student("zfliu",20);
        Student s3=new Student("zfliu",18);
        Student s4=new Student("ZFLIU",18);
        Student s5=new Student("Java",18);

        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        ts.add(s4);
        ts.add(s5);


        for (Student s:ts) {
            System.out.println(s.getName()+s.getAge());
        }
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/java/ji-he-lei/map.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
