TreeMap学习笔记(一)

package com.qunar.yuliang.test13;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;

/**
 * Created by yuliang.jin on 14-7-9.
 */
public class HotelInfoSorter {

    private static Logger logger = LoggerFactory.getLogger(HotelInfoSorter.class);

    public static File getFileSortedByHotelCode(File hotelInfoFile) throws IOException {
        TreeMap<String, String> hotelInfo = new TreeMap<String, String>();
        List<String> lines = Files.readLines(hotelInfoFile, Charsets.UTF_8);
        int size = lines.size();
        String[][] hotelInfoParts = new String[size][];
        /**
         * split each item of hotel info into hotel code and hotel name, and put them into a tree map.
         */
        for (int i = 0; i < size; i++) {
            hotelInfoParts[i] = lines.get(i).split("\\s+", 2);
            hotelInfo.put(hotelInfoParts[i][0], hotelInfoParts[i][1]);
            logger.info("酒店代号:" + hotelInfoParts[i][0]);
            logger.info("酒店名称:" + hotelInfoParts[i][1]);
        }

        /**
         * save the sorted info in the tree map to a file
         */
        File fileSortedByHotelCode = new File("src/main/resources/test13/fileSortedByHotelCode");

        Iterator iterator = hotelInfo.keySet().iterator();
        while (iterator.hasNext()) {
            String hotelCode = (String) iterator.next();
            String hotelName = hotelInfo.get(hotelCode);
            Files.append(hotelCode + "\t" + hotelName + "\n", fileSortedByHotelCode, Charsets.UTF_8);
        }
        return fileSortedByHotelCode;
    }
}



遍历TreeMap:

Iterator iterator = hotelInfo.keySet().iterator();
        while (iterator.hasNext()) {
            String hotelCode = (String) iterator.next();
            String hotelName = hotelInfo.get(hotelCode);
            Files.append(hotelCode + "\t" + hotelName + "\n", fileSortedByHotelCode, Charsets.UTF_8);
        }

按key逆序输出TreeMap的值:仅对keySet进行逆序。

        Iterator iterator = hotelInfo.keySet().iterator();
        List<String> keyList = new LinkedList<String>();
        while (iterator.hasNext()) {
            String hotelCode = (String) iterator.next();
            keyList.add(hotelCode);
        }

        Collections.reverse(keyList);

        for(String hotelCode : keyList) {
           Files.append(hotelCode + "\t" + hotelInfo.get(hotelCode) + "\n", fileSortedByHotelCode, Charsets.UTF_8);
        }
总结:

1)遍历TreeMap:Iterator iterator = hotelInfo.keySet().iterator();

2)LinkedList逆序:Collections.reverse(keyList);

3)遍历LinkedList:for(String hotelCode : keyList) {}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值