在Java中比较器的总结_9.4JAVA-复习-29(比较器2 数据分析例题 )

1.比较器

comparable(自然排序)

6de49624b0ed

概述

6de49624b0ed

概述

6de49624b0ed

举例说明

6de49624b0ed

总结

....重要要注意转型

6de49624b0ed

要点

6de49624b0ed

第二种方法

6de49624b0ed

第一个相同,如何比较第二个

comparator(定制排序)

6de49624b0ed

image.png

6de49624b0ed

注意转型,也可以是泛型

代码如下: 继承,自然排序

package Dayt9_3;

public class People implements Comparable{

String name;

long id;

String sex;

public People() {

}

public People(String name, long id, String sex) {

this.name = name;

this.id = id;

this.sex = sex;

}

@Override

public String toString() {

return "People{" +

"name='" + name + '\'' +

", id=" + id +

", sex='" + sex + '\'' +

'}';

}

@Override

public int compareTo(People people) {

if (this.name.equals(people.name)){

return (int) - (this.id-people.id);

}

else {

if (this.id == people.id){

return -(this.sex.compareTo(people.sex));

}

else return this.name.compareTo(people.name);

}

}

}

package Dayt9_3;

import java.util.ArrayList;

import java.util.Collections;

public class TestPeople {

public static void main(String[] args) {

ArrayList objects = new ArrayList<>();

People people = new People("小明", 10, "浙江");

People people1 = new People("小明", 12, "安徽");

People people2 = new People("小明", 11, "北京");

objects.add(people);

objects.add(people1);

objects.add(people2);

//比较器的两种方法

//自然比较法和定制排序法

Collections.sort(objects);

System.out.println(objects);

}

}

代码如下:自定义排序

package Dayt9_3;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

public class TestPeople {

public static void main(String[] args) {

ArrayList objects = new ArrayList<>();

People people = new People("小明", 10, "浙江");

People people1 = new People("小明", 12, "安徽");

People people2 = new People("小明", 11, "北京");

objects.add(people);

objects.add(people1);

objects.add(people2);

//比较器的两种方法

//自然比较法和定制排序法

Collections.sort(objects, new Comparator() {

@Override

public int compare(People people, People t1) {

if (t1.name.equals(people.name)){

return (int) (t1.id-people.id);

}

else {

if (t1.id == people.id){

return -(t1.sex.compareTo(people.sex));

}

else return t1.name.compareTo(people.name);

}

}

});

//怎么使用三目运算符........

}

}

数据分析例题

共同好友问题

package Dayt9_3;

import java.io.*;

import java.util.*;

public class Testfriend {

static BufferedWriter bufferedWriter;

static {

try {

bufferedWriter = new BufferedWriter(new FileWriter(new File("a.txt")));

} catch (IOException e) {

e.printStackTrace();

}

}

public static void main(String[] args) throws IOException {

//BUFFERES方法有特殊方法读取

BufferedReader bufferedReader = new BufferedReader(new FileReader(new File("C:\\Users\\hp\\IdeaProjects\\BookCollection\\src\\Dayt9_3\\新建文本文档.txt")));

//接收

String len = null;

//建立一个MAP集合来接收

Map objectfriend = new HashMap<>();

//循环获取

while ((len = bufferedReader.readLine()) != null) {

String[] people = len.split(":");

String people1 = people[0];

String[] split = people[1].split(",");

List strings = Arrays.asList(split);

//注意这个LIST是一个假集合,不能进行增添操作,采取如下的构造方法

LIst string1 =new ArrayList<>(strings);

objectfriend.put(people1, strings1);

}

//把文本的数据都放到了MAP中,寻找最大朋友数量

//返回键值对.是单列的

Set> entries = objectfriend.entrySet();

//把这个键值对放到list中,因为list有比较方法

List> entries1 = new ArrayList<>(entries);

//采用Arrays.sort() 同样也能比较

//方法一,自然比较法 Comparable

// Arrays.sort();

//方法二 LIST自带比较法

entries1.sort(new Comparator>() {

@Override

public int compare(Map.Entry stringListEntry, Map.Entry t1) {

return t1.getValue().size() - stringListEntry.getValue().size();

}

});

//方法三 ,自定义比较法

Collections.sort(entries1, new Comparator>() {

@Override

public int compare(Map.Entry stringListEntry, Map.Entry t1) {

//进行比较 比较好友数量

return t1.getValue().size() - stringListEntry.getValue().size();

}

});

//获取最大的那个,这是一个降序排列

Map.Entry stringListEntry = entries1.get(0);

System.out.println("人" + stringListEntry.getKey() + "好友数量" + stringListEntry.getValue().size());

//如果存在两个或者多个最大值,对排序后的集合进行遍历,找长度与最大值一样的

for (int i = 1; i < entries1.size(); i++) {

if (entries1.get(i).getValue().size()==entries1.get(0).getValue().size()) {

System.out.println(entries1.get(i).getKey() + entries1.get(i).getValue());

}

}

//需求二获取指定俩俩好友共同好友

//注意HashMap 和ArrayList 中get方法.......

Scanner scanner = new Scanner(System.in);

//注意把小写转变为大写

System.out.println("请输入第一个");

String next1 = scanner.next();

if (!objectfriend.containsKey(next1.toUpperCase())) {

System.out.println("再见,没有该用户");

System.exit(0);

}

List list = objectfriend.get(next1.toUpperCase());

System.out.println("请输入第二个");

String next2 = scanner.next();

List list1 = objectfriend.get(next2.toUpperCase());

//把被比较的放到一个集合中,这里是第一个

List strings = new ArrayList<>(list);

//使用方法retainAl 比较集合共有的元素 把共有的元素放到了新建的集合中

strings.retainAll(list1);

if (strings.size() != 0) {

System.out.println(next1 + "和" + next2 + "共同好友为" + strings);

}

//三获取所有人的共同好友,

//这里问题list中放string

Set> entries2 = objectfriend.entrySet();

//把这个set集合放在ARRAYLIST集合中

List> entries3 = new ArrayList<>(entries2);

//获取的所有键值对都放在了list集合中,遍历一样比较

for (int i = 0; i < entries3.size(); i++) {

//获取外循环的数值

Map.Entry stringListEntry1 = entries3.get(i);

String key = stringListEntry1.getKey();

List value = stringListEntry1.getValue();

for (int j = 1 + i; j < entries3.size(); j++) {

Map.Entry stringListEntry2 = entries3.get(j);

String key1 = stringListEntry2.getKey();

List value1 = stringListEntry2.getValue();

List arrayList = new ArrayList(value);

arrayList.retainAll(value1);

if (arrayList.size()!=0) {

System.out.println(key + "和" + key1 + "共同好友" + arrayList.size() + " "+ arrayList );

}

else { System.out.println(key + "和" + key1 + "无共同好友" );}

}

}

}

//写入文本,可以吧=把上面的方法给封装一下

public void addtext(String ADD) throws IOException {

bufferedWriter.write(ADD);//把打印的内容写入进去

bufferedWriter.newLine();

bufferedWriter.flush();

}

public void closeio () throws IOException {

bufferedWriter.close();

}

}

A:B,C,D,F,E,O

B:A,C,E,K

C:F,A,D,I

D:A,E,F,L

E:B,C,D,M,L

F:A,B,C,D,E,O,M

G:A,C,D,E,F

H:A,C,D,E,O

I:A,O

J:B,O

K:A,C,D

L:D,E,F

M:E,F,G

O:A,H,I,J

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值