Java container-introducing the Guava class library

Source: Internet
Author: User

first, Directory

1. read-only Settings

2. Functional Programming + Modular programming

3. Constraint conditions

4. Set Operation (combination, difference set, Intersection)

second, The code implementation

1. read-only Settings

 public Static voidmain (String [] Args) {//read-only SettingsList ls=NewArrayList (); Ls.add (A); Ls.add ("b"); Ls.add (C); //class libraries that do not use guavaList <string > readlist=collections.unmodifiablelist (ls); //Readlist.add ("d"); Error//using the Guava class librarylist<string> imutablelist= immutablelist.of ("a", "b", "c"); //Imutablelist.add ("a"); Operation error
}

2. Function-type Programming

(1) function One: Find the set of Palindrome string, Palindrome also known as Mirror word, backword, refers to the string from the front or back to read the same, such as Moom

//result: Moom because Moon is moom after reverse order     public Static voidmain (string[] Args) {List<String> list = lists.newarraylist ("dog", "cat", "pig", "moom"); Collection<String> Plist=collections2.filter (list,NewPredicate<string>() {             public BooleanApply (String S) {return NewStringBuilder (s). Reverse (). toString (). equals (s);        }        }); //Anonymous inner class, create object at the same time, collections2.filter similar filter         for(Object O:plist) {System.out.println (o); }    }

(2) function Two: Date conversion

Results: 1970-01-01    1970-01-24   1970-01-02
  public Static voidmain (String [] Args) {Set<Long> timeset=Sets.newhashset (); Timeset.add (1000L); Timeset.add (2000l*1000000); Timeset.add (3000l*20000); Collection<String> translist= Collections2.transform (timeset,Newfunction<long, string>() {             publicString Apply (Long input) {return NewSimpleDateFormat ("yyyy-mm-dd"). Format (input);        }        });  for(String S:translist) {System.out.println (s); }    }

(3) function Three: Modular programming

 public Static voidmain (String [] Args) {List<String> list = lists.newarraylist ("happy", "sad", "wahaha"); //Method Onefunction<string,string>f1=Newfunction<string, string>() {             publicstring Apply (string S) {returnS.length () >5&&s.length () <20?s: "error";        }        }; //method Two: all uppercase lettersfunction<string, string> F2 =Newfunction<string, string>() {             publicstring Apply (string Input) {returnInput.touppercase ();        }        }; //Combination Methodfunction<string, string> f =functions.compose (f1, f2); Collection Resultcol=Collections2.transform (list,f);  for(Object S:resultcol) {System.out.println (s); }    }

3. Constraint conditions

  public Static voidmain (string[] Args) {Set<String> sets =Sets.newhashset (); //Create a constraintConstraint<string> Constraint =NewConstraint<string>() {@Override publicstring checkelement (string element) {//non-null ValidationPreconditions.checknotnull (element); //length limit 5-20, otherwise errorpreconditions.checkargument (element.length ()>= 5 && element.length () <= 20, element); returnelement;        }        }; Set<String> cs =Constraints.constrainedset (sets, constraint); //cs.add (null); Error java.lang.NullPointerException//cs.add ("qaz"); Error Java.lang.IllegalArgumentException:qaz}

4, intersection, set, difference set

  public Static voidmain (String [] Args) {Set<Integer> Sets=sets.newhashset (1,2,3,4,5,6); Set<Integer> Set2=sets.newhashset (3,4,5,6,7,8,9); Sets.setview<Integer> intersection =sets.intersection (sets, set2);  for(Integer In:intersection) {System.out.print ( in+"  "); } System.out.println (""); //Difference SetSets.setview<integer> intersection2=sets.difference (sets,set2);  for(Integer in:intersection2) {System.out.print ( in+"  "); } System.out.println (""); //and setSets.setview<integer> intersection3=sets.union (sets,set2);  for(Integer in:intersection3) {System.out.print ( in+"  "); }    }

Java container-introducing The Guava class library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.