1 Redis Set

1.1 案例1

package myset;

import redis.clients.jedis.Jedis;

import java.util.Set;

public class SetMain {


    public static void main(String[] args){

        Jedis jedis = new Jedis("127.0.0.1", 6379);
        //武林人物登记表---杜绝冒名顶替的情况
        String[] daxias = new String[]{"郭靖", "黄蓉", "令狐冲", "杨过", "林冲",
                "鲁智深", "小女龙", "虚竹", "独孤求败", "张三丰", "王重阳", "张无忌"
                , "王重阳", "东方不败", "逍遥子", "乔峰", "虚竹", "段誉"
                , "韦小宝", "王语嫣", "周芷若", "峨眉师太", "慕容复", "郭靖", "乔峰", "王重阳"};

        jedis.sadd("hero",daxias);
        //获取一个set中的所有值
        Set<String> daxiaSet=jedis.smembers("hero");
        for(String name:daxiaSet){
            System.out.print(name+" "); //set--无序,无重复元素
        }
        System.out.println();

        //判断一个元素是否属于set
        boolean isComing = jedis.sismember("hero","无名氏");
        if(!isComing){
            System.out.println("无名氏没有参加");
        }

        //计算一个set中有多少元素
        long totalNum=jedis.scard("hero");
        System.out.println("参加人数为:"+totalNum);
        System.out.println();



        //大侠无名氏没有来,是因为报名参与另外一个会议 国际武林大会
        String[] daxiaArr = new String[]{"王语嫣", "周芷若", "峨眉师太", "慕容复","郭靖", "乔峰", "无名氏"};
        jedis.sadd("superhero", daxiaArr); //国际武林大会登记表
        Set<String> xindaxias = jedis.smembers("superhero");
        for (String name : xindaxias) {
            System.out.print(name + "--- ");  //集合的特点:无序、无重复元素
        }
        System.out.println();

        /*
        * 计算2 个set之间的交集
        * */
        Set<String> users=jedis.sinter("hero","superhero");
        System.out.print("交集:");
        for(String name:users){

            System.out.print(name+" ");
        }
        System.out.println();

        //计算2个set之间的并集
        users=jedis.sunion("hero","superhero");
        System.out.print("并集:");
        for(String name:users){

            System.out.print(name+" ");
        }
        System.out.println();

        //两个集合的差集
        users = jedis.sdiff("hero","superhero");
        System.out.print("差集:");
        for(String name:users){

            System.out.print(name+" ");
        }
        System.out.println();

        //将两个集合的差集保存
        jedis.sdiffstore("stars","hero","superhero");
        for(String name:jedis.smembers("stars")){
            System.out.print(name+" ");
        }

    }



}


package myset;

import redis.clients.jedis.Jedis;

import java.util.Set;

public class SetMain {


    public static void main(String[] args){

        Jedis jedis = new Jedis("127.0.0.1", 6379);
        //武林人物登记表---杜绝冒名顶替的情况
        String[] daxias = new String[]{"郭靖", "黄蓉", "令狐冲", "杨过", "林冲",
                "鲁智深", "小女龙", "虚竹", "独孤求败", "张三丰", "王重阳", "张无忌"
                , "王重阳", "东方不败", "逍遥子", "乔峰", "虚竹", "段誉"
                , "韦小宝", "王语嫣", "周芷若", "峨眉师太", "慕容复", "郭靖", "乔峰", "王重阳"};

        jedis.sadd("hero",daxias);
        //获取一个set中的所有值
        Set<String> daxiaSet=jedis.smembers("hero");
        for(String name:daxiaSet){
            System.out.print(name+" "); //set--无序,无重复元素
        }
        System.out.println();

        //判断一个元素是否属于set
        boolean isComing = jedis.sismember("hero","无名氏");
        if(!isComing){
            System.out.println("无名氏没有参加");
        }

        //计算一个set中有多少元素
        long totalNum=jedis.scard("hero");
        System.out.println("参加人数为:"+totalNum);
        System.out.println();



        //大侠无名氏没有来,是因为报名参与另外一个会议 国际武林大会
        String[] daxiaArr = new String[]{"王语嫣", "周芷若", "峨眉师太", "慕容复","郭靖", "乔峰", "无名氏"};
        jedis.sadd("superhero", daxiaArr); //国际武林大会登记表
        Set<String> xindaxias = jedis.smembers("superhero");
        for (String name : xindaxias) {
            System.out.print(name + "--- ");  //集合的特点:无序、无重复元素
        }
        System.out.println();

        /*
        * 计算2 个set之间的交集
        * */
        Set<String> users=jedis.sinter("hero","superhero");
        System.out.print("交集:");
        for(String name:users){

            System.out.print(name+" ");
        }
        System.out.println();

        //计算2个set之间的并集
        users=jedis.sunion("hero","superhero");
        System.out.print("并集:");
        for(String name:users){

            System.out.print(name+" ");
        }
        System.out.println();

        //两个集合的差集
        users = jedis.sdiff("hero","superhero");
        System.out.print("差集:");
        for(String name:users){

            System.out.print(name+" ");
        }
        System.out.println();

        //将两个集合的差集保存
        jedis.sdiffstore("stars","hero","superhero");
        for(String name:jedis.smembers("stars")){
            System.out.print(name+" ");
        }

    }



}
D:\programs\Java\jdk1.8.0_181\bin\java.exe "-javaagent:D:\programs\JetBrains\IntelliJ IDEA 2018.2.4\lib\idea_rt.jar=53762:D:\programs\JetBrains\IntelliJ IDEA 2018.2.4\bin" -Dfile.encoding=UTF-8 -classpath D:\programs\Java\jdk1.8.0_181\jre\lib\charsets.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\deploy.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\access-bridge-64.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\cldrdata.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\dnsns.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\jaccess.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\jfxrt.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\localedata.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\nashorn.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\sunec.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\sunjce_provider.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\sunmscapi.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\sunpkcs11.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\zipfs.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\javaws.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\jce.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\jfr.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\jfxswt.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\jsse.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\management-agent.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\plugin.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\resources.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\rt.jar;D:\Data\JavaProject\myredis\target\classes;C:\Users\tzb\.m2\repository\redis\clients\jedis\2.8.2\jedis-2.8.2.jar;C:\Users\tzb\.m2\repository\org\apache\commons\commons-pool2\2.4.2\commons-pool2-2.4.2.jar;C:\Users\tzb\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\tzb\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\tzb\.m2\repository\com\google\code\gson\gson\2.3.1\gson-2.3.1.jar;C:\Users\tzb\.m2\repository\org\jsoup\jsoup\1.8.3\jsoup-1.8.3.jar myset.SetMain
周芷若 段誉 令狐冲 黄蓉 鲁智深 东方不败 林冲 逍遥子 王重阳 虚竹 韦小宝 小女龙 乔峰 张三丰 独孤求败 郭靖 杨过 慕容复 王语嫣 峨眉师太 张无忌 
无名氏没有参加
参加人数为:21

周芷若--- 王语嫣--- 慕容复--- 郭靖--- 无名氏--- 峨眉师太--- 乔峰--- 
交集:周芷若 王语嫣 慕容复 郭靖 峨眉师太 乔峰 
并集:周芷若 段誉 令狐冲 黄蓉 东方不败 鲁智深 林冲 逍遥子 王重阳 虚竹 韦小宝 小女龙 乔峰 张三丰 独孤求败 郭靖 王语嫣 慕容复 杨过 无名氏 峨眉师太 张无忌 
差集:鲁智深 张三丰 虚竹 令狐冲 小女龙 韦小宝 王重阳 杨过 逍遥子 黄蓉 林冲 段誉 独孤求败 东方不败 张无忌 
林冲 段誉 令狐冲 逍遥子 黄蓉 王重阳 韦小宝 虚竹 东方不败 鲁智深 小女龙 张三丰 独孤求败 杨过 张无忌 
Process finished with exit code 0

1.2 案例2

package myset;

import redis.clients.jedis.Jedis;

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Transform {


    public static void main(String[] args) {

        Jedis jedis = new Jedis("127.0.0.1", 6379);

        //浏览某商品的用户
        jedis.sadd("viewUsers", "郭靖", "黄蓉", "令狐冲", "杨过", "林冲",
                "鲁智深", "小女龙", "虚竹", "独孤求败", "张三丰", "王重阳", "张无忌"
                , "王重阳", "东方不败", "逍遥子", "乔峰", "虚竹", "段誉");

        //下单用户
        jedis.sadd("orderUsers", "郭靖", "黄蓉", "令狐冲", "杨过", "林冲",
                "鲁智深", "小女龙", "虚竹", "独孤求败", "乔峰", "虚竹", "段誉");
        //支付用户
        jedis.sadd("paymentUsers", "郭靖", "黄蓉", "令狐冲", "杨过", "独孤求败", "段誉");

        //浏览过商品的用户,有哪些下单了。
        jedis.sinterstore("view2order", "viewUsers", "orderUsers"); //求两个集合的交集

        /*
         * 计算浏览某商品的用户数量和既浏览又下单的用户
         * */
        double viewUserNum = jedis.scard("viewUsers");
        double orderUserNum = jedis.scard("view2order");
        NumberFormat formatter = new DecimalFormat("0.00");
        Double x = new Double(orderUserNum / viewUserNum);
        System.out.print("订单" + orderUserNum + " /浏览" + viewUserNum + "转化率:" + formatter.format(x) + " 用户是:");
        for (String name : jedis.smembers("view2order")) {
            System.out.print(name + " ");
        }
        System.out.println();


        /*
         * 浏览并且下单的,最终的支付转化
         * */
        jedis.sinterstore("order2Payment", "view2order", "paymentUsers"); //交集
        double paymentUserNum = jedis.scard("paymentUsers");
        x = new Double(paymentUserNum / orderUserNum);
        System.out.print("支付" + paymentUserNum + "/订单" + orderUserNum + "转换率:" + formatter.format(x) + "  他们是:");
        for (String name : jedis.smembers("order2Payment")) {
            System.out.print(name + " ");
        }
        System.out.println();

        //浏览并最终支付的用户转化
        x = new Double(paymentUserNum / viewUserNum);
        System.out.print("支付" + paymentUserNum + "/浏览" + viewUserNum + "转化:" + formatter.format(x)+"    他们是:");
        for(String name:jedis.smembers("order2Payment")){
            System.out.print(name+" ");

        }
        System.out.println();

    }


}


package myset;

import redis.clients.jedis.Jedis;

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Transform {


    public static void main(String[] args) {

        Jedis jedis = new Jedis("127.0.0.1", 6379);

        //浏览某商品的用户
        jedis.sadd("viewUsers", "郭靖", "黄蓉", "令狐冲", "杨过", "林冲",
                "鲁智深", "小女龙", "虚竹", "独孤求败", "张三丰", "王重阳", "张无忌"
                , "王重阳", "东方不败", "逍遥子", "乔峰", "虚竹", "段誉");

        //下单用户
        jedis.sadd("orderUsers", "郭靖", "黄蓉", "令狐冲", "杨过", "林冲",
                "鲁智深", "小女龙", "虚竹", "独孤求败", "乔峰", "虚竹", "段誉");
        //支付用户
        jedis.sadd("paymentUsers", "郭靖", "黄蓉", "令狐冲", "杨过", "独孤求败", "段誉");

        //浏览过商品的用户,有哪些下单了。
        jedis.sinterstore("view2order", "viewUsers", "orderUsers"); //求两个集合的交集

        /*
         * 计算浏览某商品的用户数量和既浏览又下单的用户
         * */
        double viewUserNum = jedis.scard("viewUsers");
        double orderUserNum = jedis.scard("view2order");
        NumberFormat formatter = new DecimalFormat("0.00");
        Double x = new Double(orderUserNum / viewUserNum);
        System.out.print("订单" + orderUserNum + " /浏览" + viewUserNum + "转化率:" + formatter.format(x) + " 用户是:");
        for (String name : jedis.smembers("view2order")) {
            System.out.print(name + " ");
        }
        System.out.println();


        /*
         * 浏览并且下单的,最终的支付转化
         * */
        jedis.sinterstore("order2Payment", "view2order", "paymentUsers"); //交集
        double paymentUserNum = jedis.scard("paymentUsers");
        x = new Double(paymentUserNum / orderUserNum);
        System.out.print("支付" + paymentUserNum + "/订单" + orderUserNum + "转换率:" + formatter.format(x) + "  他们是:");
        for (String name : jedis.smembers("order2Payment")) {
            System.out.print(name + " ");
        }
        System.out.println();

        //浏览并最终支付的用户转化
        x = new Double(paymentUserNum / viewUserNum);
        System.out.print("支付" + paymentUserNum + "/浏览" + viewUserNum + "转化:" + formatter.format(x)+"    他们是:");
        for(String name:jedis.smembers("order2Payment")){
            System.out.print(name+" ");

        }
        System.out.println();

    }


}
D:\programs\Java\jdk1.8.0_181\bin\java.exe "-javaagent:D:\programs\JetBrains\IntelliJ IDEA 2018.2.4\lib\idea_rt.jar=53243:D:\programs\JetBrains\IntelliJ IDEA 2018.2.4\bin" -Dfile.encoding=UTF-8 -classpath D:\programs\Java\jdk1.8.0_181\jre\lib\charsets.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\deploy.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\access-bridge-64.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\cldrdata.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\dnsns.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\jaccess.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\jfxrt.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\localedata.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\nashorn.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\sunec.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\sunjce_provider.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\sunmscapi.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\sunpkcs11.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\zipfs.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\javaws.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\jce.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\jfr.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\jfxswt.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\jsse.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\management-agent.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\plugin.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\resources.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\rt.jar;D:\Data\JavaProject\myredis\target\classes;C:\Users\tzb\.m2\repository\redis\clients\jedis\2.8.2\jedis-2.8.2.jar;C:\Users\tzb\.m2\repository\org\apache\commons\commons-pool2\2.4.2\commons-pool2-2.4.2.jar;C:\Users\tzb\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\tzb\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\tzb\.m2\repository\com\google\code\gson\gson\2.3.1\gson-2.3.1.jar;C:\Users\tzb\.m2\repository\org\jsoup\jsoup\1.8.3\jsoup-1.8.3.jar myset.Transform
订单11.0 /浏览16.0转化率:0.69 用户是:令狐冲 黄蓉 虚竹 鲁智深 乔峰 小女龙 林冲 段誉 独孤求败 郭靖 杨过 
支付6.0/订单11.0转换率:0.55  他们是:段誉 令狐冲 独孤求败 郭靖 杨过 黄蓉 
支付6.0/浏览16.0转化:0.38    他们是:段誉 令狐冲 独孤求败 郭靖 杨过 黄蓉 

Process finished with exit code 0