JAVA笔记13 TreeSet集合与MAP集合

1.TreeSet 集合的介绍和排序
public class TreeSetDemo{
    public static void main(String[] args){
        //TreeSet元素唯一,且可以对元素进行排序
        //排序,自然排序,比较器排序
        TreeSet<Integer> treeSet = new TreeSet<>();
        treeSet.add(20);
        treeSet.add(13);
        treeSet.add(11);
        treeSet.add(28);
        treeSet.add(34);
        System.out.println(treeSet);
        //TreeSet 底层数据结构是二叉树
    }
}
//假设已经自定类Student已经存在
public class TreeSetDemo{
    public static void main(String[] args){
        TreeSet<Student> stu = new TreeSet<>();
        
        treeSet.add(new Student("张"21));
        treeSet.add(new Student("张"25));
        treeSet.add(new Student("张"32));
        treeSet.add(new Student("张"54));
        treeSet.add(new Student("张"43));
        treeSet.add(new Student("张"22))//排序:自然排序和比较器排序
            //自然排序:采用的是空参构造,那就是自然排序
            //如果是自然排序,那么对元素有要求,要求元素必须实现一个Compartor接口,重写这个接口中的一个compareTo这个比较的方法
            //根据此方法的返回值的正负 0 来决定排序的顺序
            for(Student student :treeSet){
              System.out.println(student);    
        }      
   /* public int  compare To(Student student){
      int num = this.age-student.age;
      int num2 = num == 0 ? this.name.compareTo(student.name);num
              retur num2;
            */ 
            }
            
//假设已经自定类Student已经存在
public class TreeSetDemo{
    public static void main(String[] args){
        TreeSet<Student> stu = new TreeSet<>();
        TreeSet<Seudent>treeSet = new TreeSet<>(new Comparator<Student>){
            public int compare(Student s1,Studeng s2){
                int num = s1.getName().length()-s2.getName().length();
                int num2 = num ==0?s1.getName().compareTo(s2.getName):num
                int num3= num2==0?s1.getAge()-s2.getAge():num2
                return num3;
                
            }
        }
        
        treeSet.add(new Student("张4一"12));
        treeSet.add(new Student("李撒三"13));
        treeSet.add(new Student("刘"18));
        treeSet.add(new Student("林茜茜"56));
        treeSet.add(new Student("张五六七"19));
        treeSet.add(new Student("张"77))for(Student student :treeSet){
        System.out.println(student); 
        }
    }
}
public class Demo10 { //列表排序
    public static void main(String[] args) {
        ArrayList<Integer> integers = new ArrayList<>();
        integers.add(100);
        integers.add(10);
        integers.add(198);
        integers.add(200);
        integers.sort(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1-o2;
            }
        });
        System.out.println(integers);

    }
 }


public class Demo10 {  //方式一 数组排序
    public static void main(String[] args) {
        Integer[] a ={1,4,2,9,6,5};
        Arrays.sort(a);
        System.out.println(Arrays.sort(a))
            
 public class Demo10 { //方式二 数组排序
    public static void main(String[] args) {
        Integer[] a = {1,1,3,2,4,6,4,8};
        Arrays.sort(a, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1-o2;
            }
        });
        System.out.println( Arrays.toString(a));
    }
 }     
//编写一个程序,获取10个1至20的随机数,要求随机数不能重复
TreeSet<Integer>treeSet = new TreeSet<>();
Random random = new Random();
for(int i = 0;i<10;i++){
    int num = random.nextInt(20)+1;
    treeSet.add(num);
}
System.out.println(treeSet.add(num);)


2.HashMap的学习与介绍
public class Demo {  
    public static void main(String[] args) {
//引入
//假如有这种数据
 //s001---------张三
 //s002---------李四
 //s003---------王五
 ArrayList<String> list = new ArrayList<>()
 list.add("s001---------张三");
 list.add("s002---------李四");
 list.add("s003---------王五");
 String s = list.get(0);
 String[] strings = s.split("[-]");
 System.out.println(strings[0]);
 System.out.println(strings[1]);
        
 //java针对键值映射关系的数据,给我们提供了另外一种容器Map,用来存储这种键值映射关系的数据
 //一个键,只能对应一个值
 //Map集合称作为双列集合       
     
public class Demo {  
   public static void main(String[] args) { 
       //所有map集合的数据结构,只跟键有关,一个键对应一个值,键相同,值覆盖
       HashMap<String,String> map = new HashMap<>();
       //当我们第一次存储键值对时,返回的是null
       //当我们再次存储一个键值相同的数据时,会覆盖掉旧值,返回值,上次这个键所对应的旧值
       String str = map.put("q","11");
       String str1 = map.put("q","12");
       map.put("q","11");
       //键相同,值覆盖
       map.put("q","12");
       map.put("e","13");
       map.put("t","14");
       map.put("p","22");
       System.out.println(map); //{p=22, q=12, t=14, e=13}
       System.out.println(str); //null
       System.out.println(str1);// 11
       
       /*
       map的方法
           map.clear()
           map.remove(key)
           map.isEmpty()
           map.containKey(key)
           map.get(key)
           */
     //遍历 键找值
     
     //获取所有键的集合
     Set<String> KeySet = map.keySet(); //遍历方式1
     for(String key : keySet){
     System.out.println(key+"==="+map.get(key));
     } 
     
       
     Set<Map.Entry<String, String>> entries = map.entrySet(); //遍历方式二
         for (Map.Entry<String, String> entry : entries) {
             String key = entry.getKey();
             String value = entry.getValue();
             System.out.println(key+"="+value);
     
     
           
           
3.HashMap与HashTable的区别
//HashMap 键的数据结构是哈希表,键唯一(键唯一,靠键重写equals方法,来保证,合理的重写hashcode方法,是想让元素减少碰撞
//HashMap 允许nul值null键,线程不安全效率高
HashMap<String,Integer>hashMap = new HashMap<>();
hashmap.put(null,"1");
hashmap.put("2",null);
hashmap.put("7","4");
hashmap.put("null",null);
System.out.println(hasMap);//正常输出结果
 
//HashTable 不允许null值null键,线程安全效率低
HashTable<String,Integer>HashTable = new HashTable<>();
HashTable.put(null,"1");
HashTable.put("2",null);
HashTable.put("7","4");
System.out.println(hasMap);//提示Null值异常

4.LinkedHashMap集合的学习
LinkedHashMap<Integer, String> map = new LinkedHashMap<>();
//键唯一,且有序,链表保证了键有序,哈希表保证了键唯一
map.put(1,"qq");
map.put(2,"ww");
map.put(3,"ee");
map.put(4,"tt");
map.put(5,"uu");
System.out.println(map);
5.TreeMap集合的学习
public class Demo10 {
    public static void main(String[] args) { 
        @Override  
        TreeMap<Student,Integer > treeMap = new TreeMap<>(new Comparator<Object>() {
            public int compare( Student a1, Student a2) {
             int i = a1.getAge()-a2.getAge();
             int j =i==0?a1.getName().compareTo(s2.getName()):i
              return j;
        //键的数据结构是二叉树,键唯一,且可以对键进行排序
        //空参构造 自然排序
        //有参构造 比较器排序
                
       treeMap.put(new Student(name:"zhang",age:1)1) ;        
       treeMap.put(new Student(name:"li",age:12)3) ; 
       treeMap.put(new Student(name:"wang",age:15)4);  
       treeMap.put(new Student(name:"lin",age:16)5);
       Set<Map.Entry<Object, Object>> entries = treeMap.entrySet();
        for (Map.Entry<Object, Object> entry : entries) {
            Object key = entry.getKey();
            
            Object value = entry.getValue();
            System.out.println(key+"=="+value);          
6.集合的练习
//输出一串字母要求输出格式如a(5)b(4)c(3)d(2)
public class Demo10 {
    public static void main(String[] args) {
        HashMap<Character, Integer> hashMap = new HashMap<>();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入字符串");
        String s = scanner.nextLine();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if(!hashMap.containsKey(c)){
                hashMap.put(c,1);
            }else{
                Integer integer = hashMap.get(c);
                integer++;
                Integer put = hashMap.put(c, integer);
            }
        }
        Set<Map.Entry<Character, Integer>> entries = hashMap.entrySet();
        for (Map.Entry<Character, Integer> entry : entries) {
            Character key = entry.getKey();
            Integer value = entry.getValue();
            System.out.print(key+"("+value+")");
        }
    }
}
public class Demo10 {
    public static void main(String[] args) {
      
        //基础班
             //张三 20
             //李四 22
         //就业班
            //王五 21
            //赵六 23
     
 public class Demo10 {
    public static void main(String[] args) {
        HashMap<String, Integer> hashMap = new HashMap<>();
        hashMap.put("张三", 20);
        hashMap.put("李四", 21);
        HashMap<String, Integer> hashMap1 = new HashMap<>();
        hashMap1.put("王五", 23);
        hashMap1.put("赵六", 25);
        HashMap<String, HashMap<String, Integer>> hashMap2 = new HashMap<>();
        Set<Map.Entry<String, HashMap<String, Integer>>> entries = hashMap2.entrySet();
        for (Map.Entry<String, HashMap<String, Integer>> entry : entries) {
            String key = entry.getKey();
            System.out.println(key);
            HashMap<String, Integer> value = entry.getValue();
            Set<Map.Entry<String, Integer>> entries1 = value.entrySet();
            for (Map.Entry<String, Integer> stringIntegerEntry : entries1) {
                Integer value1 = stringIntegerEntry.getValue();
                String key1 = stringIntegerEntry.getKey();
                System.out.println(key1+value1);
            }
            System.out.println("");
        }
    }
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值