Last active
December 16, 2015 17:09
-
-
Save YukiYoshikawa/5468385 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package trial.yy.guava.client.collect; | |
| import com.google.common.collect.Lists; | |
| import java.util.HashSet; | |
| import java.util.List; | |
| import java.util.Set; | |
| /** | |
| * com.google.common.collect.Listsを試すためのサンプル | |
| * User: yy | |
| */ | |
| public class ListsClient { | |
| public static void main(String[] args) { | |
| // Lists#newArrayListでArrayListを生成(右辺がすっきり) | |
| List<String> list1 = Lists.newArrayList(); | |
| list1.add("abc"); | |
| list1.add("ghi"); | |
| list1.add("def"); | |
| System.out.println("list1: " + list1); | |
| // Lists#newArrayListでCollectionを指定してArrayList生成 | |
| Set<String> strSet = new HashSet<>(); | |
| strSet.add("mario"); | |
| strSet.add("luigi"); | |
| strSet.add("nokonoko"); | |
| List<String> list2 = Lists.newArrayList(strSet); | |
| System.out.println("list2: " + list2); | |
| // Lists#partitionで対象のListを指定した要素数のListに分割する | |
| List<String> tempList = Lists.newArrayList("a", "b", "c", "d", "e", "f", "g", "h", "i"); | |
| List<List<String>> list3 = Lists.partition(tempList, 2); | |
| System.out.println("list3: " + list3); | |
| // Lists#reverseで逆順にしてみる(Collections#reverseと違って引数のListの並び順は変化しない) | |
| List<String> list4 = Lists.newArrayList("a", "b", "c", "d"); | |
| List<String> list5 = Lists.reverse(list4); | |
| System.out.println("list4: " + list4); | |
| System.out.println("list5: " + list5); | |
| // Lists#asListでList生成(第1引数の値が先頭に、第2引数の配列の各要素がそれ以降にセットされる) | |
| List<String> list6 = Lists.asList("first", new String[] { "1", "2", "3" }); | |
| System.out.println("list6: " + list6); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment