jdk1.8特性2

public class User {
	private Long id;
	private String userName;
	private String roleName;
	private String password;
	private Integer age;
	private Integer gender;
	private String nickName;
	private Double salary;
	private StatsType Type;
	
	/**
	 * 
	 * @param id
	 * @param userName
	 * @param roleName
	 * @param password
	 * @param age
	 * @param gender
	 * @param nickName
	 * @param salary
	 * @param Type
	 */
	public User(Long id,String userName,String roleName,String password,Integer age,Integer gender,String nickName,Double salary,StatsType Type) {
		this.id = id;
		this.userName = userName;
		this.roleName = roleName;
		this.password = password;
		this.age = age;
		this.gender = gender;
		this.nickName = nickName;
		this.salary = salary;
		this.Type = Type;
	}

	/**
	 * 分段类型
	 */
	public static enum StatsType {

		OFFLINE((short) 0), NOTWORK((short) 1), WORKING((short) 2);

		private short value;

		private StatsType(short value) {
			this.value = value;
		}

		public void setValue(short value) {
			this.value = value;
		}

		public short getValue() {
			return value;
		}

		public static StatsType valueOf(Short value) {

			if (value == null) {
				return null;
			}

			switch (value) {
			case 0:
				return OFFLINE;// 缺勤
			case 1:
				return NOTWORK;// 闲置
			case 2:
				return WORKING;// 工作
			}
			return null;
		}
	}

	get,set.....

}

  

List<User> list2 = Lists.newArrayList(
				new User(1L,"sa","职员","***",18,0,"张三",13242.69,User.StatsType.NOTWORK), 
				new User(2L,"yunying","职员","***",30,1,"李四",12746.36,User.StatsType.WORKING),
				new User(3L,"ta","经理","***",50,0,"王五",15312.98,User.StatsType.OFFLINE)
				);

  取出对象中某个属性值

private static List<String> getUserName(List<User> list) {
return list.stream().map(User::getUserName).collect(Collectors.toList());
}

  List<User> 分组(根据性别)

private static Map<Integer, List<User>> getGroupMapByGender(List<User> userList) {
return userList.stream().collect(Collectors.groupingBy(User::getGender));
}

  

Map<Integer, List<User>> map = getGroupMapByGender(list2);
		for (Integer key : map.keySet()) {
			System.out.println(key + "---" + JSONArray.toJSONString((List<User>) map.get(key)));
		}

  求两个数组的交集

String str = "a,b,c,d,e";
String str1 = "a,b,c,d,e,f,g";
List<String> list = Arrays.asList(str.split(","));
List<String> list1 = Arrays.asList(str1.split(","));

  

private static List<String> getMixList(List<String> orgList, List<String> deskList) {
		return orgList.stream().filter(t -> deskList.contains(t)).collect(Collectors.toList());
	}

  对map的value进行排序

Map<String, Integer> mapData = new HashMap<>();
mapData.put("aa", 20);
mapData.put("bb", 7);
mapData.put("cc", 10);

  

private static Map<String, Integer> sortOrderByMapValue(Map<String, Integer> map) {
		return map.entrySet().stream().sorted(Map.Entry.<String, Integer> comparingByValue())
				.collect(Collectors.toMap(k -> k.getKey(), v -> v.getValue(), (k, v) -> k, LinkedHashMap::new));
	}

  List转Map(抽取某一属性做value)

private static Map<String, String> getList2Map(List<User> list) {
return list.stream().collect(Collectors.toMap(User::getUserName, User::getNickName));
}

  

     List转Map(value为对象本身)

private static Map<String, User> getList2Map2(List<User> list) {
		return list.stream()
				.collect(Collectors.toMap(User::getUserName, User -> User, (key1, key2) -> key2, LinkedHashMap::new));
	}

  数组中某属性最大值

private static Optional<User> getMaxAgeByList(List<User> list) {
		return list.stream().max((e1, e2) -> Integer.compare(e1.getAge(), e2.getAge()));
	}

  

 分组查询职员数量

Map<String,Long> map5 = list2.stream()
                .collect(Collectors.groupingBy(User::getRoleName,Collectors.counting()));
        System.out.println(map5);

  数组中返回的第一个元素

private static Optional<User> getFirstValueByList(List<User> list) {
		return list.stream().findFirst();
	}

  数组中属性是否有某值(是否所有人都没在工作)

private static boolean isHasNoneValueByList(List<User> list) {
		return list.stream().noneMatch((e) -> e.getType().equals(User.StatsType.WORKING));
	}

  数组中属性是否有某值(是否所有人都在工作)

private static boolean isHasAllValueByList(List<User> list) {
		return list.stream().allMatch((e) -> e.getType().equals(User.StatsType.WORKING));
	}

  数组中属性是否有某值(是否有人没来)

private static boolean isHasAnyValueByList(List<User> list) {
return list.stream().anyMatch((e) -> e.getType().equals(User.StatsType.OFFLINE));
}

  数组中某属性最小值

private static Optional<User> getMinAgeByList(List<User> list2) {
		return list2.stream().min((e1, e2) -> Integer.compare(e1.getAge(), e2.getAge()));
	}

System.out.println(user4.get().getAge());

  

posted @ 2019-06-04 13:42  蔡徐坤1987  阅读(282)  评论(0编辑  收藏  举报