java类List,Map以及Set常用方法-java(4)

前言

总结一下java中常用的类及其方法吧,免得忘记没有地方寻找。

正文

List

即集合,里面的元素可以相同也可以不同。

构造方法

		List<Object b> arrayList = new ArrayList<>();
		List<Object b> linkedList = new LinkedList<>();
	

以类User为例
User.java

public class User {
	
	private int id;
	private String name;
	
	public User(int id,String name) {
		this.id = id;
		this.name = name;
	}
	

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + "]";
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	

}

Example.java

public class Example {

	public static void main(String[] args) {
		List<User> arrayList = new ArrayList<>();
		List<User> linkedList = new LinkedList<>();
		}
}

元素操作

  • 新增元素
    boolean add(Object b)新增成功则返回true,否则为false;
  • 删除元素
    remove(int index)按照索引删除元素,remove(Object b)按照内容删除元素。
  • 查询元素
    get(int index)返回索引下的元素的值。
    测试代码如下:
public class Example {

	public static void main(String[] args) {
		List<User> arrayList = new ArrayList<>();
		List<User> linkedList = new LinkedList<>();
		arrayList.add(new User(1,"user1"));
		arrayList.add(new User(2,"user2"));
		arrayList.add(new User(3,"user3"));
		
		System.out.println(arrayList);//输出User对象 
		System.out.println(linkedList.add(new User(21,"xwds"))); //测试add返回结果
		System.out.println(arrayList.get(0));
		arrayList.remove(0);
		System.out.println(arrayList); 
		
		
			
	}

}
  • 查询是否包含某个元素
    contains(Object b) 包含则返回true,否则false
String string = "apple";
		List<String> list = new ArrayList<>();
		list.add("apple");
		list.add("fruit");
		if (list.contains(string)) {
			System.out.println("true");
		} else {
			System.out.println("false");
		}
  • 链表长度
    size()返回链表长度
public class Example {
    public static void main(String args[]) {
        String string = "apple";
        List<String> list = new ArrayList<>();
        list.add("apple");
        list.add("fruit");
        System.out.println(list.size());
    }
}

  • 第一次与最后一次出现的位置
    indexOf(Object b)返回第一次出现元素的位置,不存在则返回-1
    lastIndexOf(Object b)返回最后一次出现的位置,不存在则返回-1
public class Example {
    public static void main(String args[]) {
        List<String> list = new ArrayList<>();
        list.add("apple");
        list.add("fruit");
        list.add("apple");
        System.out.println(list.indexOf("apple"));
        System.out.println(list.lastIndexOf("apple"));
    }
}
  • 判断是否为空
    isEmpty()为空则为true,否则为false
public class Example {
    public static void main(String args[]) {
        List<String> list = new ArrayList<>();
        list.add("apple");
        list.add("fruit");
        list.add("apple");
        System.out.println(list.isEmpty());
    }
}
  • 将某一索引元素替换
    set(int index, Object b)成功则返回true,否则为false
public class Example {
    public static void main(String args[]) {
        List<String> list = new ArrayList<>();
        list.add("apple");
        list.add("fruit");
        list.add("apple");
        list.set(0, "苹果被我吃了");
        System.out.println(list.get(0));
        
        


    }
}

  • 向指定位置添加元素
    add(int index, Object b)成功则返回true,否则为false
public class Example {
    public static void main(String args[]) {
        List<String> list = new ArrayList<>();
        list.add("apple");
        list.add("fruit");
        list.add("apple");
        list.add(0, "向指定位置添加元素");
        System.out.println(list.get(0));

    }
}

遍历

  • for循环遍历
public class Example {
    public static void main(String args[]) {
        List<String> list = new ArrayList<>();
        list.add("apple");
        list.add("fruit");
        list.add("apple");
        for (int i=0;i<list.size();i++) {
            System.out.println(list.get(i));
        }



    }
}
  • Iterator
public class Example {
    public static void main(String args[]) {
        List<String> list = new ArrayList<>();
        list.add("apple");
        list.add("fruit");
        list.add("apple");
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}

转为数组

  • toArray()
public class Example {
    public static void main(String args[]) {
        List<String> list = new ArrayList<>();
        list.add("apple");
        list.add("fruit");
        list.add("apple");
        System.out.println(list.toArray()[0]);
 
    }
}

转为字符串

  • toString()
public class Example {
    public static void main(String args[]) {
        List<String> list = new ArrayList<>();
        list.add("apple");
        list.add("fruit");
        list.add("apple");
        System.out.println(list.toString());
    }
}

Set

Set中不允许有重复元素

  • HashSet<>():可以快速访问,不进行排序

  • TreeSet<>():树集,进行排序,下一层比上一层小,同一层从左到右递增。

  • LinkedHashSet<>():介于以上二者之间,内部顺序与插入顺序一样

  • add()
    为何元素不重复呢?
    在进行add()方法时,会首先判断hash值是否相同,不同则存,相同则判断equals(),相同则不存,不同则存。
    所以可以重写hashCode()以及equals()做到随心所欲的存储。
    equals()

 @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        User user = (User) o;

        if (id != user.id) return false;
        if (age != user.age) return false;
        return name != null ? name.equals(user.name) : user.name == null;
        
    }

hashCode()

  @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + age;
        return result;
       
    }
  • remove()移除某个元素
  • removeAll()移除所有

set没有下标,不能修改其他方法均与list类似。

Map

Map是存储是键-值对,并且要保证键的唯一性。
常用方法:

  • put(key,value)添加元素
  • get(key)获取元素
  • remove(key) remove(key,value)删除元素
  • values()所有value值
  • keySet()获得key的集合
  • 遍历
  1. 通过keySet(),先得到key的集合然后迭代。
  2. entrySet()得到Map<Map.Entry<key,value>>集合,然后迭代
public static void main(String args[]) {
        Map<String,String> map = new HashMap<>();
        map.put("1", "first");
        map.put("2", "second");
        map.put("3", "third");
        Set<Map.Entry<String, String>> mapSet = map.entrySet();
        Iterator<Map.Entry<String, String>> iterator = mapSet.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> mapEnter = iterator.next();
            System.out.println(mapEnter.getKey()+" = "+mapEnter.getValue());
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值