MapBuilder,操作集合工具类
public class MapBuilder {
/**
* Creates an instance of {@code HashMap}
*/
public static <K, V> HashMap<K, V> newHashMap() {
return new HashMap<>();
}
/**
* Returns the empty map.
*/
public static <K, V> Map<K, V> of() {
return newHashMap();
}
/**
* Returns map containing a single entry.
*/
public static <K, V> Map<K, V> of(K k1, V v1) {
Map<K, V> map = of();
map.put(k1, v1);
return map;
}
/**
* Returns map containing the given entries.
*/
public static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2) {
Map<K, V> map = of();
map.put(k1, v1);
map.put(k2, v2);
return map;
}
/**
* Returns map containing the given entries.
*/
public static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3) {
Map<K, V> map = of();
map.put(k1, v1);
map.put(k2, v2);
map.put(k3, v3);
return map;
}
/**
* Returns map containing the given entries.
*/
public static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
Map<K, V> map = of();
map.put(k1, v1);
map.put(k2, v2);
map.put(k3, v3);
map.put(k4, v4);
return map;
}
/**
* Returns map containing the given entries.
*/
public static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
Map<K, V> map = of();
map.put(k1, v1);
map.put(k2, v2);
map.put(k3, v3);
map.put(k4, v4);
map.put(k5, v5);
return map;
}
/**
* Returns map containing the given entries.
*/
public static <K, V> Map<K, V> of(
K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) {
Map<K, V> map = of();
map.put(k1, v1);
map.put(k2, v2);
map.put(k3, v3);
map.put(k4, v4);
map.put(k5, v5);
map.put(k6, v6);
return map;
}
/**
* Returns map containing the given entries.
*/
public static <K, V> Map<K, V> of(
K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7) {
Map<K, V> map = of();
map.put(k1, v1);
map.put(k2, v2);
map.put(k3, v3);
map.put(k4, v4);
map.put(k5, v5);
map.put(k6, v6);
map.put(k7, v7);
return map;
}
/**
* Returns map containing the given entries.
*/
public static <K, V> Builder<K, V> builder() {
return new Builder<>();
}
public static final class Builder<K, V> {
private Map<K, V> map;
private boolean underConstruction;
private Builder() {
map = newHashMap();
underConstruction = true;
}
public Builder<K, V> put(K k, V v) {
if (!underConstruction) {
throw new IllegalStateException("Underlying map has already been built");
}
map.put(k, v);
return this;
}
public Map<K, V> build() {
if (!underConstruction) {
throw new IllegalStateException("Underlying map has already been built");
}
underConstruction = false;
return map;
}
}
}
MapBuilder,操作集合工具类的更多相关文章
- Redis操作Set工具类封装,Java Redis Set命令封装
Redis操作Set工具类封装,Java Redis Set命令封装 >>>>>>>>>>>>>>>>& ...
- Java—集合工具类
集合中的元素工具类排序: Java提供了一个操作Set.List和Map等集合的工具类:Collections,该工具类提供了大量方法对集合进行排序.查询和修改等操作,还提供了将集合对象置为不可变.对 ...
- Java从零开始学二十四(集合工具类Collections)
一.Collections简介 在集合的应用开发中,集合的若干接口和若干个子类是最最常使用的,但是在JDK中提供了一种集合操作的工具类 —— Collections,可以直接通过此类方便的操作集合 二 ...
- Collections 集合工具类
集合工具类 包括很多静态方法来操作集合list 而Collections则是集合类的一个工具类/帮助类,其中提供了一系列静态方法,用于对集合中元素进行排序.搜索以及线程安全等各种操作. 1) 排序( ...
- Java操作Redis工具类
依赖 jar 包 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis< ...
- 集合工具类CollectionUtils、ListUtils、SetUtils、MapUtils探究(转)
之前一直以为集合工具类只有CollectionUtils,主要用它的isEmpty(final Collection<?> coll)静态方法来判断一个给定的集合是否为null或者是否长度 ...
- java之集合工具类Collections
Collections类简介 java.utils.Collections 是集合工具类,用来对集合进行操作.此类完全由在 collection 上进行操作或返回 collection 的静态方法组成 ...
- [Google Guava] 2.3-强大的集合工具类:java.util.Collections中未包含的集合工具
原文链接 译文链接 译者:沈义扬,校对:丁一 尚未完成: Queues, Tables工具类 任何对JDK集合框架有经验的程序员都熟悉和喜欢java.util.Collections包含的工具方法.G ...
- Collections集合工具类常用的方法
java.utils.Collections //是集合工具类,用来对集合进行操作.部分方法如下: public static <T> boolean addAll(Collection& ...
随机推荐
- ExtJS清除表格缓存
背景 在使用ExtJS时遇到不少坑,如果不影响使用也无所谓,但是有些不能忍的,比如表格数据缓存问题.如果第一次打开页面查询出一些数据展示在表格中:第二次打开,即使不查询也会有数据,这是缓存的数据. 我 ...
- cf 429 B Working out
B. Working out time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- 微信分享签名Java代码实现
最近写了一个小微信签名功能,记录一下希望用到的朋友可以参考下. RestController @RequestMapping("/api/wx") public class Wei ...
- 使用80percent开发rails程序:gem的了解。(kaminari)
学习目的: 对一些主要的gem进行学习了解基本功能: 作者的一些答复:(链接) 关于安全配置: 对于配置文件, 安全仅有一点: 不要提交任何敏感信息到服务端. 所以 rails-template 是添 ...
- 【转】ubuntu下修改文件夹权限
常用方法如下: sudo chmod 600 ××× (只有所有者有读和写的权限)sudo chmod 644 ××× (所有者有读和写的权限,组用户只有读的权限)sudo chmod 700 ××× ...
- URAL 1040 Airline Company 构造,思路 难度:2
http://acm.timus.ru/problem.aspx?space=1&num=1040 题目要求在一个联通无向图中找出一种方法给边标号使得任意一个有多条边的点,边的号码的最大公约数 ...
- ios 怎么禁止点击子视图的时候不响应父视图的点击事件
方法一 可以在触发手势的方法里添加一个区域的判断,如果点击区域正好是子视图的区域,则过滤掉,不处理此时的手势,如果点击的区域没有被子视图覆盖则,处理手势的事件.具体的代码如下: if( CGRect ...
- SharePoint Development - Custom Field using Visual Studio 2010 based SharePoint 2010
博客地址 http://blog.csdn.net/foxdave 自定义列表的时候有时候需要自定义一些字段来更好地实现列表的功能,本文讲述自定义字段的一般步骤 打开Visual Studio,我们还 ...
- c#实现对登陆信息的反馈,实现对网站登录密码的扫描
最近发现我们学校的电信上网改密码的页面很简单,没有验证码,于是我就很好奇,后来发现原来是我们学校的电信的那个改密码的页面有漏洞于是就可以通过扫描账号免费上网 原理就是对修改密码的页面进行POST请求 ...
- MyEclipse web jsp 如何调试
MyEclipse如何调试 | 浏览:882 | 更新:2014-03-13 17:38 1 2 3 4 5 分步阅读 当程序写好之后,如何调试呢? 我们在MyEclipse中jav添加断点,运行de ...