(Item 11) Comparable

(Item 11) Comparable

Why: by implementing comparable, your class can interoperate with many generic algorithms.

Contract: compare the object with another, return a negative integer, zero, or positive integer

 

Classes that depends on comparison

Sorted collection

TreeSet

TreeMap

Utility classes: Collections and Arrays

 

//Sample Code, for PhoneNumber class above

public int compareTo(Object o) {

    PhoneNumber pn = (PhoneNumber)o;

 

    // Compare area codes

    if (areaCode < pn.areaCode)

        return -1;

    if (areaCode > pn.areaCode)

        return  1;

        // Area codes are equal, compare exchanges

    if (exchange < pn.exchange)

        return -1;

    if (exchange > pn.exchange)

        return  1;

 

    // Area codes and exchanges are equal, compare extensions

    if (extension < pn.extension)

        return -1;

    if (extension > pn.extension)

        return  1;

 

    return 0;  // All fields are equal

}

 
Traceback (most recent call last): File "D:\Python\lib\multiprocessing\pool.py", line 125, in worker result = (True, func(*args, **kwds)) File "D:\Python\lib\multiprocessing\pool.py", line 48, in mapstar return list(map(*args)) File "E:\comparableDjango\comparable\comparable\comparable.py", line 59, in parallel_compute similarity = [compute_xsd(ss1_item, ss2_item) for ss1_item in ss1] File "E:\comparableDjango\comparable\comparable\comparable.py", line 59, in <listcomp> similarity = [compute_xsd(ss1_item, ss2_item) for ss1_item in ss1] File "E:\comparableDjango\comparable\comparable\comparable.py", line 21, in compute_xsd s1_cut = cut_words(ss1) File "E:\comparableDjango\comparable\comparable\comparable.py", line 17, in cut_words return [word for word, flag in words if (word not in stopwords) and word.strip() != '' and word.isalnum()] File "E:\comparableDjango\comparable\comparable\comparable.py", line 17, in <listcomp> return [word for word, flag in words if (word not in stopwords) and word.strip() != '' and word.isalnum()] File "E:\comparableDjango\comparable\venv\lib\site-packages\jieba\posseg\__init__.py", line 294, in cut for w in dt.cut(sentence, HMM=HMM): File "E:\comparableDjango\comparable\venv\lib\site-packages\jieba\posseg\__init__.py", line 249, in cut for w in self.__cut_internal(sentence, HMM=HMM): File "E:\comparableDjango\comparable\venv\lib\site-packages\jieba\posseg\__init__.py", line 217, in __cut_internal sentence = strdecode(sentence) File "E:\comparableDjango\comparable\venv\lib\site-packages\jieba\_compat.py", line 79, in strdecode sentence = sentence.decode('utf-8') AttributeError: 'float' object has no attribute 'decode' """ The above exception was the direct cause of the following exception: Traceback (most recent call last): File "E:\comparableDjango\comparable\comparable\comparable.py", line 65, in <module> results = pool.map(parallel_compute, ss2) File "D:\Python\lib\multiprocessing\pool.py", line 364, in map return self._map_async(func, iterable, mapstar, chunksize).get() File "D:\Python\lib\multiprocessing\pool.py", line 771, in get raise self._value AttributeError: 'float' object has no attribute 'decode'这段报错怎么解决
最新发布
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值