关于java基础:JAVA基础之Collection的Lambda

47次阅读

共计 2615 个字符,预计需要花费 7 分钟才能阅读完成。

明天讲一下 Collection,以及 Collection 和 Lamdba 的联合

Collection 包含
List(ArrayList,LinkedList)
Set(HashSet)— SortedSet(TreeSet)
Queue(PriorityQueue)—–Deque(LinkedList,ArrayDeque)
Map(HashMap)— SortedMap(TreeMap)(Map 不属于 Collection)
对于 Iterable

boolean forEach(Cosumer<? super E> consumer);

对于 Collection

boolean removeIf(Predicate<? super E> filter);

对于 List

boolean replaceAll(UnaryOperator<? super E> operator);
boolean sort(Comparator<? super E> comparator);

public class Main {public static void main(String[] args) {List<BankAccount> bankAccountList = new ArrayList<>();
        bankAccountList.forEach(System.out::println);
        bankAccountList.removeIf(bankAccount -> bankAccount.getBalance()>20);
        List<String> names = new ArrayList<>();
        names.replaceAll(name->name.toUpperCase());
        names.replaceAll(String::toUpperCase);
        bankAccountList.sort(Comparator.comparing(BankAccount::getBalance)
                .thenComparing(BankAccount::getId));
    }
}

对于 Map

void forEach(BiConsumer<? super K, ? super V> consumer);

Map<String,List<BankAccount>> map = new HashMap<>();
map.put("test",bankAccountList);
map.forEach((city,list)-> System.out.println(city+":"+list.size() +"account"));
输入:test: 0 account

putIfAbsent 办法,能够间接 get 后再间接调用 add 办法

Map<String, List<Person>> map = new HashMap<>();
map.putIfAbsent("boston",new ArrayList<>());
map.get("boston").add(new Person());

compute 办法,返回的值为新值,同 put 办法相同,put 办法返回的是旧值
computeIfAbsent 办法,只有当 key 对应的 value 为空时才放入新值,并返回新值
computeIfPresent 办法,只有当 key 对应的 value 为非空时才放入新值,并返回新值

V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)
V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) 
V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) 
Map<String,String> map1 = new HashMap<>();
String val= map1.put("test","3");
System.out.println(val);
val= map1.compute("test", (k,v) -> "v");
System.out.println(val);
val = map1.computeIfAbsent("test",k -> "s");
System.out.println(val);
val = map1.computeIfPresent("test",(k,v)  -> "s1");
System.out.println(val);
输入
null
v
v
s1

computeIfAbsent 办法,能够间接获取到新值并间接设置,写法简便

Map<String, Map<String,Person>> map = new HashMap<>();
map.computeIfAbsent("one",key -> new HashMap<String,Person>)
.put("two",john);

Map<String, List<Person>> map = new HashMap<>();
map.computeIfAbsent("one",key -> new ArrayList<Person>())
.add(john);

merge 办法,获取原有 key 对应的 value,通过 Function 操作,设置该 key 的值,并返回新值

V merge(K key, V value,
        BiFunction<? super V, ? super V, ? extends V> remappingFunction)
// 接下面的将 test 的 value 设置为 s1        
val =map1.merge("test","dd",(k,v)-> k+v);
System.out.println(val);
输入
s1dd

或者循环两个 map 的值,将其内容合并

Map<String,List<BankAccount>> map2 = new HashMap<>();
Map<String,List<BankAccount>> map3 = new HashMap<>();
map3.forEach((key,value) ->
                map2.merge(key, value, (existingBankList, newBankList)->{existingBankList.addAll(newBankList);
                    return existingBankList;
                })
);

正文完
 0