学习java第27天,str.equals();

本文详细解释了Java中equals方法的基本概念及其实现原理。equals方法用于比较两个对象是否相等,可以通过重写该方法来定义对象之间的相等条件。默认情况下,equals使用==操作符判断两个对象引用是否指向同一个对象。
1,java中的原有的equals();里边比较的是使用 == 来比较, == 是比较是不是同一个东西 ,equals比较的是两个东西的特征是否一致,不用比较所有的特征,可以对原生的equals重写来实现具体的类的equals特征的比较;
1.mapper package myPageRank; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; import java.io.IOException; import java.util.StringTokenizer; /** * @author tulie * @ceerte $(YEAR)-$(MONTH)-$(DAY)-$(TIME) */ public class PRMapper extends Mapper<Object, Text, IntWritable, Text> { private IntWritable id; private String pr; private int count; private float average_pr; //重写map方法 public void map(Object key, Text value, Context context) { //构造一个用来解析 strStringTokenizer 对象。java 默认的分隔符是空格("")、制表符(\t)、换行符(\n)、回车符(\r)。 StringTokenizer str = new StringTokenizer(value.toString()); //|1 6 2 4 5 if(str.hasMoreTokens())//返回是否还有分隔符 { id = new IntWritable(Integer.parseInt(str.nextToken()));//1 |6 2 4 5 }else{ return; } pr = str.nextToken();//返回从当前位置到下一个分隔符的字符串 1 6 |2 4 5 count = str.countTokens();//3 average_pr = Float.parseFloat(pr)/count; while(str.hasMoreTokens()) { try{ String nextId = str.nextToken();//1 6 2| 4 5 IntWritable linid = new IntWritable(Integer.parseInt(nextId)); //将网页向外链接的ID以“pr+得到贡献值”格式输出 Text avpr = new Text("pr" + average_pr); context.write(linid, avpr); // 将网页ID和PR值输出 Text ids = new Text("id" + nextId); context.write(id, ids); }catch(IOException e) { e.printStackTrace(); }catch (InterruptedException e) { e.printStackTrace(); } } } } AI写代码 java 运行 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 收起代码块 2.reducer import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; import java.io.IOException; import java.util.ArrayList; /** * @author tulie * @ceerte $(YEAR)-$(MONTH)-$(DAY)-$(TIME) */ public class PRReducer extends Reducer<IntWritable, Text, IntWritable, Text> { //重写reduce方法 public void reduce(IntWritable key, Iterable<Text> values, Context context) { // 定义一个存储网页链接ID的队列 ArrayList<String> ids = new ArrayList<String>(); // 将所有的链接ID以String格式保存 String strid = " "; // 定义一个保存网页PR值的变量 float pr = 0; //遍历 System.out.println(key.get()); for(Text txt : values) { String str = txt.toString(); //判断value是贡献值还是向外部的链接 if (str.startsWith("pr")) { // 贡献值 pr += Float.parseFloat(str.substring(2)); System.out.println(pr); } else if (str.startsWith("id")) { // 链接id String id = str.substring(2); ids.add(id); } } pr = 0.85f*pr + 0.15f; // 得到所有链接ID的String形式 for (int i = 0; i < ids.size(); i++) { strid += ids.get(i) + " "; } // 组合pr+lianjie成原文件的格式类型 String strpr = String.format("%.5f", pr); String result = strpr + strid; try { context.write(key, new Text(result)); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } } AI写代码 java 运行 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 3.Driver 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.mapred.lib.ChainMapper; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; /** * @author tulie * @ceerte $(YEAR)-$(MONTH)-$(DAY)-$(TIME) */ AI写代码 java 运行 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class PRDriver { public static void main(String[] args) throws Exception{ Configuration conf = new Configuration(); String pathIn1 = “D:/PageRank/input/input.txt”; String pathOut=“D:/PageRank/output1/PageRankOutput0”; for(int i=1;i<41;i++){ //加入for循环 Job job = Job.getInstance(conf, “MapReduce pagerank”); job.setJarByClass(PRDriver.class); job.setMapperClass(PRMapper.class); job.setReducerClass(PRReducer.class); job.setOutputKeyClass(IntWritable.class); job.setOutputValueClass(Text.class); FileInputFormat.addInputPath(job, new Path(pathIn1)); FileOutputFormat.setOutputPath(job, new Path(pathOut)); pathIn1 = pathOut;//把输出的地址改成下一次迭代的输入地址 pathOut = pathOut+i;//把下一次的输出设置成一个新地址。 job.waitForCompletion(true);//把System.exit()去掉 } } ———————————————— 版权声明:本文为优快云博主「图列」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/TulieCS/article/details/108483363(生成一个完整的)
最新发布
10-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值