关于TreeMap中如何输出排序好第一个key及value

在做实验的时候,遇到了需要从treemap中得到第一个 key和value。

我们都知道,treemap会根据键值进行排序(字典序),那怎么得到第一个value呢?通过查看treemap的方法文件介绍,我写了以下测试程序

package test;


import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeMap;


public class test {


    public static void main(String[] args) {
        TreeMap< Integer,String> map=new TreeMap<Integer,String>();
        map.put(2,"abcd");
        map.put(1, "Abc");
        map.put(3,"bbb");
        map.put(4, "BBBB");
        System.out.println(map);
        System.out.println(map.get(map.firstKey()));
        System.out.println(map.get(map.lastKey()));
    }

}

输出结果如下:


可以看到treemap按键值排序输出

同时map.firstKey(),和map.lastKey()方法分别输出了排好序之后第一个键值,和最后一个键值,同时可以由此得到相对应的value

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Java程序,可以读一个txt文件,提第一列和第四列,然后按照第一列进行排序。该程序需要在Hadoop环境下运行。 ```java import java.io.IOException; import java.util.StringTokenizer; import java.util.TreeMap; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class SortColumns { public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{ private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(Object key, Text value, Context context) throws IOException, InterruptedException { StringTokenizer itr = new StringTokenizer(value.toString()); while (itr.hasMoreTokens()) { String[] columns = itr.nextToken().split("\t"); if (columns.length >= 4) { word.set(columns[0]); context.write(word, new IntWritable(Integer.parseInt(columns[3]))); } } } } public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> { private TreeMap<String, Integer> sortedMap = new TreeMap<String, Integer>(); public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } sortedMap.put(key.toString(), sum); } protected void cleanup(Context context) throws IOException, InterruptedException { for (String key : sortedMap.keySet()) { context.write(new Text(key), new IntWritable(sortedMap.get(key))); } } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "sort columns"); job.setJarByClass(SortColumns.class); job.setMapperClass(TokenizerMapper.class); job.setCombinerClass(IntSumReducer.class); job.setReducerClass(IntSumReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } } ``` 在该程序,首先定义了一个Mapper类来提第一列和第四列,并将第一列作为键,第四列作为值输出。接着定义了一个Reducer类来对相同键的值进行求和,并使用TreeMap对键进行排序最后在main方法配置作业,包括输入和输出路径以及Mapper和Reducer类,然后提交作业并等待完成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值