zoukankan      html  css  js  c++  java
  • TreeSetTest

    简介

    java 核心编程
    关于TreeSetTest的知识

    输出

    [[descrition=Toaster,partNumber=1234], [descrition=Widget,partNumber=4562], [descrition=Modem,partNumber=9912]]
    [[descrition=Modem,partNumber=9912], [descrition=Toaster,partNumber=1234], [descrition=Widget,partNumber=4562]]
    

    有两个输出,第一个是默认自带的比较函数结构

        public int compareTo(Item other){
            int diff = Integer.compare(partNumber, other.partNumber); // 先比较编号再比较字符串
            return diff != 0? diff : description.compareTo(other.description);
        }
    

    表示 先比较编号再比较字符串
    第二个输出,设定的比较方式采用字符串比较的方式进行排序。
    NavigableSet sortByDescription = new TreeSet<>(Comparator.comparing(Item::getDescription));

    code

    import java.util.*;
    public class TreeSetTest {
        public static void main(String[]args){
            SortedSet<Item> parts = new TreeSet<>();
            parts.add(new Item("Toaster", 1234));
            parts.add(new Item("Widget", 4562));
            parts.add(new Item("Modem", 9912));
            System.out.println(parts);
    
            NavigableSet<Item> sortByDescription = new TreeSet<>(Comparator.comparing(Item::getDescription)); // 设定的比较方式,直接通过字符串进行排序
    
            sortByDescription.addAll(parts);
            System.out.println(sortByDescription);
        }
    }
    
    import java.util.*;
    public class Item implements Comparable<Item>{
        private String description;
        private int partNumber;
        /**
         * Constructs an item.
         * 
         * @param aDescription
         *          the item's description
         * @param aPartNumber
         *          the item's part number
         */
        public Item(String aDescription, int aPartNumber){
            description = aDescription;
            partNumber = aPartNumber;
        }
    
        public String getDescription(){
            return description;
        }
    
        public String toString(){
            return "[descrition=" + description + ",partNumber=" + partNumber + "]";
        }
    
        public boolean equals(Object otherObject){
            if(this == otherObject) return true;
            if (otherObject == null) return false;
            if(getClass() != otherObject.getClass()) return false;
            Item other = (Item) otherObject;
            return Objects.equals(description,other.description) && partNumber == other.partNumber;
        }
    
        public int hashCode(){
            return Objects.hash(description, partNumber);
        }
    
        public int compareTo(Item other){
            int diff = Integer.compare(partNumber, other.partNumber); // 先比较编号再比较字符串
            return diff != 0? diff : description.compareTo(other.description);
        }
    }
    
    
    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    Lua调用C++时打印堆栈信息
    Node.js批量去除BOM文件
    cocos2d-x中CCLabelAtlas的小图片拼接
    node.js使用mysql模块的坑
    关于chrome插件编写的小结
    【吐槽】如风达快递
    bat调用TexturePacker更新SpriteSheet
    使用node-webkit实现打包工具的小结
    使用devenv.exe自动编译项目
    svn导出文件进行比较
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/13887460.html
Copyright © 2011-2022 走看看