List of usage examples for java.util List add
boolean add(E e);
From source file:com.manisha.allmybooksarepacked.utility.JSONUtils.java
public static void main(String[] args) throws IOException { Book one = new Book(); one.setIsbn10(UUID.randomUUID().toString()); Book two = new Book(); one.setIsbn10(UUID.randomUUID().toString()); List<Book> bookList = new ArrayList<>(); bookList.add(one); bookList.add(two);// w w w .j a v a2 s. c om System.out.println(objectToJSON(bookList)); }
From source file:Main.java
public static void main(String[] argv) { List<Person> persons = new ArrayList<>(); persons.add(new Person("Joe", 12)); persons.add(new Person("Jim", 34)); persons.add(new Person("John", 23)); List<Person> list = persons.stream().filter(p -> p.getAge() > 18).collect(Collectors.toList()); list.forEach(p -> System.out.println(p.getFirstName())); }
From source file:Main.java
public static void main(String[] args) { List<String> yearList = new ArrayList<>(25); yearList.add("042015"); yearList.add("052015"); yearList.add("062015"); yearList.add("072015"); yearList.add("082015"); yearList.add("092015"); yearList.add("102010"); yearList.add("112010"); yearList.add("122010"); yearList.add("012015"); yearList.add("022015"); yearList.add("032015"); Collections.sort(yearList, new Comparator<String>() { private DateFormat format = new SimpleDateFormat("MMyyyy"); @Override//from www .ja v a 2s .c o m public int compare(String o1, String o2) { int result = 0; try { Date d1 = format.parse(o1); try { Date d2 = format.parse(o2); result = d1.compareTo(d2); } catch (ParseException ex) { result = -1; } } catch (ParseException ex) { result = 1; } return result; } }); System.out.println(yearList); }
From source file:SafeListCopy.java
public static void main(String[] args) { List wordList = Collections.synchronizedList(new ArrayList()); wordList.add("Synchronization"); wordList.add("is"); wordList.add("important"); String[] wordA = (String[]) wordList.toArray(new String[0]); printWords(wordA);/* w w w . j a v a 2s.c o m*/ String[] wordB; synchronized (wordList) { int size = wordList.size(); wordB = new String[size]; wordList.toArray(wordB); } printWords(wordB); // Third technique (the 'synchronized' *is* necessary) String[] wordC; synchronized (wordList) { wordC = (String[]) wordList.toArray(new String[wordList.size()]); } printWords(wordC); }
From source file:Main.java
public static void main(String[] args) throws java.lang.Exception { List<Person> people = new ArrayList<Person>(); people.add(new Person("J", "S")); people.add(new Person("J", "S")); people.add(new Person("J", "F")); people.add(new Person("J", "W")); people.add(new Person("J", "B")); Set<Object> seen = new HashSet<Object>(); for (Person p : people) { Wrap wrap = new Wrap(p); if (seen.add(wrap)) { System.out.println(p + " is new"); } else {/*from w w w . j a va 2s.co m*/ System.out.println(p + " is a duplicate"); } } }
From source file:Main.java
public static void main(String[] args) { List<Student> members = new ArrayList<>(); members.add(new Student("P", Sex.MALE, 12, "p@g.com")); members.add(new Student("J", Sex.MALE, 42, "j@g.com")); members.add(new Student("K", Sex.FEMALE, 22, "k@g.com")); members.add(new Student("L", Sex.FEMALE, 52, "l@g.com")); for (Sex gender : Sex.values()) { System.out.println("All " + gender + ":"); processMembersWithFunction(members, p -> p.getGender() == gender, Student::getName, System.out::println); }//from w w w .j ava 2s. c o m System.out.println("All FEMALE over 30:"); processMembersWithFunction(members, p -> p.getGender() == Sex.FEMALE && p.getAge() > 30, Student::getName, System.out::println); }
From source file:Main.java
public static void main(final String[] args) { final List<String> asu = new ArrayList<String>(); asu.add("2"); asu.add("11"); asu.add("7"); asu.add("10"); asu.add("7"); asu.add("12"); asu.add("2"); asu.add("11"); asu.add("11"); asu.add("7"); asu.add("7"); asu.add("7"); List<String> list = new ArrayList<String>(); Map<String, Integer> counts = new HashMap<String, Integer>(); list.addAll(asu);/*from w ww . j av a2s .c om*/ for (String item : list) { Integer count = counts.get(item); if (count == null) { count = 1; } else { count = count + 1; } counts.put(item, count); } Collections.sort(asu, new Comparator<String>() { @Override public int compare(final String left, final String right) { int result = counts.get(left).compareTo(counts.get(right)); if (result == 0) { result = left.compareTo(right); } return result; } }); System.out.println(asu); }
From source file:MainClass.java
public static void main(String[] args) { String[] coins = { "A", "B", "C", "D", "E" }; List src = new LinkedList(); for (int i = 0; i < coins.length; i++) src.add(coins[i]); List dst = new ArrayList(); for (int i = 0; i < coins.length; i++) dst.add(""); Collections.copy(dst, src);/*from www . j ava 2 s . c o m*/ ListIterator liter = dst.listIterator(); while (liter.hasNext()) System.out.println(liter.next()); Collections.fill(src, "no coins"); liter = src.listIterator(); while (liter.hasNext()) System.out.println(liter.next()); }
From source file:ShuffleTest.java
public static void main(String[] args) { List<Integer> numbers = new ArrayList<Integer>(); for (int i = 1; i <= 49; i++) numbers.add(i); Collections.shuffle(numbers); List<Integer> winningCombination = numbers.subList(0, 6); Collections.sort(winningCombination); System.out.println(winningCombination); }
From source file:Main.java
public static void main(String args[]) { List<Character> ll = new LinkedList<Character>(); for (char n = 'A'; n <= 'F'; n++) ll.add(n); System.out.println(ll);//from w w w .j a va 2s . c om Collections.reverse(ll); System.out.println(ll); Collections.rotate(ll, 2); System.out.println(ll); Collections.shuffle(ll); System.out.println(ll); }