java 8 hashmap 遍历方式

public class LambdaMap {

private Map map = new HashMap<>();

@Before
public void initData() {
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");
    map.put("key4", 4);
    map.put("key5", 5);
    map.put("key5", 'h');
}


/**
 * 遍历Map的方式一
 * 通过Map.keySet遍历key和value
 */
@Test
public void testErgodicWayOne() {
    System.out.println("---------------------Before JAVA8 ------------------------------");
    for (String key : map.keySet()) {
        System.out.println("map.get(" + key + ") = " + map.get(key));
    }
    System.out.println("---------------------JAVA8 ------------------------------");
    map.keySet().forEach(key -> System.out.println("map.get(" + key + ") = " + map.get(key)));
}

/**
 * 遍历Map第二种
 * 通过Map.entrySet使用Iterator遍历key和value
 */
@Test
public void testErgodicWayTwo() {
    System.out.println("---------------------Before JAVA8 ------------------------------");
    Iterator> iterator = map.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry entry = iterator.next();
        System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue());
    }
    System.out.println("---------------------JAVA8 ------------------------------");
    map.entrySet().iterator().forEachRemaining(item -> System.out.println("key:value=" + item.getKey() + ":" + item.getValue()));
}

/**
 * 遍历Map第三种
 * 通过Map.entrySet遍历key和value,在大容量时推荐使用
 */
@Test
public void testErgodicWayThree() {
    System.out.println("---------------------Before JAVA8 ------------------------------");
    for (Map.Entry entry : map.entrySet()) {
        System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue());
    }
    System.out.println("---------------------JAVA8 ------------------------------");
    map.entrySet().forEach(entry -> System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue()));
}

/**
 * 遍历Map第四种
 * 通过Map.values()遍历所有的value,但不能遍历key
 */
@Test
public void testErgodicWayFour() {
    System.out.println("---------------------Before JAVA8 ------------------------------");
    for (Object value : map.values()) {
        System.out.println("map.value = " + value);
    }
    System.out.println("---------------------JAVA8 ------------------------------");
    map.values().forEach(System.out::println); // 等价于map.values().forEach(value -> System.out.println(value));
}

/**
 * 遍历Map第五种
 * 通过k,v遍历,Java8独有的
 */
@Test
public void testErgodicWayFive() {
    System.out.println("---------------------Only JAVA8 ------------------------------");
    map.forEach((k, v) -> System.out.println("key:value = " + k + ":" + v));
}

}

	 //按提交时间降序--stream写法
 List companyList =this.mapper.selectCompany(param);//根据参数查询符合的实体列表
companyList = companyList.stream().sorted(Comparator.comparing(Company::getCreateTime).reversed()).collect(Collectors.toList());//根据创建时间倒排

//按提交时间降序 --Lamdba表达式
Collections.sort(companyList, (a, b) -> b.getCreateTime().compareTo(a.getCreateTime()));

java8 stream map 的例子

        //  符合规则的数据
        items.forEach(item -> {
            item.getLocationInfos().getLocationInfo().stream().filter(locationInfo ->
                    !locationInfo.getLocationName().contains("DCL") &&
                            !locationInfo.getLocationName().equals("FHYCCW")
                            && !locationInfo.getLocationName().equals("RKHCCW")
                            && !locationInfo.getLocationName().equals("XXX")
                            && !locationInfo.getLocationName().contains("JXQ")
                            && !locationInfo.getLocationName().equals("BFP")
                            && Integer.valueOf(locationInfo.getQuantity()) > 0
                            && Integer.valueOf(locationInfo.getQuantity()) > 0).
                    map(locationInfo -> item).forEach(filterList::add);
        });

你可能感兴趣的:(java8,java)