java 集合 之 equals contain

id 是string类型 contains方法使用equals方法 来比较,所以值相等就返回ture

Value 是Student类型,自己定义的,默认的contains方法中的equals方法,比较的是两个引用是否一样,所以要重写equals方法

由于是比较两student类型是否相同,所以要做Student类中重写equals方法

怎么生成随机字符

public void testSort2() {

List strings = new ArrayList();

String str = “0123456789abcdefghijklmnobqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”;

Random random = new Random();

for (int a = 0; a < 10; a++) {

StringBuilder builder = new StringBuilder();

do {

int j = random.nextInt(10) + 1;

for (int i = 0; i < j; i++) {

builder.append(str.charAt(random.nextInt(str.length())));

}

} while (strings.contains(builder));

System.out.println(“将要添加字符串”+builder);

String s=builder.toString();

strings.add(s);

}

System.out.println(“-----------排序前------------”);

for (String string : strings) {

System.out.println(“元素”+string);

}

Collections.sort(strings);

System.out.println(“-----------排序后-----------”);

for (String string : strings) {

System.out.println(“元素”+string);

}

}

public static void main(String[] args) {

CollectionTest collectionTest = new CollectionTest();

// collectionTest.testSort1();

collectionTest.testSort2();

}

对Integer泛型的list进行排序–>

List integerList=new ArrayList();

Random random = new Random(); // 创建一个新的随机生成器

Integer k;

for(int i=0;i<10;i++){

do{

k=random.nextInt(100);// 随机生成100以内的整数

}while(integerList.contains(k)); //若已存在,循环运行,生成其他值

integerList.add(k);

}

思路:

给泛型为Integer的list里插入十个100以内的不重复的随机数。

Random类的nextInt(100)方法可以返回100以内的随机数。

Collections.sort()方法给Integer list按数字升序排列顺序。

Comparable接口-----可比较的

1.实现该接口表示:这个类的实例可以比较大小,可以进行自然排序

2.定义了默认的比较规则

3.其实现类需实现compareTo()方法

compareTo()方法返回正数表示打,负数表示小,0表示相等

Comparator接口-----比较工具接口

1.用于定义临时比较规则,而不是默认比较规则

2.其实现类需要实现compare()方法

3.Comparator和Comparable都是Java集合框架的成员

Java集合框架:Collection接口、Collections工具类、Map接口、Comparator接口、Comparable接口

1. 创建一副扑克牌,不考虑大小王

  1. 创建两名玩家,玩家至少要有ID、姓名、手牌等属性,手牌为扑克牌的集合

  2. 洗牌,将之前创建的扑克牌顺序打乱(说明是有序的)

  3. 发牌,将洗牌之后的扑克牌集合,从第一张开始,发给两名玩家,按照一人一张的方式,每人发两张

  4. 开始游戏,比大小,取两人各自的点数最大的牌进行比较,点数大的赢,若大小相同比花色(黑红梅方)

package com.xk;

public class Poker

{

public String color;

public String points;

public Poker(String color,String points){ this.color=color; this.points=points; }

@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((points == null) ? 0 : points.hashCode()); result = prime * result + ((color == null) ? 0 : color.hashCode()); return result; }

@Override public boolean equals(Object obj) {

if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false;

Poker other = (Poker) obj; if (points == null) {

if (other.points != null) return false; } else if (!points.equals(other.points)) return false;

if (color == null)

{ if (other.color != null) return false; }

else if (!color.equals(other.color))

return false; return true; } }

package com.xk; import java.util.ArrayList; import java.util.List;

public class Player {

public int id; public String name;

public List<Poker> handCards=new ArrayList<>();

public Player(int id,String name){

this.id=id; this.name=name;

}

}

package com.xk; import java.util.Comparator;

public class CompareToPoker implements Comparator<Poker>

{ @Override public int compare(Poker o1, Poker o2) {

// TODO Auto-generated method stub String[] color = {“方片”, “梅花”, “红桃”, “黑桃”};

String[] point = {“2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”, “J”,“Q”,“K”, “A”};

int temp1 = 0; int temp2 = 0; for(int i=0;i<point.length;i++){ if(o1.points.equals(point[i])) temp1 += i*10; if(o2.points.equals(point[i])) temp2 += i*10; }

for(int i=0;i<color.length;i++){ if(o1.color.equals(color[i])) temp1 += i;

if(o2.color.equals(color[i])) temp2 += i; }

if( temp1 > temp2) return -1;

if( temp1 < temp2 )

return 1; return 0; } }

public class GameStart{

String[] point=new String[]{“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”,“10”,“J”,“Q”,“K”,“A”};

String[] color=new String[]{“黑桃”,“红桃”,“梅花”,“方块”};

Scanner input = new Scanner(System.in);

List<Poker> pokerList=new ArrayList<>();

List<Player> playerList=new ArrayList<>();

public void initialize(){ for(int i=0;i<4;i++) { for(int j=0;j<13;j++) {

pokerList.add(new Poker(color[i],point[j])); } }

System.out.println(“------------创建扑克牌-------------”);

int i=0; for(Poker poker : pokerList){ System.out.print(poker.color+poker.points+" ");

i++; if(i%13==0){ System.out.println(“”); } } } public void shuffle(){ System.out.println(“-------------开始洗牌-------------”);

Collections.shuffle(pokerList);//shuffle List随机排序 System.out.println(“-------------洗牌完成-------------”); }

public void sort(){ Collections.sort(playerList.get(0).handCards,new CompareToPoker());

Collections.sort(playerList.get(1).handCards,new CompareToPoker());

System.out.println(playerList.get(0).name+“最大手牌:”+playerList.get(0).handCards.get(0).color+playerList.get(0).handCards.get(0).points); System.out.println(playerList.get(1).name+“最大手牌:”+playerList.get(1).handCards.get(0).color+playerList.get(1).handCards.get(0).points); }

public void print(){ System.out.println(“----------玩家各自手牌-------------”);

for(Player player : playerList){ System.out.print(player.name+“:”);

for(Poker poker : player.handCards){ System.out.print(“[”+poker.color+poker.points+“]”); } System.out.println(“”); } }

public void compareToPonint(){ System.out.println(“--------------获胜方--------------”);

List<Poker> maxPoker = new ArrayList<Poker>();

List<Poker> minPoker = new ArrayList<Poker>();

maxPoker.add(playerList.get(0).handCards.get(0));

maxPoker.add(playerList.get(1).handCards.get(0));

minPoker.add(playerList.get(0).handCards.get(1));

minPoker.add(playerList.get(1).handCards.get(1));

Collections.sort(maxPoker,new CompareToPoker());

Collections.sort(minPoker,new CompareToPoker());

/* * 判断最大牌点数是否相等 * 判断最小牌点数是否相等 * 判断最大牌花色 * 判断最小牌花色 * */ if(maxPoker.get(0).points.equals(maxPoker.get(1).points)){

if(minPoker.get(0).points.equals(minPoker.get(1).points)){

if(maxPoker.get(0).equals(playerList.get(0).handCards.get(0))){

System.out.println(“红方获胜\tWinner:”+playerList.get(0).name);

}else{

最后

文章中涉及到的知识点我都已经整理成了资料,录制了视频供大家下载学习,诚意满满,希望可以帮助在这个行业发展的朋友,在论坛博客等地方少花些时间找资料,把有限的时间,真正花在学习上,所以我把这些资料,分享出来。相信对于已经工作和遇到技术瓶颈的朋友们,在这份资料中一定都有你需要的内容。

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值