Java常用的基本工具_Java基础:常用工具_集合

泛型

即泛指任意类型,又叫参数化类型(ParameterizedType),对具体类型的使用起到辅助作用,类似于方法的参数。

表示该集合中存放指定类型的元素

//类型安全 避免了类型转换

List list = new ArrayList<>();

List接口的常用子类有:

1、ArrayList集合

ArrayList集合数据存储的结构是数组结构。元素增删慢,查找快,由于日常开发中使用最多的功能为查询数据、遍历数据,所以ArrayList是最常用的集合。

2、LinkedList集合

LinkedList集合数据存储的结构是链表结构。方便元素添加、删除的集合。实际开发中对一个集合元素的添加与删除经常涉及到首尾操作,而LinkedList提供了大量首尾操作的方法。

List接口中常用的方法

增加元素方法

add(Object e):向集合末尾处,添加指定的元素

add(int index, Object e):向集合指定索引处,添加指定的元素,原有元素依次后移

删除元素删除

remove(Object e):将指定元素对象,从集合中删除,返回值为被删除的元素

remove(int index):将指定索引处的元素,从集合中删除,返回值为被删除的元素

替换元素方法

set(int index, Object e):将指定索引处的元素,替换成指定的元素,返回值为替换前的元素

查询元素方法

get(int index):获取指定索引处的元素,并返回该元素

public static void main(String[] args) {

List list = new ArrayList();

//添加元素。

list.add("小红");

list.add("小梅");

list.add("小强");

//2,插入元素。插入元素前的集合["小红","小梅","小强"]

list.add(1, "老王"); //插入元素后的集合["小红","老王","小梅","小强"]

//3,删除元素。

list.remove(2);// 删除元素后的集合["小红","老王","小强"]

//4,修改元素。

list.set(1, "隔壁老王");// 修改元素后的集合["小红","隔壁老王","小强"]

Iterator it = list.iterator();

while (it.hasNext()) {

String str = it.next();

System.out.println(str);

}

由于List集合拥有索引,因此List集合迭代方式除了使用迭代器之外,还可以使用索引进行迭代。

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

String str = list.get(i);

System.out.println(str);

}

}

LinkedList集合的常用方法

LinkedList集合数据存储的结构是链表结构。方便元素添加、删除的集合。实际开发中对一个集合元素的添加与删除经常涉及到首尾操作,而LinkedList提供了大量首尾操作的方法。

LinkedList是List的子类,List中的方法LinkedList都是可以使用,这里就不做详细介绍,我们只需要了解LinkedList的特有方法即可。在开发时,LinkedList集合也可以作为堆栈,队列的结构使用。

1018afb4a7fe1b59289188645581752b.png

public static void main(String[] args) {

LinkedList link = new LinkedList();

//添加元素

link.addFirst("abc1");

link.addFirst("abc2");

link.addFirst("abc3");

//获取元素

System.out.println(link.getFirst());

System.out.println(link.getLast());

//删除元素

System.out.println(link.removeFirst());

System.out.println(link.removeLast());

while(!link.isEmpty()){ //判断集合是否为空

System.out.println(link.pop()); //弹出集合中的栈顶元素

}

}

Set接口

Set接口,它里面的集合,所存储的元素是不重复的以及无序的。通过元素的equals方法,来判断是否为重复元素

HashSet集合

set集合默认调用object类中的equals()和hashCode()方法,而在object类中的equals()方法默认比较的是地址值是否相同

解决方法:在Student类中重写equals()和hashCode()方法

public static void main(String[] args) {

Set set = new HashSet<>();

Student s1 = new Student("小黑",10);

Student s2 = new Student("小白",11);

Student s3 = new Student("小白",11);

Student s4 = new Student("小黄",13);

set.add(s1);

set.add(s2);

set.add(s3);

set.add(s4);

System.out.println(set);

System.out.println("----------------------");

//通过迭代器遍历set集合

Iterator it = set.iterator();

while (it.hasNext()) {

Student s = it.next();

System.out.println(s);

}

System.out.println("----------------------");

//通过增强for遍历set集合

for (Student student : set) {

System.out.println(student);

}

}

Map集合

特点:双列集合,元素由键值对(Entry)构成: key -- value key不可以重复,value可以重复

创建集合对象

Map map = new HashMap();

T1:表示键的数据类型

T2:表示值得数据类型

成员方法:

put添加元素:

put(key,value);

如果元素第一次添加,返回null

重复添加,会用新值覆盖旧值并返回旧值

get获取元素:

get(key);

根据键获取值

keySet();

获取所以键的集合

Student s1 = new Student("小黑",10);

Student s2 = new Student("小白",11);

Student s3 = new Student("小黄",13);

Student put1 = map.put(1, s1);

Student put2 = map.put(1, s2);

map.put(2,s2);

map.put(3,s3);

Student stu1 = map.get(2);

遍历步骤

1、获取所以的键的集合 keySet();

2、遍历所有的键,获取到每一个键 迭代器,增强for

3、根据键,获取指定的值 get();

Set keys = map.keySet();

//获取迭代器对象

Iterator it = keys.iterator();

while (it.hasNext()) {

Integer key = it.next();

Student val = map.get(key);

System.out.println(key+":"+val);

}

//增强for

for (Integer key : keys) {

Student val = map.get(key);

System.out.println(val);

}

模拟斗地主发牌

public static void main(String[] args) {

// 创建数字数组:

Map pokers = new HashMap<>();

// 创建花色数组:

List list = new ArrayList<>();

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

String[] colors = {"♦","♣","♥","♠"};

int i = 0;

for (String num : nums) {

for (String color : colors) {

// 将每一个花色分别和数字进行拼接

String poker = color + num;

// 将牌的编号和具体的牌放到双列集合

pokers.put(i,poker);

// 将牌的编号放到单列集合

list.add(i);

// 编号自增

i++;

}

}

//添加大小王

pokers.put(i,"小王🃏");

list.add(i++);

pokers.put(i,"大王🃏");

list.add(i);

System.out.println("所有的牌:"+pokers);

System.out.println("所有的编号:"+list);

//洗牌

Collections.shuffle(list);

System.out.println("洗牌后的编号:"+list);

List player1 = new ArrayList<>();

List player2 = new ArrayList<>();

List player3 = new ArrayList<>();

List diPai = new ArrayList<>();

for (int j = 0; j < list.size(); j++) {

Integer pokerNum = list.get(j);

if(j >= list.size() - 3) {

diPai.add(pokerNum);

}else if(j % 3 == 0) {

player1.add(pokerNum);

}else if(j % 3 == 1) {

player2.add(pokerNum);

}else if(j % 3 == 2) {

player3.add(pokerNum);

}

}

//查看编号

System.out.println("玩家1:"+player1);

System.out.println("玩家2:"+player2);

System.out.println("玩家3:"+player3);

System.out.println("底牌:"+diPai);

//查看具体的牌

System.out.println("玩家1:"+ printPoker(player1,pokers));

System.out.println("玩家2:"+ printPoker(player2,pokers));

System.out.println("玩家3:"+ printPoker(player3,pokers));

System.out.println("底牌:"+ printPoker(diPai,pokers));

}

//看牌方法

public static String printPoker(List nums, Map pokers) {

Collections.sort(nums);

//创建字符串容器

StringBuilder sb = new StringBuilder();

for (Integer num : nums) {

String poker = pokers.get(num);

sb.append(poker + " ");

}

//转为String对象

String str = sb.toString();

return str.trim();

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值